How to retrieve information about Python errors in a C extension
Python posted 6 months ago by christian
1 result = PyEval_CallObject(tmp_callback, args); 2 // result == NULL means an error occured 3 if (PyErr_Occurred()) { 4 PyObject* ptype; 5 PyObject* pvalue; 6 PyObject* ptraceback; 7 PyErr_Fetch(&ptype, &pvalue, &ptraceback); 8 printf("Error occurred on line: %d", ((PyTracebackObject*)ptraceback)->tb_lineno); 9 // Restore exception instead of disposing of it 10 PyErr_Restore(ptype, pvalue, ptraceback); 11 PyErr_Print(); 12 13 Py_XDECREF(ptype); 14 Py_XDECREF(pvalue); 15 Py_XDECREF(ptraceback); 16 }
How to use a Python decorator wrapper to get a reference to the calling class instance
Python posted 6 months ago by christian
1 def requires_authentication(method): 2 """ 3 self points to a SheisseController instance instead of the decorator function. 4 """ 5 def wrapper(self, *args, **kwargs): 6 if self._requires_authentication == True and self._authenticated == False: 7 return response('403 Forbidden or whatever') 8 9 return method(self, *args, **kwargs) 10 return wrapper 11 12 class SheisseController: 13 @requires_authentication 14 def index(self): 15
How to parse XML with Python's built-in ElementTree parser
Python posted 7 months ago by christian
1 from xml.etree.ElementTree import fromstring, tostring 2 3 namespace = 'https://xxx.com/xxx' 4 element = fromstring(xml) 5 6 device = element.find('.//{%s}Device' % namespace) 7 detail = device.find('.//{%s}Details' % namespace) 8 series = device.findall('.//{%s}Series' % namespace)
Watch out for namespaces…
How to use Python's simplejson to read and write JSON data
Python posted 8 months ago by christian
First you need to install simplejson:
1 easy_install simplejson
Now you can dump data to JSON:
1 import simplejson as json 2 3 class Something: 4 5 def __init__(self): 6 self.test = "test" 7 8 def to_json(self): 9 return json.dumps(self.__dict__)
Or if you have complex objects:
1 import simplejson as json 2 class Something: 3 4 def __init__(self): 5 self.test = [Other('a', 'b'), Other('a', 'c')] 6 7 def to_json(self): 8 return json.dumps([p.__dict__ for p in self.devices])
Fixing "the fastcgi-backend /usr/bin/python2.5 app.py failed to start:"
Lighttpd posted 8 months ago by christian
If you’re getting an error similar to this one on Ubuntu:
1 $ 2010-01-07 11:35:20: (log.c.97) server started 2 2010-01-07 11:35:20: (mod_fastcgi.c.1051) the fastcgi-backend /usr/bin/python2.5 /var/www/xxx/app.py failed to start: 3 2010-01-07 11:35:20: (mod_fastcgi.c.1055) child exited with status 1 /usr/bin/python2.5 /var/www/xxx/app.py 4 2010-01-07 11:35:20: (mod_fastcgi.c.1058) If you're trying to run PHP as a FastCGI backend, make sure you're using the FastCGI-enabled version. 5 You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' in the output, NOT '(cgi)' NOR '(cli)'. 6 For more information, check http://trac.lighttpd.net/trac/wiki/Docs%3AModFastCGI#preparing-php-as-a-fastcgi-programIf this is PHP on Gentoo, add 'fastcgi' to the USE flags. 7 2010-01-07 11:35:20: (mod_fastcgi.c.1365) [ERROR]: spawning fcgi failed. 8 2010-01-07 11:35:20: (server.c.902) Configuration of plugins failed. Going down.
check the following:
- are the /var/www and /var/www/python-test directories readable by the www-data group? If not: chgrp -R /var/www
- are you specifying the full path to both python binary and the script?
- have you installed flup? If not: sudo easy_install flup or sudo easy_install-2.5 flup
- can the www-data user run the script? Check with: su – www-data then /usr/bin/python2.5 /var/www/xxx/app.py.
- does your configuration work? This works for me:
1 "/app.py" => (( 2 "bin-environment" => ( 3 "REAL_SCRIPT_NAME" => "" 4 ), 5 "check-local" => "disable", 6 "min-procs" => 1, 7 "bin-path" => "/usr/bin/python2.5 /var/www/xxx/app.py", 8 "socket" => "/tmp/fastcgi.socket" 9 ))