Archive for the ‘python’ Category

Django model history with django-reversion

Tuesday, October 13th, 2009

I’m currently working on a django project and I needed to track all changes to a model and provide a log showing what has changed (like a wiki).

I’ve found three extensions/libs that apparently could do the job for me (fullhistory, django-history and django-reversion) so I decided to test them, but because of the lack of structure/documentation of the other two I tried just the last one.

django-reversion

(http://code.google.com/p/django-reversion/)

It has a good documentation, integration with the admin and the most important for me a low level api, as I am not using the admin app for this project.

The installation went pretty well (svn co http://django-reversion.googlecode.com/svn/tags/1.1.2/src/reversion to your PYTHONPATH, add the reversion app to your INSTALLED_APPS in settings.py and the usual python manage.py syncdb).

To track the changes of a model you must register it with the reversion framework (importing reversion and calling reversion.register(YourModel)).

Reversion provides some ways for creating revisions of your model and the docs recommend that you choose one of them to use in your project. I chose the MiddleWare method because it seemed the better option for our project, but you should look the reversion documentation and decide which one is the best for you.

After registering the model and installing the MiddleWare no more changes are needed, whenever I save a model a new revision is created on the database. Another cool feature is the follow argument when registering a model. That way you can specify a ForeingKey/ManyToMany field to follow. This means that when you save the model a new revision is created with all “followed” fields in its current state. It’s very useful. See the docs for an example.

The only thing that’s not so clear for me is: what happens if I delete a field from a model that’s registered within reversion? Can you still restore its versions?

Transparência HackDay

Friday, October 2nd, 2009

“Two days for hacking the Brazilian politics”

This weekend (October 3rd and 4th) will happen the first “Transparência HackDay” (“Transparency HackDay” in english), a free and open hacking event with focus on bringing together hackers, activists, managers and people with ideas to make government information/public data more accessible for everyone using and writing web tools.

I’m attending and one of my ideas is to hack CET (Companhia de Engenharia de Tráfego / Traffic Engineering Company) data and make them available. The idea is to improve a crawler I wrote (in python ;) ) that downloads and stores data about the traffic in São Paulo every 30 minutes from the “traffic now” CET website (as they don’t store it [or at least don't provide anything]).

More information about the HackDay (in portuguese):
http://www.slideshare.net/esferamobi/transparencia-hackday-proposta

São Paulo Python User Group – May Meeting

Saturday, May 16th, 2009

Every month the GruPy-SP members get together to discuss, chat and/or code.

This month we will meet to watch/present some lightning talks and to talk about our participation at the Fórum Internacional de Software Livre (FISL 10).

The meeting will be hold at the SP HackLab, May 23. More details (in pt-br): GruPy-SP wiki.

See you there :-)

Nokia Code Camp São Paulo

Monday, October 27th, 2008

Last Friday (Oct 25) I’ve attended to the Forum Nokia Code Camp here in São Paulo. I must admit that the main motivation to go was that the place was very near my house :-P

After finding out that rbp, lhonda and Luiz Irber (friends from our local Python User Group) were attending too I thought that it’d be a nice event. At least we could talk about GruPy-SP plans…

Anyway, I went to the hotel and after watching the three presentations (speaking about Nokia Serie60, Python and mobile development) the fun really started. 4 hours to develop the coolest mobile application in the world :-P

The prize was two E71 (the “new” Nokia Smartphone) for the “winner” application, so we decided to code in pairs. Me and rbp, after talking about our ideas, decided to write a musical instrument using the N95 accelerometer.

The first two hours of “development” actually we spent trying to find a proper way to transfer files to our cellphones (the S60 SDK is Windows only…). Then, with the “bluetooth easiest method” for transferring the app working, we started trying to find out how to interpret the accelerometer data. It worked as expected. Then we needed to play a sound (as it was a musical instrument). Easy, Python for S60 is very intuitive, the API is very High Level. Looking to the reference guide there was something like:

from audio import Sound
Sound.open(“file.mp3″).play()

Just as I expected… except that there wasn’t any sound coming from the freakin’ phone’s speaker. There was forty minutes left to the end of the competition and all we had was a soundless musical instrument, not very useful.

Ok, plan B. I started looking to some old code in my N95 and found a bluetooth webcam prototype. Rbp had an idea of making the webcam “crash” with a movement (using the accelerometer). And then we made it.

It’s a funny application and, as I said to rbp, as programmers we are great comedians. We presented the webcam in the stage and people seemed to like us (the app or the comedians? I really don’t know :-) )

Well, me and rbp won a Nokia E71 (each), it’s a very nice (and THIN) smartphone, I don’t like phones with qwerty keyboards, but this one is really cool (thin, small and almost comfortable to type).

10/27/2008

Thanks Forum Nokia! :-)

DjangoCon day two

Monday, September 8th, 2008

Almost sleeping, but there’s still a bit of caffeine in my body to write this post.

