This blog is a dumping-ground for research, thoughts and developments. dgtlmoon@gmail.com
skype: dgtlmoon
not rocket-science, but something handy for debugging/developing web apps, use this to pretty print and validate the JSON responses
Two really frustrating things that one needs to adjust to when writing python applications for google's appengine, which for me I hit straight away because I was writing a 'mashup' type webapp which involves consuming services and processing the data into something new and hopefully fanstastic
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!
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 ?
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! 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]
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])
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
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.
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.