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.

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?

Sunday, December 27, 2009

favicon service in firefox

That's the places thing everybody talked so much about when announcing firefox 3. What we actually see in bookmarks or history is just a top of the iceberg. Had I read the places documentation before I started working on seoprofesional toolbar, I would probably just use the places tagging system or something to select the sites to remember...

Anyway, at the first attempt I just used favicon service as follows:

The trouble was, the icon was really the favicon from the site. The original size (whatever it was) and possibly animated. That I didn't like. So the second attempt looked like this:

The resulting icon is 16x16 and not animated. Only remember to put try...catch in the right places because you must close the container at the end (result.root.containerOpen = false)

(The last example was updated on the 7th of Feb 2010 to incorporate changes in FF3.6)

Sunday, December 13, 2009

flot and flotr

I promised a few words about flot. I thought a lot about it and instead of writing how good and easy to use it is, I will just point you to it. I must also mention flotr, which is a similar library, but uses prototype instead of jquery. Now the links:

2 small notes: I like flotr better. It's output seems to me to be a bit more polished. But I picked flot for seoprofesional toolbar, because flotr didn't like interrupted series (a line from 1 to 3, then nothing from 3 to 4, and then a line from 4 to 5 again). On the other hand, flotr does support value labels, for which I had to write my own plugin.

Thursday, December 10, 2009

ff3sudoku now for google chrome

I have just finished and released ff3sudoku for google chrome. It should be roughly the same as the good old ff3sudoku for firefox but thanks to the firefox system of approval of add-ons by editors you have to wait for the firefox version even though I released it about a week ago, while you can have it instantly for chrome. Good work google!

Now, if there is anybody actually reading this who has installed the extension, please give me your feedback. The extension has it's rough edges and I need to know whether I should be polishing them or not...

firefox personas disappointment

I finally got down to getting personas installed on my machine. Honestly, I do not understand what the hype was about. It's nice, alright, but is that all? Just an image to display on top of the window and another one to display on the bottom? I sort of expected something like skins we all know from X-Window System widget sets, where at least all window backgrounds are changed.

Yet, skinning the red panda opens one interesting possibility, that as far as I know hasn't been researched much yet. How about making your web-page follow the mood of your browser? Or the other way round, your browser, following the mood of your webpage? As far as I can say, none of these are implemented in personas. I expect the second one might be considered a security threat, because if I understand correctly the header and the footer image can actually be html with some javascript in it. Or am I wrong?

Anyway, I can see personas firing a DOM event to a freshly loaded web-page, telling the page what the currently selected persona is, allowing the page to react. The only problem is that there are too many personas for a web-page to implement. Perhaps there might be a way how to allow a web-page use a local resource - a persona-related background image. Web-page will thus indirectly access the viewer's hard-drive. Here we are back to security threats.


(...might be continued...)

Thursday, December 3, 2009

ff3sudoku 1.2.0

I have just released a new version of the sudoku firefox add-on. It finally let's you remove the tooltips some find so annoying. This version hasn't been approved by editors yet, so it is still marked experimental. You must go directly here: https://addons.mozilla.org/en-US/firefox/addons/versions/8036#version-1.2.0 to install it.

I have got some ideas what to do next, but to be honest, I would like to see some interest. I mean, if everybody is pretty much satisfied with the status of sudoku, there is probably no need for me to waste time on it.

Meanwhile, I will look a bit more into turning the bloody thing into a Google Chrome extension.

Tuesday, November 17, 2009

extending pageinfo

In our SEOProfesional Toolbar firefox extension I considered, how I should present some web-page related information to the user. Of course the easiest but quite stupid way to do it would be to make my own information box with the requested information. But in principle, the info we wanted to show was something about the page, therefore logically belonging to the Page Info dialog. I did not find much information on developer.mozilla.org, so I dived into the source code. Let's start with chrome.manifest of our testbed add-on: which simply says that the browser's pageInfo.xul should be overlayed with our pageInfoOverlay.xul. The overlay should look like this: The important parts are:
  • The radiogroup overlay with an item with id somethingTab and the oncommand="showTab('something');"
  • The deck overlay with a vbox called again somethingPanel
Now you should see your page in the pageinfo. The real fun hasn't started yet. We need to add some functionality. You have probably noticed the link to pageinfo.js in our xul. Until now we expected it was empty. Let's start filling it with stuff. If you look into chrome://browser/content/pageinfo/pageInfo.js you will see that Firefox developers have helped us with building a framework for extending page info. There is a bunch of callbacks to help us set the contents of our page. Following are javascript arrays of functions to be called for certain events.
  • onLoadRegistry
  • onResetRegistry
  • onProcessFrame
  • onProcessElement
  • onFinished
  • onUnloadRegistry
We will just look at onLoadRegistry and onProcessFrame here as they are the only 2 I have used. onLoadRegistry functions will be called whenever page info is displayed. That's the good place to decide whether we want to display our page or not. The code of our pageinfo.js could look like this: That will hide our page for pages retrieved through https. The second one, onProcessFrame, gets all the frames the page consists of. If we want to enumerate them, we can do something like this: 2 last pieces of the puzzle are missing. The first one: how to assign a headline image for our page. Easy! Through css. You have noticed link to chrome://testbed/skin/overlay.css in our xul. Those 2 images should be 32x32px. And the last thing: what if we also want to invoke the page info dialog from our UI and switch directly to our page? Again, that's something Firefox developers thought about. Very basic: Not ideal though. There is a possibility, that the page info for this page has already been opened. Therefore it is better to look and in such case, focus the existing window: The best news at the end: I have created a test-bed for my experiments. You can install this add-on and see pretty much everything I have been writing about today...