Some notes on day two:

  • In São Paulo we had two lighting talks before the “official” conference. João (JS) talked about a little script he wrote that uses PyGame to display text from a .txt file in a presentation format. Rbp explained a bit about 2to3 (the Python 2.x [actually x equals 6] to Python 3 conversion tool) and showed some examples.
  • First talk. Mark Ramm had some good points. Specially, IMO, about making Django more modular, which means that each part of it doesn’t depend on other parts of the framework. As an example, you can choose to use the Django ORM without using Django. But, please, don’t make it a lot of little packages that you need to grab together to start using the framework. One of the nice things about Django is that it’s simple to start using it (install one package and then django-admin.py startproject)
  • Official lighting talks. As always, a great moment on any Python conference. Lots of curious and interesting stuff (and other stuff not so interesting… or not interesting at all :-P [for me, obviously]). Funny moment: People, don’t use GMail over HTTP, there IS HTTPS support :-)
  • Schema evolution, three options: DMigration, South and Django Evolution. There are a lot of doubts about each one and I think I need to test all of them to choose one (and hope that they start collaborating to each other and make something really good). But it’s great to see that there is people working on that.
  • Last talk was Django’s Future by, obviously, Jacob and Adrian. It was more like a chat between them. Some good ideas and discussions. Then they received questions/requests/suggestions from the public. Things like, dropping old python versions support, python 3 support, documentation, debugging tools, etc. I think that some suggestions/requests were really nice and I hope that Django developers (and the community) take them seriously. This was a great opportunity for developers to receive feedback from the community (users).

That’s it for 2008. Again, I’d like to thank Rodolpho and Google for making it possible to attend to DjangoCon from São Paulo. Congratulations to DjangoCon organizers and to the Django community!

And, of course, thanks GruPy-SP people :-)

Pictures: Rbp’s DjangoCon set @ Flickr

DjangoCon @ Google (SP)

It was 11p.m. Almost sleeping in this blue big puff :-)

DjangoCon first day

Sunday, September 7th, 2008

I’m attending to DjangoCon from São Paulo Google’s Office.

Some notes on the first day of event:

  • I expected more news from GvR on his talk about GAE (nothing really new).
  • David Cramer talk about High Performance was kinda heavy (but good things learned)
  • The history behind Django is hilarious (State of Django by Jacob and Adrian)
  • It seems Django is getting bigger (contributors) and better (features). Good!
  • Really good talk by Malcolm Tredinnick about the Django ORM
  • GeoDjango seems very interesting, but it’s not for me (at least now…)
  • Cal Henderson made good “feature requests” and “bug reports” live, the unreadable SQL generated by Django scares me a bit… but not that much

In general it was great. I can’t wait for the lighting talks tomorrow (probably the best moment on any Python conference :-) ).

Oh, the snacks @ Google (Sao Paulo) are awesome!

Modifying Django NewForms-Admin views

Friday, September 5th, 2008

Playing with the new Django features, specially with the NewForms-Admin, I was looking for a way to change the add view behavior for some models.

It’s really easy and obvious (it’s always easy and obvious with Python and Django).

First, you’ll need the admin.py file in your app directory.


from django.contrib import admin
from myproject.myapp.models import MyModel

class MyModelAdmin(admin.ModelAdmin):
def add_view(self, request):
if request.method == 'POST':
# do whatever you want
# remember, POSTing means that someone entered data.
return admin.ModelAdmin.add_view(self, request)

admin.site.register(MyModel, MyModelAdmin)

The add_view method is called when you try to add an entry using the admin. There are other interesting methods you should look too, for customizing the admin behavior (change_view, delete_view, etc).

Another way to customize things is to write your own ModelForm, but it’s beyond the scope of this post :)

DjangoCon in São Paulo

Thursday, September 4th, 2008

I’m not crazy at all. (yet)

Thanks to Google (I think specially to Rodolpho) DjangoCon will be transmitted to Google’s Office in São Paulo while the conference is held in Mountain View.

Cool :)

I’m definitively going! So, Saturday and Sunday (September 6th and 7th) @ Google’s Office, see you there!

Translating pypy-c with 2.5 support

Thursday, August 28th, 2008

Last week I did some fixes to PyPy 2.5-features branch because the translator was crashing. That’s because some of the code I wrote in the last four months were not RPython.

The changes were mostly to help the annotator (with assertions). Now you can have a pypy-c with full 2.5 support.

There are some bugs so stdlib may not be fully working. I’m working on that.


svn co http://codespeak.net/svn/pypy/branch/2.5-features/ pypy-2.5-features
cd pypy-2.5-features/pypy/translator/goal/
python translate.py targetpypystandalone --allworkingmodules
# wait........
./pypy-c

Please, read PyPy’s documentation on translating and if you find any problem while translating let me know. Note that this docs are for the trunk so some of the options may not be available in 2.5-features branch.

I wrote this post last week and it was in my drafts. Actually I fixed some other bugs (one related to PEP 352) and it seems that my changes broke the translator again. Talking in #pypy Armin said that this is probably a bug in the annotator. I don’t know much about the translation process or about the annotator… I guess it’s time to start looking at it :-)

PyConBrasil 2008

Saturday, August 9th, 2008

This year’s PyConBrasil will be held at Universidade Veiga de Almeida, Rio de Janeiro – RJ. I’m definitely going! Last year’s PyConBrasil was great.

So, if you are in Brazil or planning come near September 18th, 19th and 20th, don’t miss the chance to meet the awesome guys from Python Brasil community :-)