python

Django 1.2 + South 0.7 + django-annoying's AutoOneToOneField leads to TypeError: 'LegacyConnection' object is not iterable

I'm using Django 1.2 trunk with South 0.7 and an AutoOneToOneField copied from django-annoying. South complained that the field does not have rules defined and the new version of South no longer has an automatic field type parser. So I read the South documentation and wrote the following definition (basically an exact copy of the OneToOn...

Periodically updating data in a matplotlib figure embedded in a QWidget

I have a thread that updates some data periodically. I want do visualize the data with matplotlib. When the thread updates the data, the plot should be updated as well. I tried embedding a matplotlib.FigureCanvas(see following snippet) in a QWidget... class MplSubPlotCanvas(FigureCanvas): def __init__(self, parent=None): se...

Where to put Django startup code?

I'd like to have these lines of code executed on server startup (both development and production): from django.core import management management.call_command('syncdb', interactive=False) Putting it in settings.py doesn't work, as it requires the settings to be loaded already. Putting them in a view and accessing that view externally ...

cgi.FieldStorage always empty - never returns POSTed form Data

This problem is probably embarrassingly simple. I'm trying to give python a spin. I thought a good way to start doing that would be to create a simple cgi script to process some form data and do some magic. My python script is executed properly by apache using mod_python, and will print out whatever I want it to print out. My only pro...

Two Dimensional Python Array: Sort second by First

I have a Multidimensional array in Python. How do I go about sorting the second array, by the first - all the while keeping it in the same order? ...

MongoDB ORM for Python?

im trying to convert from sqlalchemy (sqlite) to using mongodb. i would like schema vertification. im looking at mongokit, but i want something with similar to mappers, so that it would save from the object's property, and not a dict. i would like a mapper so that i can use existing objects without modifying them. ...

Mutable global variables don't get hide in python functions, right?

Please see the following code: def good(): foo[0] = 9 # why this foo isn't local variable who hides the global one def bad(): foo = [9, 2, 3] # foo is local, who hides the global one for func in [good, bad]: foo = [1,2,3] print('Before "{}": {}'.format(func.__name__, foo)) func() print('After "{}": {}'.format(...

name of the class that contains the method code

I'm trying to find the name of the class that contains method code. In the example underneath I use self.__class__.__name__, but of course this returns the name of the class of which self is an instance and not class that contains the test() method code. b.test() will print 'B' while I would like to get 'A'. I looked into the inspect m...

How to layout a queue/worker structure to support large tasks for multiple environments?

For a Python/Django/Celery based deployment tool, we have the following setup: We currently use the default Celery setup. (One queue+exchange called "celery".) Each Task on the queue represents a deployment operation. Each task for an environment ends with a synchronisation phase that potentially takes (very) long. The following spec...

Opinions about Dabo

Has anyone used Dabo lately? How does it rate vs Boa Constructor, etc? I'm writing a new Python database app and Dabo looks promising, but what's the real-world scoop on it? Is it used by many developers? It's not talked about very much here on SO, or anywhere, as far as I can tell. I'm just a little concerned that the support community...

Why does Python output a string and a unicode of the same value differently?

I'm using Python 2.6.5 and when I run the following in the Python shell, I get: >>> print u'Andr\xc3\xa9' André >>> print 'Andr\xc3\xa9' André >>> What's the explanation for the above? Given u'Andr\xc3\xa9', how can I display the above value properly in an html page so that it shows André instead of André? ...

Python: Is there a built in package to parse html into dom

I found HTMLParser for sax and xml.minidom for xml. I have a pretty well formed html so I don't need a too strong parser - any suggestions? ...

Datastore query outputting for Django form instance

Hello! I'm using google appengine and Django. I'm using de djangoforms module and wanted to specify the form instance with the information that comes from the query below. userquery = db.GqlQuery("SELECT * FROM User WHERE googleaccount = :1", users.get_current_user()) form = forms.AccountForm(data=request.POST or None,instance...

Most lightweight way to create a random string and a random hexadecimal number

what is the most lightweight way to create a random string of 30 characters like this ? : ufhy3skj5nca0d2dfh9hwd2tbk9sw1 and an hexadecimal number of 30 digits like this ?: 8c6f78ac23b4a7b8c0182d7a89e9b1 ...

Remove substring from a string

I want to remove the first characters from a string. Is there a function that works like this? >>> a = "BarackObama" >>> print myfunction(4,a) ckObama >>> b = "The world is mine" >>> print myfunction(6,b) rld is mine ...

Python: Hack to call a method on an object that isn't of its class

Assume you define a class, which has a method which does some complicated processing: class A(object): def my_method(self): # Some complicated processing is done here return self And now you want to use that method on some object from another class entirely. Like, you want to do A.my_method(7). This is what you'd ...

Django Test Failing

Hello all! I am experiencing an error running django unit tests, I've not experienced this before, and have been googling it all afternoon. I am getting this error in terminal after running django manage.py test: Error: Database test_unconvention couldn't be flushed. Possible reasons: * The database isn't running or isn't configured...

yet another confusion with multiprocessing error, 'module' object has no attribute 'f'

I know this has been answered before, but it seems that executing the script directly "python filename.py" does not work. I have Python 2.6.2 on SuSE Linux. Code: #!/usr/bin/python # -*- coding: utf-8 -*- from multiprocessing import Pool p = Pool(1) def f(x): return x*x p.map(f, [1, 2, 3]) Command line: > python example.py Proce...

Break up a polygon into smaller ones

Hi, I am working with geodjango and I want to breakup a 2D Rectangular Polygon into smaller ones. My input is a big rectangle and I want to subdivide it in smaller rectangles. The sum of the smaller rectangles must be the original rectangle. All subrectangles should be equal size. How can I do that? Thank you. ...

How do I convert a unicode to a string at the Python level?

The following unicode and string can exist on their own if defined explicitly: >>> value_str='Andr\xc3\xa9' >>> value_uni=u'Andr\xc3\xa9' If I only have u'Andr\xc3\xa9' assigned to a variable like above, how do I convert it to 'Andr\xc3\xa9' in Python 2.5 or 2.6? EDIT: I did the following: >>> value_uni.encode('latin-1') 'Andr\xc3\...