Flask sessions and unicode_literals

If you had tried to use Flask sessions and got something like that:

File "/usr/lib64/python2.7/site-packages/flask/app.py", line 889, in __call__
  return self.wsgi_app(environ, start_response)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 871, in wsgi_app
  with self.request_context(environ):
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 836, in request_context
  return _RequestContext(self, environ)
File "/usr/lib64/python2.7/site-packages/flask/ctx.py", line 33, in __init__
  self.session = app.open_session(self.request)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 431, in open_session
  secret_key=key)
File "/usr/lib64/python2.7/site-packages/werkzeug/contrib/securecookie.py", line 308, in load_cookie
  return cls.unserialize(data, secret_key)
File "/usr/lib64/python2.7/site-packages/werkzeug/contrib/securecookie.py", line 255, in unserialize
  mac = hmac(secret_key, None, cls.hash_method)
File "/usr/lib64/python2.7/hmac.py", line 133, in new
  return HMAC(key, msg, digestmod)
File "/usr/lib64/python2.7/hmac.py", line 72, in __init__
  self.outer.update(key.translate(trans_5C))

…you might be obscured. Fear not! The reason may lurk in

from __future__ import unicode_literals

Just declare your SECRET_KEY as a bytes object and get happy again!

SECRET_KEY = b'smthverrysekret'

About springs

Spring has came (to northern hemisphere, that is). That is to say, calendar one, and it doesn’t seem true one to me. We count years by sun, so all the seasons of year should be determined by solar events, solstices and equinoxes (equinoces?). Astronomers believe that spring begins on vernal equinox (20 Mar in 2011). But that’s a bit strange to me personally, since that way daytime increases only in winter and spring, and summer and autumn are both time of “darkening”. I’d prefer to consider that equinox (or solstice) are middles of theire seasons, so spring is a quarter of year, when daytime growth is fastest; summer is a quarter of year with the biggest daytime etc. Either way, the first day of real, solar spring is three weeks away from today. Before or after.

Tabs and spaces

That’s quite old and well-known, of course, but I now have to deal with code like that (count the spaces!):

class c_integral_type(c_type):
    def __init__(self, name, format, min, max):
       c_type.__init__(self, name, struct.calcsize(format))

or like that (tabstop is obviously expected to be 8 or 12):

def genMemList( self, members ):              # indented with 4 spaces
self.append( '\tdef members(self):\n' )  # indented with one <Tab>

So…

image0


Oh noes! That’s what I had to deal with:

  % pylint buratino
   .......
Your code has been rated at -1.79/10

My code isn’t perfect, I’d be first to agree, but it scores 7/10, and it’s positive 7, mind you! Bloody hell. So, back to work then

Eselect’ing xorg.conf

While experimenting with nvidia/nouveau I’ve decided to write module for app-admin/eselect to eselect required /etc/X11/xorg.conf from list. So here it is. Get it, use it, report bugs in it (here or, better, open issue on GoogleCode). Ebuilds are in rion overlay.

psycopg2 returns tuples of string

Today I was fiddling with PostgreSQL (v8.3, if anyone is interested) database from Python using famous psycopg2, but suddenly got stuck in unexpected place. Had switched to bpython shell I saw the following:

>>> cur.execute('''SELECT (parent, level) FROM "MsgElems" WHERE msg_id=1;''')

>>> cur.fetchone()

('(0,1)',)

SELECT returned tuple of string, one string. Now that’s what I call unexpected… I didn’t find anything neither in Python DBAPI PEP nor psycopg docs, but after some time a bright idea came:

>>> cur.execute('''SELECT parent, level FROM "MsgElems" WHERE msg_id=1;''')

>>> cur.fetchone()

(0, 1)

Watch out. Meanwhile I’ll try to find out, what triggers such weird behaviour, and if it is weird at all.

Change the code and see the result

An interesting idea came to me several days ago. I have to write C++ code generator (sourcing data from SQL database), so I installed Jinja2, sharpened text editors and then asked myself: „Wouldn’t it be beautiful to see the result just as I type Python code, portion by portion? There’s a use for two monitors: the first one displays coding, the second one keeps freshly generated result always ready for review.“

Read more…

Philips Xenium E100: review

Recently my old Siemens C60 has died, so I had to buy new cell phone. Since I’m not phone geek at all, I’ve decided to buy the simplest (just to call and write SMS) of Philips Xenium series (exceptional battery lifetime). So, here it is, my Philips Xenium E100.

Read more…

Gentoo: DRM, Radeon/RadeonHD, ATI R6xx/R7xx

Here’s brief summary of one hour on #radeon IRC channel:

  • old DRM modules don’t create /dev/dri/* for ATI R6xx/R7xx-cards, that’s normal;

  • kernel (up to, but not incl. 2.6.30) DRM aren’t that old, but neither aren’t new enough to support the cards in question;

  • moreover, DRM mainline lacks their support too: therefore x11-base/x11-drm doesn’t suit;

  • in order to build DRM modules for R6xx/R7xx, one should git-clone source from specific git branch (see Xorg Wiki)

  • use ebuild from x11 overlay with DRM_LIVE_BRANCH="r6xx-r7xx-support" added to your /etc/make.conf


Added later: described technique has no use, if you’re running kernel 2.6.30 and later. Just compile DRM-modules and have fun.