python

Drupal 7 and XMLRPC example

The skinny.. Drupal 7's service call for XMLRPC is now a little different, the only way i got it to work was to use Cookie/SESSID in the headers and not send the session id as an argument (as you did in the past)

So check out my fork here https://github.com/dgtlmoon/python_drupal_services of the drupal 7 python xmlrpc client example

I guess if you want to improve the cookie handling for the sessions you could take some inspiration from http://code.activestate.com/recipes/501148-xmlrpc-serverclient-which-doe...

The fat...

Munin graph your Drupal response times for user logins

Munin is a wonderful tool, it is best explained by the project itself.. Munin is a networked resource monitoring tool that can help analyze resource trends and "what just happened to kill our performance?" problems. It is designed to be very plug and play. A default installation provides a lot of graphs with almost no work.

Pretty print those complex services json replies..

not rocket-science, but something handy for debugging/developing web apps, use this to pretty print and validate the JSON responses

Google appengine handling external services and xpath

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

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

Syndicate content