Python, Firefox programming and Irish Whiskey.

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.

Wednesday, April 28, 2010

pylons in appengine

Hello again, after more than a month. Sorry you had to wait. So I have got pylons in google appengine up and running. Haven't done anything yet, just an "empty" application. Anyway, I had to compile my own python, as OpenSUSE 11.2 already contains version 2.6 and doesn't seem to have 2.5 packages in my current set of repositories. After I compiled python for the first time (./configure ; make ; sudo make install) and run python2.5, I saw that I could not use commandline properly, so I installed readline-devel. Then I compiled again, tried a bit of the appengine-monkey guide, found out that something wasn't compiled in, installed another devel package and went again. The packages I had to install were:
  • readline-devel
  • zlib-devel
  • openssl-devel
  • sqlite3-devel
Then I successfully run the bloody thing. After some mistake on my side I decided to go for Pylons-0.9.7 instead of the fresh 1.0-prerelease and I installed an older webob as one of the comments on appengine-monkey wiki advices. That's about it for now. I will keep you posted with updates.

Sunday, March 14, 2010

word count for chrome

Just a quick note: The approach described in word count and word count II seems to be working well in Google Chrome too.

Sunday, March 7, 2010

localized html in a chrome extension

Google Chrome implements a function to get a localized version of a string. Using it to update HTML can be rather tedious if we use the most naive approach I can think of:

Let's do something smarter: We shall mark all tags that contain translatable strings with a class, then we will select all tags of that class, simply take their innerText as the message id, get the message and replace innerText. Done.

Done.

Thursday, March 4, 2010

SEO Profesional Toolbar for Google Chrome

So it's finally here. I have been working on the Google Chrome port of our "famous" SEO Profesional Toolbar for some time and the very first release has been made yesterday.

You can get it here. I will appreciate any comments. Please note that it isn't really finished yet. Developers can look forward to reading some related posts.

Friday, February 26, 2010

better template

Great! I have found a template main part of which is wider than some 30% of the page! Wow! That's my second post today.

Wednesday, February 24, 2010

highlight things in a firefox extension

If you did some highlighting in a web application you would have to manipulate DOM to insert tags that would change background color (or style in general) of the area being highlighted. It is not only slow but also rather complicated and limited. Firefox offers a stronger tool, which is used for example for highlighting searched strings. Here we will look into some details.

We will start with document.createRange. This is not firefox specific yet. This is a pure DOM and should be pretty cross-browser. (I haven't tried it in MSIE8 but according to quirksmode's Introduction to Range it does not work in MSIE6/7. Honestly, I haven't tried in anything else than FF3+). Anyway, we have got quite a set of functions for manipulating ranges, and honestly they are not too easy to work with. Try this in your Firebug:

For description of functions see DOM/range @ developer.mozilla.org. So, we have got a range or more and we would like to highlight them. Here, Firefox comes to our help. We need to obtain a selection controller for the window, add the ranges to desired type of selection and tell the controller to highlight them. There are a few things to remember:

  • The selection controller can contain something. We should clear it first.
  • There are a few types of selection, like SELECTION_FIND and SELECTION_NORMAL.
  • You must do all the stuff shown below for all frames in your document separately. This will not be shown here but you can look at SEO Profesional Toolbar source code, file seopsidebar.js
  • Remember that some windows do not contain DOM. In that case this method will fail

Just for those curious: In SEO Profesional Toolbar we offer a list of something (links, found texts) in a sidebar and hightlight all these entities using SELECTION_FIND and when one of them is clicked in the sidebar, we will highlight it using SELECTION_NORMAL and scroll to it with controller.scrollSelectionIntoView(Ci.nsISelectionController.SELECTION_NORMAL, Ci.nsISelectionController.SELECTION_ANCHOR_REGION, false)

The best thing at the end: I have put some examples into my testbed firefox extension, so fire-up your browser, install testbed, look at the source code (overlay.js), sit back and enjoy! Do not forget to uninstall or disabled testbed when you are done.

Monday, January 18, 2010

The most annoying thing about javascript

First some background:

Would you believe that the beast above is a valid javascript construct? Those two commas are translated into , undefined , , so the a.length==6 and a[1]==undefined. I would really like to see an example of a code where this construct is helpful. But that's not something we cannot live with. Here comes the winner: The most annoying thing about javascript:

Here we have hit into one of those browser incompatibilities. Firefox, Google Chrome and Opera 9.63 say, quite logically, that len(a)==1. MSIE 7 on the other hand things the array is 2 elements long. I suspect MSIE is closer to ECMA script then others because being struct by the fact that a variation of this example worked in Firefox but didn't work in MSIE I read relevant part of EMCA script standard.

Why is it annoying? In Python I happily jumped at the possibility to generate lists with a comma at the end. How natural, no special handling of the last element. In languages that do not support this I at least get a syntax error. Not in javascript. If I want my code to run in MSIE too (and this stupid comma should be no reason not to) I have to remember this little thing and write more complex code. The worst thing is I cannot even bitch about stupid MSIE, because I believe MSIE has got it right according to standard. Illogical, stupid, but correct.

What is your candidate for the most annoying thing about javascript?