python

Representing an immutable hierarchy using tuples

I am trying to represent a hierarchy using namedtuple. Essentially, every node has three attributes relevant to the hierarchy: parent, leftChild and rightChild (they also have some attributes that carry the actual information, but that is not important for the question). The problem is the circular reference between parents and children....

Pygame - calling surface.convert() on animated sprite causes transparent background to become white.

Everyone says to use .convert() on surfaces to speed up animations (which will be an issue with my game because it will be and mmo...to some extent, so it might have a dozen or a couple dozen characters moving at the same time), the problem is that my transparent png images work great without convert but as soon as i use .convert() all o...

How to convert raw html from the web into parsable xml in Python

I thought BeautifulSoup could do that, but it does not seem to do the trick. What method have you already used, and is long term reliable ? ...

How to properly use relative or absolute imports in Python modules?

Usage of relative imports in Python has one drawback, you will not be able to run the modules as standalones anymore because you will get an exception: ValueError: Attempted relative import in non-package # /test.py: just a sample file importing foo module import foo ... # /foo/foo.py: from . import bar ... if __name__ == "__main__": ...

opening file: Writing is invalid mode

Hello, When executing: path=os.path.dirname(__file__)+'/log.txt' log=open(path,"w",encoding='utf-8') I get: log=open(path,'w',encoding='utf-8') File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1203, in __init__ raise IOError('invalid mode: %s' % mode) IOError: invalid mode: w I'm not sur...

A-z Index Django

Im looking for advice on desinging a web page with an A - Z index. Kind of like : http://www.bls.gov/bls/topicsaz.htm I have a long list of objects, with a title, that I want do display alphabetically, easy! But I want to put in the A-Z with anchors, do I do this in the template, I would have to loop through all the objects in the t...

Escape string Python for MySQL

Hello, I use Python and MySQLdb to download web pages and store them into database. The problem I have is that I can't save complicated strings into database because they are not escaped properly. Is there a function in Python I can use to escape a string for MySQL? I tried with ''' (tiple simple quotes) and """, but it didn't work. I ...

Average timedelta in list

Hi, I want to calculate the avarage timedelta between dates in a list. Although the following works well, I'm wondering if there's a smarter way? delta = lambda last, next: (next - last).seconds + (next - last).days * 86400 total = sum(delta(items[i-1], items[i]) for i in range(1, len(items))) average = total / (len(items) - 1) ...

returning matches from an unknown number of python lists

I have a list which contains 1-5 lists within it. I want to return only the values which appear in all the lists. I can easily create an exception for it there is only one list, but I can't think of a way round it when there are an multiple (unknown number of) lists. For example: [[1,2,3,4],[2,3,7,8],[2,3,6,9],[1,2,5,7]] would only re...

Single-file storage for a Python application

I'm starting the development of a Python application that provides a GUI for editing various graphical and numerical entities (it's a configuration and management tool for mobile robots, if you must know). One of the questions is how to store the data for a project (meaning, for a robot). The data quantity will be low (about 10MB max) an...

Python socket module: http proxy

Hello I'm trying to use protected http socks server with socket module as in the code shown below >>> import socket >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> host = 'http://user:[email protected]' >>> port = 8888 >>> s.bind((host, port)) It gives me error: socket.gaierror: [Errno -2] Name or service not known Th...

Twisted transport.write

Is there any way to force self.transport.write(response) to write immediately to its connection so that the next call to self.transport.write(response) does not get buffered into the same call. We have a client with legacy software we cannot amend, that reads for the 1st request and then starts reading again, and the problem I have is...

Python urllib2: gethostbyname

I need to get requested host's ip address using urllib2 like import urllib2 req = urllib2.Request('http://www.example.com/') r = urllib2.urlopen(req) Is there any issues like ip = urllib2.gethostbyname(req)? Sultan ...

Utf-8 with sqlalchemy on a database with init connect

I am trying to use sqlalchemy to connect with mysql database. I have set up charset=utf-8$use_unicode=0. This worked with almost all databases, but not with a particular one. I believe it is because it has 'init-connect' variable set to 'SET NAMES latin2;' I have no privileges to change that. It works for me if I send explicit query SET...

How to spawn a browser

Hi, I'm developing a irc client in python based on irc.IRCClient and pygtk, I'm using the correct reactor and all works fine. Now I would launch a browser when clicking a Url... The better choice is to use xdg-open which runs the configured default browser (in a free desktop compliant DE). The url is picked in a gtk button-press-event....

Parallelize resolution of differential equation in Python

hi, i am solving a system of ordinary differential equations using the odeint function. Is it possible (and if yes how) to parallelize easily this kind of problem? ...

Django QuerySet .defer() problem - bug or feature?

An example is better than a thousand words: In [3]: User.objects.filter(id=19)[0] == User.objects.filter(id=19)[0] Out[3]: True In [4]: User.objects.filter(id=19)[0] == User.objects.filter(id=19).defer('email')[0] Out[4]: False Does it work like this on purpose ? Subquestion: is there any simple way to get a regular mode...

python: defining registry in base class

I'm implementing enumeration using a base class that defines a variety of methods. The actual enumerations are subclasses of that, with no additional methods or attributes. (Each subclass is populated with its own values using the constructor defined in the base class). I use a registry (a class attribute that stores all the instances o...

How to insert bulk data in Google App Engine Datastore?

Hi All, I have some CSV files for cities,state and countries with their ids, names etc. I want to put all this data into Google app engine datastore. Can someone please suggest an efficient way of doing this on development server as well as on the production server? Thanks in advance. ...

python: regular expressions, how to match a string of undefind length which has a structure and finishes with a specific group

Hi, I need to create a regexp to match strings like this 999-123-222-...-22 The string can be finished by &Ns=(any number) or without this... So valid strings for me are 999-123-222-...-22 999-123-222-...-22&Ns=12 999-123-222-...-22&Ns=12 And following are not valid: 999-123-222-...-22&N=1 I have tried testing it several hours alr...