python
GTK Recently Used updated with inotify!
Heres a cute hack, an easy way to make any file you open/change from the command line appear in your GTK file widget's "Recently Used" list, it uses python's pyinotify package available in apt, and pythons python-gtk2 package.
Something that's very handy, for example when you are editing something on the command line and attaching to an email and need to browse to the location, or when you've wget'ed a file and need to open it from a gnome application, the list goes on!
python unicode escaping to xmlescape
If you get errors like..
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 48: ordinal not in range(128)
try and escape them to something supported locally..
(from http://www.amk.ca/python/howto/unicode)
particularly
u.encode('ascii', 'xmlcharrefreplace')
>>> u = unichr(40960) + u'abcd' + unichr(1972)
>>> u.encode('utf-8')
'\xea\x80\x80abcd\xde\xb4'
>>> u.encode('ascii')
Traceback (most recent call last):
File "", line 1, in ?
python regex substitution with backreferences, don't forget to escape!
amount = re.sub(r'\.(\d)0$', '.\1', amount)
wont work but...
amount = re.sub(r'\.(\d)0$', '.\\1', amount)
will!
Python libxml2 not quite threadsafe, use lxml instead for xpathing!
Python libxml2 not quite threadsafe, use lxml instead for xpathing! although lxml is just a wrapper for the same libxml2 library that python's libxml2 module (python-libxml2) uses, it will act very strange when you're calling parseDoc in a loop in your threads.
text() is a special xpath function in lxml
import lxml.etree as etree
f = StringIO.StringIO('<hours>anytime</hours>')
tree = etree.parse(f)
res = tree.xpath('//hours/text()')
print res[0]
LXML removing an element that you find
I've been using LXML (http://codespeak.net/lxml/) for a new project, Kinda tricky, not quite obvious if you want to drop an element and its children that you find, but the rest of the lxml library is pretty obvious after a little while
so you may have something like
<food>
<taste>sour</taste>
</food>
so you would..
type="sour"
elem = xml.xpath('//food[taste="%s"]' %(child))
elem[0].getparent().remove(elem[0])
ODBC with Python (pyodbc) to MS SQL (SQL Server - Microsoft)
Was having trouble figuring out pyodbc http://code.google.com/p/pyodbc/ with my ODBC configuration to talk to a MS SQL or "Sequel" as some people would have it
You'll need a couple of packages installed
tdsodbc - ODBC driver for connecting to MS SQL and Sybase SQL servers
odbcinst1debian1 - Support library for accessing odbc ini files
odbcinst - Helper program for accessing odbc ini files
so just
apt-get install tdsodbc odbcinst1debian1 odbcinst
Example/tutorial of python threading handling a queue of tasks
A handy recipe I like to use, utilising python queue's and threads (via threading module), create a bunch of threads and process them with what is in the queue.
Uses a global var to know how long it's been since the last thread executed anything of use to know when to start shutting down the worker threads.
Depending on resources you can build a pool of threads to eat its way thru the jobs in your queue, this example loads 5000 'jobs' into the queue with 50 threads polling in non-blocking mode for a job to perform off the queue stack.
Pre-processing your .QIF for GnuCash with python - use regex and a flat DB to add categories to transactions
GnuCash is an excellent linux based accounting package, it is modelled very closely to traditional two-column accounting methods but has lots of functionality like generating invoices, tracking clients and all the other things you'de expect from a package to run your finances.
Listen to /dev/dsp and record to MP3 when there is some sound to record
Something I wrote for a small project, initially designed to listen my radio scanner via /dev/dsp and record when it hears something, this uses python and some threads to launch instances of 'lame' encoder and then insert them into MySQL.
The app has a threshold for incomming sound levels and starts recording when the threshold is reached, there is also a setting for time-distance between hearing things so you dont end up a billion little mp3s because the audio is dropping out, just a nice long mp3.
