Fixing upload of files with non-ASCII names in apache/mod_wsgi

One of the first things I do when configure new server for django/mod_wsgi/apache project is checking in which locale apache is started. I usually work with CentOS installations, where apache starts with “HTTPD_LANG=C” and that results in strange 500 errors(or Unicode errors deep in standart library, which is even more confusing) when uploading files with non-ASCII characters in name. This can be fixed by editing /etc/sysconfig/httpd file, uncommenting HTTPD_LANG variable and setting it to something like en_EN.UTF-8, after service httpd restart it’s possible to upload any files. (apachectrl restart wouldn’t work, you need to use service restart)


Appcelerator Titanium – grey “run emulator” tab

Appcelerator Titanium is terrible when it comes to error reporting – it’s quite confusing when you have completely empty tab and no errors. There are two(at least) possible cases – wrong or empty build directory. And both problems can be fixed by deleting build directory, recreating it again and creating iphone and android subdirs. After restart of Titanium it should show Run emulator tab correctly.


To remember: Insert in osx mc

I was surprised that “insert” doesn’t work under osx in mc, luckily it’s possible to use “Ctrl+T” to select entries in listing.


haml and flask

After trying Pyramid for a bit, I’ve decided that it’s not exactly what I am looking for so I am playing with Flask right now.

Installing jinja-hamlish into Flask is a bit more tricky but still takes less than a minute

pip install git+http://github.com/Pitmairen/hamlish-jinja

and inside your app

from flask import Flask, render_template
from werkzeug import ImmutableDict
 
class FlaskWithHamlish(Flask):
    jinja_options = ImmutableDict(
        extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_', 'hamlish_jinja.HamlishExtension']
    )
 
app = FlaskWithHamlish(__name__)
app.jinja_env.hamlish_mode = 'indented' # if you want to set hamlish settings

haml and pyramid

Writing html sucks – ending tags reminds me of using curly braces which is not a good thing from python developer’s point of view. Luckily things can be made way better by using HAML and in case of python and Jinja2 by hamlish-jinja.

Using hamlish with jinja2 in Pyramid is very simple:

pip install hamlish-jinja (don’t do it, pypi version is outdated)
pip install git+http://github.com/Pitmairen/hamlish-jinja

in your development.ini

[app:test]
...
jinja2.extensions = hamlish_jinja.HamlishExtension

and inside __init__.py

config.add_renderer('.haml', renderer_factory)

and it’s done, from this point you can add templates file with “.haml” extension and Pyramid will render it as HTML.

As bonus:

  • PyCharm has HAML support
  • hamlish-jinja is a preprocessor, which means it will parse every template only once and will not slow down ypur application when generating pages.

Ngnix on CentOS 5.5

Big Warning: nginx from epel and CentALT repos use aio builds which will not run on kernels prior to 2.6.18-194.8.1 with errors like this

[emerg] 11665#0: eventfd() failed (38: Function not implemented)

you need to upgrade kernel or build ngnix without aio suport ( without “–with-file-aio” ./configure switch )


XPath in Firebug

Learned a good trick today – Firebug has special function $x which can be used to get element by XPath, like

$x("body");


XPath and tbody

Firebug has one great feature that Chrome developer tools lack – ability to copy XPath of inspected element. It’s especially useful when writing HTML grabbers using lxml, but it has one gotcha – when inspecting a table Firefox will recreate full DOM structure using tbody tag, but most of the pages in real world don’t have it.  So you need to check actual page source and remove tbody from XPath if needed.


Debugging exceptions deep inside Ext.js

It’s a bit confusing to see errors like

Uncaught TypeError: Cannot call method 'getColumnCount' of undefined

especially when stack-trace is full of ext.js files and has only one call from your files somewhere in top level gui creation call, like creating of Viewport. Fortunately there is a simple solution if you use Chrome Developer Tools – setting breakpoint at the beginning of the function with error and triggering it by reloading page or re-running faulty part of code will allow to print “this” in scope where error is going to happen.

By examining result you can check which element of your gui is falling.


Python enumerate trick

I write lots of code which iterates over big lists where I want to keep track on current item being processes and I was using

index = 0
for item in items:
    print index, item
    index += 1

until I’ve found a much better approach

for index, item in enumerate(items):
    print index, item

Additionally Progressbar is a nice module for showing progress bars in terminal, it even can calculate ETA without any additional work!