python

quick python string parsing question

If i have a string like: "user1:type1,user2:type2,user3:type3" and I want to convert this to a list of tuples like so: [('user1','type1'),('user2','type2'),('user3','type3')] how would i go about doing this? I'm fairly new to python but couldn't find a good example in the documentation to do this. Thanks! ...

How to append a tag after a link with BeaufitulSoup

Starting from an Html input like this: <p> <a href="http://www.foo.com"&gt;this if foo</a> <a href="http://www.bar.com"&gt;this if bar</a> </p> using BeautifulSoup, i would like to change this Html in: <p> <a href="http://www.foo.com"&gt;this if foo</a><b>OK</b> <a href="http://www.bar.com"&gt;this if bar</a><b>OK</b> </p> Is it po...

Best way to handle timed events in PyGame

I'm working on a Tetris-clone type game and I'm wondering what would be the best way to handle the logic that moves the blocks down every N seconds. I initially did it the easy way: pygame.time.set_timer(USEREVENT+1, 300) This will of course add a PyGame event to the queue every 300 milliseconds. In the main loop I can test for this e...

Formatting inline many-to-many related models presented in django admin

I've got two django models (simplified): class Product(models.Model): name = models.TextField() price = models.IntegerField() class Invoice(models.Model): company = models.TextField() customer = models.TextField() products = models.ManyToManyField(Product) I would like to see the relevant products as a nice tabl...

Which Python XML library should I use?

Hello. I am going to handle XML files for a project. I had earlier decided to use lxml but after reading the requirements, I think ElemenTree would be better for my purpose. The XML files that have to be processed are: Small in size. Typically < 10 KB. No namespaces. Simple XML structure. Given the small XML size, memory is not...

what does "from MODULE import _" do in python?

Hi all, In the Getting things gnome code base I stumbled upon this import statement from GTG import _ and have no idea what it means, never seen this in the documentation and a quick so / google search didn't turn anything up. Thank you all in advance Paul ...

Problem with insert and psycopg2

Hello. I'm a newbie in Python and psycopg2 and have problems with a simple insert. This is my table: CREATE TABLE tabla ( codigo integer NOT NULL DEFAULT nextval('dato_codigo_seq'::regclass), informacion character(30) NOT NULL, CONSTRAINT dato_pkey PRIMARY KEY (codigo) ) The field codigo is a serial. When I do the sentence: cursor....

How can I get more detail on why a fixture is not loading?

I have a TestCase that doesn't seem to load the fixtures. I'm seeing this error as the test database is being built: No fixtures found. .............................................Problem installing fixture '/Users/Bryan/work/CNPROG/forum/fixtures/forum_fixtures.json': Traceback (most recent call last): File "/Library/Frameworks...

Utility that helps in file locking - expert tips wanted

I've written a subclass of file that a) provides methods to conveniently lock it (using fcntl, so it only supports unix, which is however OK for me atm) and b) when reading or writing asserts that the file is appropriately locked. Now I'm not an expert at such stuff (I've just read one paper [de] about it) and would appreciate some fee...

django admin site - filtering available objects for user

I have models that belong to some 'group' (Company class). I want to add users, who will also belong to a one group and should be able to edit/manage/add objects with membership in associated group. something like: class Company() class Something() company = ForeignKey(Company) user Microsoft_admin company = ForeignKey(Company) ...

Problem executing script using Python and subprocces.call yet works in Bash

Hello, For the first time, I am asking a little bit of help over here as I am more of a ServerFault person. I am doing some scripting in Python and I've been loving the language so far yet I have this little problem which is keeping my script from working. Here is the code line in question : subprocess.call('xen-create-image --hostna...

Extra characters Extracted with XPath and Python (html)

I have been using XPath with scrapy to extract text from html tags online, but when I do I get extra characters attached. An example is trying to extract a number, like "204" from a <td> tag and getting [u'204']. In some cases its much worse. For instance trying to extract "1 - Mathoverflow" and instead getting [u'\r\n\t\t ...

Python: What's a correct and good way to implement __hash__()?

What's a correct and good way to implement __hash__()? I am talking about the function that returns a hashcode that is then used to insert objects into hashtables aka dictionaries. As __hash__() returns an integer and is used for "binning" objects into hashtables I assume that the values of the returned integer should be uniformly dist...

Using bundle_files = 1 with py2exe is not working

Hi folks, After some big frustration I did it! I converted my django app to an "exe" one to run as a single standalone app on windows (using cherrypy as a WSGI server) But When I try to to set py2exe's option "bundle_files" to "1" (i.e. bundle the python interpreter Python25.dll inside the generated exe) the generated exe crashes with a...

Eventlet client-server

I'm trying to setup a server-client interraction: Server gets a string from client Server returns data to client I got this working with socket.send and socket.recv, but was wondering how to get it working with files through socket.makefile. I've got most of the pieces working, but don't know how to force each of the pieces to read ...

This is my current way of writing to a file. However, I can't do UTF-8?

f = open("go.txt", "w") f.write(title) f.close() What if "title" is in japanese/utf-8? How do I modify this code to be able to write "title" without having the ascii error? Edit: Then, how do I read this file in UTF-8? ...

Python adding elements from string

I have a string like this "1 1 3 2 1 1 1 2 1 1 1 1 1 1 1 1,5 0,33 0,66 1 0,33 0,66 1 1 2 1 1 2 1 1 2 0,5 0,66 2 1 2 1 1 1 0 1". How to add elements to each other in python ? ...

Is it bad practice to use python's getattr extensively?

I'm creating a shell-like environment. My original method of handleing user input was to use a dictionary mapping commands (strings) to methods of various classes, making use of the fact that functions are first class objects in python. For flexibility's sake (mostly for parsing commands), I'm thinking of changing my setup such that I'm...

Paramiko and Pseudo-tty Allocation

I'm trying to use Paramiko to connect to a remote host and execute a number of text file substitutions. i, o, e = client.exec_command("perl -p -i -e 's/" + initial + "/" + replaced + "/g'" + conf); Some of these commands need to be run as sudo, which results in: sudo: sorry, you must have a tty to ...

Determine MP3 bit depth in Python via Mutagen

Is there a way to determine an MP3 file's encoded bit depth (ie 8, 16, 24, 32) in Python using the Mutagen library? ...