python

wxPython: How should I organize per-widget data in the controller?

I have a widget that displays a filesystem hierarchy for convenient browsing (basically a tree control and some associated toolbar buttons, such as "refresh"). Each of these widgets has a set of base directories for it to display (recursively). Assume that the user may instantiate as many of these widgets as they find convenient. Note th...

Django Forms not rendering ModelChoiceField's query set

I have the following ModelForm: class AttendanceForm(forms.ModelForm): def __init__(self, *args, **kwargs): operation_id = kwargs['operation_id'] del kwargs['operation_id'] super(AttendanceForm, self).__init__(*args, **kwargs) self.fields['deployment'].query_set = \ Deployment.objects.filt...

Python-equivalent of short-form "if" in C++

Is there a way to write this C/C++ code in Python? a = (b == true ? "123" : "456" ) Thanks so much! ...

Problem with python soap library suds. Wsdl was not understood

The code below throw a SAXParseException: "mismatched tag": from suds.client import Client <br> url = 'http://www.didww.com/api/?wsdl' client = Client(url, cache=None) print client Is it problem with suds, or there is some errors in wsdl? ...

Why does Pylons use StackedObjectProxies instead of threading.local?

It seems like threading.local is more straightforward and more robust. ...

Sphinx (documentation tool): set tab width in output

How do you set tab width in HTML output of Sphinx code snippets highlighted by Pygments? By default it is the annoying 8, but I want 4. Did not find a word about this setting in Sphinx conf.py. ...

Weighted average of angles

I want to calculate the weighted mean of a set of angles. In this Question, there's an answer how to calculate the mean as shown in this page. Now I'm trying to figure out how to calculate the weighted average. That is, for each angle there is a weight (the weights sum up to 1) 0.25, 0 degrees 0.5, 20 degrees 0.25, 90 degrees The we...

How to profile a Django custom management command exclusively

I would like to profile a custom management command that is relatively CPU intensive (renders an image using PIL). When I use the following command I get all sorts of Django modules (admin, ORM etc) in my profiling results: python -m cProfile manage.py testrender I have removed all imports that can potentially import Django but I am g...

Why does str.lstrip strips an extra character?

Python 2.6 (trunk:66714:66715M, Oct 1 2008, 18:36:04) [GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> path = "/Volumes/Users" >>> path.lstrip('/Volume') 's/Users' >>> path.lstrip('/Volumes') 'Users' >>> I am expecting output of path.lstrip('/Volumes') ...

why cant i just install the pycrypto?

i type "python setup.py install" in a windows XP console, it reported as follows: running install running build running build_py running build_ext warning: GMP library not found; Not building Crypto.PublicKey._fastmath. building 'Crypto.Random.OSRNG.winrandom' extension error: None when i try to run a script which import Crypto.Cipher...

How do you generate random unique identifiers in a multi process and multi thread environment?

Every solution I come up with is not thread save. def uuid(cls,db): u = hexlify(os.urandom(8)).decode('ascii') db.execute('SELECT sid FROM sessions WHERE sid=?',(u,)) if db.fetch(): u=cls.uuid(db) else: db.execute('INSERT INTO sessions (sid) VALUES (?)',(u,)) return u ...

Updating Python on Mac

I wanted to update my python 2.6.1 to 3.x on mac but i was wondering if its possible to do it using terminal or i have to download the installer from python website? The reason why i am asking this question is because installer is not updating my terminal python version. ...

What is the default chunker for NLTK toolkit in Python?

I am using their default POS tagging and default tokenization..and it seems sufficient. I'd like their default chunker too. I am reading the NLTK toolkit book, but it does not seem like they have a default chunker? ...

sorl.thumbnail : 'thumbnail' is not a valid tag library ?

I am trying to install sorl.thumbnail but am getting the following error message: 'thumbnail' is not a valid tag library: Could not load template library from django.templatetags.thumbnail, No module named PIL This error popped up in this question as well http://stackoverflow.com/questions/1356334/need-help-solving-sorl-thumbnail-error...

How can I tell if a given method is a classmethod or instancemethod in Python?

Checking to see if m.im_self is the class works some of the time but doesn't seem to be 100% reliable (ex. if you use multiple decorators on a method.) ...

Why does an assignment for double-sliced numpy arrays not work?

Hello everybody, why do the following lines not work as I expect? import numpy as np a = np.array([0,1,2,1,1]) a[a==1][1:] = 3 print a >>> [0 1 2 1 1] # I would expect [0 1 2 3 3] Is this a 'bug' or is there another recommended way to this? On the other hand, the following works: a[a==1] = 3 print a >>> [0 3 2 3 3] Cheers, Philip...

Python app distribution cross-platform

I want to distribute my app on OSX (using py2app) and as a Debian package. The structure of my app is like: app/ debian/ <lots of debian related stuff> scripts/ app app/ __init__.py app.py mod1/ __init__.py a.py mod2/ _...

python cProfile and profile models skip functions

basically the cProfile module skips some functions when i run it, and the normal profile module produces this error. The debugged program raised the exception unhandled AssertionError "('Bad call', ('objects/controller/StageController.py', 9, '__init__'), <frame object at 0x9bbc104>, <frame object at 0x9bb438c>, <frame object at 0x9...

How flatten a list of lists one step

I have a list of lists of tuples A= [ [(1,2,3),(4,5,6)], [(7,8,9),(8,7,6),(5,4,3)],[(2,1,0),(1,3,5)] ] The outer list can have any number of inner lists, the inner lists can have any number of tuples, a tuple always has 3 integers. I want to generate all combination of tuples, one from each list: [(1,2,3),(7,8,9),(2,1,0)] [(1,2,3),...

Datetime - 10 Hours

Consider: now = datetime.datetime.now() now datetime.datetime(2009, 11, 6, 16, 6, 42, 812098) How would I create a new datetime object (past) and minus n values from the hours? ...