Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, 2 May 2011

Gedit plugin to emulate emacs alt-q

Here is a small gedit plugin for all the people who are like me on an emacs rehab.

If there is one emacs feature I really miss when using gedit -which is by the way a great text editor- this is the alt-q command (fill-paragraph), that automatically reformats the current paragraph according to the margin width.

Saturday, 20 February 2010

laoshi : A Chinese learning application


I had some free time recently so I created a small application to learn Chinese. The name "laoshi" is the pinyin for "老師" which means "teacher".

For the moment the application features :
  • A dictionary, using the database from cedict containing more than 90,000 entries.
  • A lessons viewer, with automatic dictionary lookup when we pass the mouse over the text.
  • A flash card player.
  • Two lessons that I wrote for beginners.
I wrote everything in python using gtk for the interface, so it should work on every platform. I don't provide any package yet, but people interested can check the code at google code.

Friday, 7 August 2009

Check python coding style on the fly with emacs


A nice emacs trick : using flymake-mode and this python code styles PEP8 checker script written by Johann C. Rocholl, we can have automatic real time checking for standard python coding style.

To make this work I copied the script (pep8.py) in my PATH, and then I added this block of code in my .emac file :

(when (load "flymake" t)
(defun flymake-pylint-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
(list "pep8.py" (list "--repeat" local-file))))

(add-to-list 'flymake-allowed-file-name-masks
'("\\.py\\'" flymake-pylint-init)))

After this I can just enable flymake mode when I edit a python file, and every coding style error will be highlighted on the fly.

Sunday, 31 May 2009

Tichy 1.1.0 released

Yesterday I released tichy 1.1.0. In this new release a lot of internal refactoring, improvement of the style system, the text editor, the terminal, and the PIM applications. I also added some unit tests using py.tests.

See the release notes.

Thursday, 22 January 2009

online javascript game, in python

Last time I spoke about pypy, which allow (among other things) to translate python code into other languages.

This week I wrote a small video game (inspired by the famous boulder dash game) in rpython and used pypy to create an online version of the game out of it.

You can play the game here.
The sources can be found here.

The game as it is now is not really fun, but I only wrote it as an example of using pypy.

What I like with this approach is that I can develop in rpython, and even run the game using python interpreter, and only before I publish it use pypy to create the javascript version.

I could also create a C version using the same code for the game engine (only the graphic functions would have to be rewritten for each backends.)

This open the door to a lot of interesting applications.

On a side note, the pypy javascript translation is really not optimised. For example, let's try to guess what this generated function does :

function (){
var v1086,v1087,self_145;
var block = 0;
for(;;){
switch(block){
case 0:
self_144 = this;
self_145 = self_144;
block = 1;
break;
case 1:
v1086 = 'H';
block = 2;
break;
case 2:
return ( v1086 );
}
}
}

I don't want to spoil the fun of figuring this out by giving the answer...

Sunday, 11 January 2009

Pypy is great

Even though I was in holidays recently, I spent some time to have a look at the update from the pypy project.

I get really excited by what the project has achieved so far.

Pypy is a python interpreter written in python.
The most interesting part of the project is the ability to use it to translate python code into other languages, like C, java, javascript, and LLVM.

So far only a subset of python language is translatable (for example generators won't work.) [edit: as the pypy people pointed out, full python language will never be translatable, that is not the goal of the project.]

But this simplified python (called RPython) is already good enough for many projects. As an example pypy interpreter itself is written in RPython and so can be compiled into machine code.

The good thing about this is that you can develop an application totally in python and then use the translators to generate fast machine code. So python could become some sort of universal language that replaces all other languages.

An other interesting things about pypy is the ability to generate stackless code, that allow many cool things, like tasklets (see the documentation about this).

Now I can't help thinking about OpenMoko paroli project, that uses python and tasklets. Could it be possible to use pypy to translate the python code into machine code and so create optimized version of paroli ?

The idea is very seducing, but many problems would need to be solved before we can get to there. The first thing is that we would have to modify our code to remove all the things that are not supported by pypy.

The second things is that even though pypy supports tasklets, it is impossible to do it using the python yield statement and the trick explained in PEP342.

An other good use of pypy would be to create web application without having to write -quite ugly- javascript code.

Paroli get a website

Happy new year everybody !
I just come back from holidays, and haven't worked for a few weeks, but now I am back to Taipei where I still work for OpenMoko, with a few news about our next software stack :

The tichy project had a lot of modifications ; it is now entirely based on etk and edje (part of raster enlightenment windows manager). It has also been renamed "paroli-core". We loose the ability to use different graphic back-ends, but the other features are still presents. Paroli-core offers :
  • Signal / slot objects (à la Qt.)
  • A service system to allow user to register and retrieve python object based on there role.
  • A tasklet library for easy creation of chained callback function.
  • A plug-in system.
  • A set of services that create a layer between the plug-ins and Mikey FSO framework.

The Paroli project based on paroli-core started.
The project includes paroli-core (ex tichy), plus a set of applets that aim at providing basic telephony functionalities.

More information (and the sources) can be found from the paroli-project web site.

Sunday, 18 May 2008

Calling Vala code from Python

Today I gave a try at using vala and python together in my OpenMoko mobile phone.
Python is a powerful object oriented script language, and vala is a C gobject compiler that makes it easy to generate object oriented code in C. It is quite easy to mix python and vala in a same programme, allowing us to write very quickly modular and fast applications.

So here is how it works :

Let say I have a vala module, that defines one class, with one public method. Here is the file (test.vala) :

using GLib;

namespace Test {
public class Test : Object {
public int sum(int x, int y) {
return x + y;
}
}
}

I can generate the C/gobject code for this module, using vala :

valac -C test.vala

this command will create test.c and test.h. These files are conform C and can be compiled with gcc (in fact these are the files people using gobject in C would write. If we have a look at it, we understand the pain that it is to write gobject code in C !).
But to be able to call the functions from python, we need to create wrapper to the python C interface. This can be easily done using some tools (included in the debian pygtk-dev package). For more information, see this page.

Here are the commands that generate our C python interface :

python /usr/share/pygtk/2.0/codegen/h2def.py test.h > test.defs
pygtk-codegen-2.0 -o test.override -p test test.defs > test_wrap.c

Where 'test.override' is a compulsory file that looks like this :

%%
headers
#include
#include "pygobject.h"
#include "test.h"
%%
modulename test
%%
import gobject.GObject as PyGObject_Type
%%
ignore-glob
*_get_type
%%

Alright we now have a file called test_wrap.c, that contains all the wrapper functions from the python C interface to gobject C.

The next step is to actually create the C library callable from python. this library must contains a function 'inittest' (this is the function that will be called when we import the module from python.) The file looks like this (test_module.c) :

#include

void test_register_classes (PyObject *d);
extern PyMethodDef test_functions[];

DL_EXPORT(void)
inittest(void)
{
PyObject *m, *d;
init_pygobject();
m = Py_InitModule("test", test_functions);
d = PyModule_GetDict(m);
test_register_classes(d);
if (PyErr_Occurred ()) {
Py_FatalError ("can't initialise module test");
}
}

And then we are almost done... finally we compile and link everything :

CFLAGS="`pkg-config --cflags gtk+-2.0 pygtk-2.0` -I/usr/include/python2.5/ -I."
LDFLAGS=`pkg-config --libs gtk+-2.0 pygtk-2.0`
gcc -c -o test.o test.c
gcc $CFLAGS -c test_wrap.c -o test_wrap.o
gcc $CFLAGS -c test_module.c -o test_module.o
gcc $LDFLAGS -shared test.o test_wrap.o test_module.o -o test.so

And now we finally have our library : test.so, that can be directly used from python. Here is our python script (test.py):

import test
t = test.Test()
print t.sum(2,3)

Conclusion :
This combination (python + vala) is ideal to write applications for OpenMoko : there is no need to compile the python part, and since the vala code generate C code, it can be easily compiled for different targets.
So no excuses not to write great apps for OpenMoko :)

Monday, 12 May 2008

First day at OpenMoko

Today is my first working day at OpenMoko.

After I met my new coworkers - I will need some time to get used to all the new faces - I started to play with the Neo FreeRunner. So I followed the wiki instructions to flash my cell phone with the last kernel and file system image. It worked fine :-)

