petrsstuff

Python, Firefox programming and Irish Whiskey.

Friday, September 16, 2011

Get the week of the year in javascript

For those looking for that particular function and already using jqueryui: You already have it in datepicker widget. Just call $.datepicker.iso8601Week(date_object).

The code is simple (taken from jqueryui, so either MIT or GPL license):


I just have to warn you against the attempt to extend the built-in Date type with a new method, as some suggest. You will find enough explanations around on why it is a bad idea.

Tuesday, May 3, 2011

ff3sudoku for firefox 4

Yesterday I released a new version of my sudoku firefox extension compatible with firefox 4. Before it gets reviewed, you can install it from here: https://addons.mozilla.org/en-US/firefox/addon/sudoku/versions/ There have been no changes, I only made it compatible with firefox 4.

Monday, December 27, 2010

Wrong auth logic

I generally use authkit in my pylons projects for authentication and authorization. I have recently realized that the logic I use is wrong. The thing is that whenever I encounter a user that is authenticated but not authorized, I redirect to a login screen, practically without a word of explanation. Now, that's wrong. I should be showing the login screen only when the user is not authenticated, otherwise I should only display an explanatory message. Now, why am I writing this? Why...? Why? Perhaps not to forget and implement it properly in my next project, now with repoze.who though.

Thursday, September 2, 2010

seoprofesional toolbar beta

Are you interested in test-driving a new seoprofesional toolbar for firefox, now compatible with FF4? Drop me a note to petrb.feedback+seop110beta@gmail.com.

SQLAlchemy Custom Types

SQLAlchemy is just ingenious. I must admit I haven't worked with any other ORM before, yet SQLAlchemy is more than that. I have been working with it's query language for years (4-5) and just recently I started to tuck into orm. So easy to use and so comprehensive.

If you want to do something related to your database, with SQLAlchemy you probably can. I, for example, need to "modify" a date-time value read from Oracle database backing our ERP. I suspect somebody didn't bother to set-up the timezone information properly. So all I had to do was to subclass types.TypeDecorator and override member function process_result_value to do some magic about my datetime values. That was it (I only read the data so I didn't care about converting back.)

Monday, July 12, 2010

pylons, sqlalchemy, pickles

My blog has got Python in the title and yet there hasn't been a serious Python related post. You might have guessed I use Pylons because I tried to run Pylons under Google App Engine. I abandoned these efforts ever since and decided to use the setup I am used to for my next projects. Currently I use quite traditional:

  • python 2.5 or 2.6
  • pylons, currently 0.9.7 because I started with version 0.9.3 or 0.9.4 and it would be to much work to get rid of webhelpers.rails
  • sqlalchemy 0.5 (with MySQL)
  • authkit
  • formencode
  • prototype, because it used to be a part of webhelpers
  • simile timeline, flotr and a bunch of other javascript tools
  • reportlab for pdf generation

For new projects I will move on to:

  • pylons 1.0
  • sqlalchemy 0.6 even though it is not officially supported in pylons
  • not sure about an auth... middleware, perhaps repoze, perhaps something else
  • not sure about formencode but probably yes
  • jquery

I am experimenting with some pickling and unpickling. In one project I am saving pickled data directly into the database. Of course I don't expect I will be able to use the pickled data as database keys or even search in them. The keys will stay in their own native sql colunms. The pickled section will just have some additional info in it. Using sqlalchemy this is rather easy to do. I defined my model declaratively like this>: And for working with unpickled details: You cannot do: If you look at the declaration, you will probably realize why.

In another project I just pickle an object and send it to a client through network. There I unpickle it and use it. Strange things were hapenning. When I have both the server and the client on the same maching it worked perfectly. But when I moved the server elsewhere, it stopped working. It took me a few hours before I realized that on the server I had old version of the module containing the class for the pickled object, which was an old-style class. Of course the unpickling didn't work then.

Saturday, May 22, 2010

facebook :-(

I am on a facebook too. Nothing to board about though. I just wanted to let you readers know I have just created a group named I won't join every silly facebook group I am invited into. Still accepting members. Feel free to join. If anybody at all reads this blog.