python

How can I draw a log-normalized imshow plot with a colorbar representing the raw data in matplotlib

I'm using matplotlib to plot log-normalized images but I would like the original raw image data to be represented in the colorbar rather than the [0-1] interval. I get the feeling there's a more matplotlib'y way of doing this by using some sort of normalization object and not transforming the data beforehand... in any case, there could ...

How to update the filename of a Django's FileField instance ?

Hello, Here a simple django model: class SomeModel(models.Model): title = models.CharField(max_length=100) video = models.FileField(upload_to='video') I would like to save any instance so that the video's file name would be a valid file name of the title. For example, in the admin interface, I load a new instance with title ...

Create static instances of a class inside said class in Python

Apologies if I've got the terminology wrong here—I can't think what this particular idiom would be called. I've been trying to create a Python 3 class that statically declares instances of itself inside itself—sort of like an enum would work. Here's a simplified version of the code I wrote: class Test: A = Test("A") B = Test("B...

Python - animation with matplotlib.pyplot

How can one create animated diagrams using popular matplotlib library? I am particularly interested in animated gifs. ...

How to handle 'self' argument with Python decorators

I am trying to setup some decorators so that I can do something like: class Ball(object): def __init__(self, owner): self.owner = owner class Example(CommandSource): @command @when(lambda self, ball: ball.owner == self) def throwBall(self, ball): # code to throw the ball pass e = Example() ball...

wxPython: Sending a signal to several widgets

I am not even sure how to ask this question. I want something that is like the wxPython event system, but a bit different. I'll try to explain. When there is a certain change in my program (a "tree change", never mind what that is,) I want to send a signal to all the widgets in my program, notifying them that a "tree change" has occurre...

Why connection in Python's DB-API does not have "begin" operation?

Working with cursors in mysql-python I used to call "BEGIN;", "COMMIT;", and "ROLLBACK;" explicitly as follows: try: cursor.execute("BEGIN;") # some statements cursor.execute("COMMIT;") except: cursor.execute("ROLLBACK;") then, I found out that the underlying connection object has the corresponding methods: try: c...

Pylibmc: ImportError: dynamic module does not define init function (init_pylibmc)

>>> import pylibmc Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pylibmc.py", line 55, in <module> import _pylibmc ImportError: dynamic module does not define init function (init_pylibmc) Trying to import pylibmc, which...

Why is python decode replacing more than the invalid bytes from an encoded string?

Trying to decode an invalid encoded utf-8 html page gives different results in python, firefox and chrome. The invalid encoded fragment from test page looks like 'PREFIX\xe3\xabSUFFIX' >>> fragment = 'PREFIX\xe3\xabSUFFIX' >>> fragment.decode('utf-8', 'strict') ... UnicodeDecodeError: 'utf8' codec can't decode bytes in position 6-8: in...

Scipy.cluster.hierarchy.fclusterdata + distance measure

1) I am using scipy's hcluster module. so the variable that I have control over is the threshold variable. How do I know my performance per threshold? i.e. In Kmeans, this performance will be the sum of all the points to their centroids. Of course, this has to be adjusted since more clusters = less distance generally. Is there an obse...

Why does a JRuby application on App Engine take so long to start (versus a Python app)?

I'm considering using JRuby on App Engine but have heard that Juby app on App Engine have a long startup lag versus a Python app. Why is this? Is it because the JRuby jar files are so large that a cold startup requires them to be loaded into memory before the app can start serving? That would be my guess but I'm not sure if that's a p...

Python unicode issues (2.6)

I'm currently working on a irc bot for a multi-lingual channel, and I'm encountering some issues with unicode which are proving nearly impossible to solve. No matter what configuration of unicode encoding I seem to try, the list function which the below code sits within just flat out does nothing (c.notice is a class function which sen...

Official multiple python versions on the same machine?

Is there an official documentation on python website somewhere, on how to install and run multiple versions of python on the same machine? On linux? I can find gazillions of blog posts and answers - but I want to know if there is a "standard" official way of doing this? Or is this all dependent on OS? ...

Python: fetching SVG file using urllib is returning binary when I need ASCII

I'm using urllib (in Python) to fetch an SVG file: import urllib urllib.urlopen('http://alpha.vectors.cloudmade.com/BC9A493B41014CAABB98F0471D759707/-122.2487,37.87588,-122.265823,37.868054?styleid=1&amp;viewport=400x231').read() which produces output of the sort: xb6\xf6\x00\xb3\xfb2\xff\xda\xc5\xf2\xc2\x14\xef\xcd\x82\x0b\xdbU\xb0...

Sorting a dictionary having keys as string of numbers in python

I have a dictionary of the following form a = {'100':12,'6':5,'88':3,'test':34, '67':7,'1':64 } I want to sort this dictionary with respect to key as following a = {'1':64,'6':5,'67':7,'88':3, '100':12,'test':34 } Please help ...

Function to hide sloppy phone numbers..

I need to hide phone numbers (and maybe other contact details) in user generated content to protect my users from anonymous web. Input is very random, therefore I'd be looking to replace anything that looks like a phone number (e.g.: string of 3 or more numbers) with just dots, and also perhaps remove some exotic notations of e-mail add...

In Python, after I INSERT Into mysqldb, how do I get the "id"?

The primary key. cursor.execute("INSERT INTO mytable(height) VALUES(%s)",(height)) My table has 2 columns: id << primary, auto increment height << this is the other column. How do I get the "id", after I just inserted this? ...

working python xml

hello all, my question is the following, which is the best way of working XML (kml) with python?, especially script serializable. thanks for your attention and answers ...

Does anyone have feeback on using Psyco in a wsgi application?

Did you try Psyco in a wsgi application (custom, Pylons, Django...)? What does your set up look like? Did you get measurable results? ...

How to stringfy a swig matrix object in python

Hi, I am using swig wrapper of openbabel (written in C++, and supply a python wrapper through swig) Below i just use it to read a molecule structure file and get the unitcell property of it. import pybel for molecule in pybel.readfile('pdb','./test.pdb'): unitcell = molecule.unitcell print unitcell |..> |..> <o...