Of course, since I am a fan of python and gtk, the second thing I tried was to run a python / gtk / glade application. And the proof that it works with no problem :

Here is a screenshot of glade designer running on my laptop :
And here is the same interface running on the Neo FreeRunner, from a python script :

This is pretty cool. The only problem is that the menu bar looks a little bit too small for a screen touch interface.

Wednesday, 30 April 2008

How to compute "y = a * x + y"

I recently tried to write a numerical computation software that needed to perform a lot of vector operations. My first idea was to use python, and the numpy library.
But then I found that this simple operation :

y = a * x + y

Where x and y are large numpy arrays, and a is a scalar,
was much slower than the same thing coded in C, like this :

void func(float x[N], float y[N], float a) {
for(size_t i = 0; i < N; ++i) {
y[i] += a * x[i];
}
}

The reason is that numpy has to create a temporary object to store the result of the multiplication, and then also create a new object to store the result of the addition. When the size of the arrays becomes large - I was dealing with several Megabytes data arrays - it can make a lot of difference.

But I don't like the C code either, because of the very ugly loop.

So what can I do ? Well, first I could use the C blas library, then the operation is replaced by a function call, like this :

cblas_daxpy(N, a, (double*)x, 1, (double*)y, 1);

But it still looks over complicated.

OK maybe some other languages can be more suitable for this task. What about C++ ? With the boost::ublas library, it looks already much better :

using namespace boost::numeric::ublas;
void func(const vector& x, vector& y, double a) {
y += a * x;
}

Well, almost what I wanted, I could even use template parameters to make the function accepts any kind of input data.

But since at the end I want to be able to call this function from python, C++ is not the easiest choice (beside, I don't like C++ that much). Finally I used a language I really don't like, but that appeared to be the best for this particular task : Fortran90

Fortran90 is already much better than the horrible Fortran77 (That nobody should ever use). and most important, it has vector operations included in the language.

So now my simple operation looks like this :

subroutine func(x, y, a)
implicit none
REAL, dimension(:), intent(in) :: x
REAL, dimension(:), intent(inout) :: y
REAL, intent(in) :: a

y = a * x + y

end subroutine func

(Note : in Fortran90, you DON'T need to start every lines at the 7th character !)

I can call this fortran function from python quite easily, thanks to F2PY, and voilà. Then I can extend my function to perform all sort of numerical operations.

Conclusion : Fortran90 is not as bad as I though it was, combined with python it can even be quite useful. But I still don't like this language, I am looking forward for the D language to have good numerical operations library, as well as python wrapping (it already exists, but there are still a few issues.)