python

"Pretty" Continuous Integration for Python

This is a slightly.. vain question, but BuildBot's output isn't particularly nice to look at.. For example, compared to.. phpUnderControl Hudson CruiseControl.rb ..and others, BuildBot looks rather.. archaic I'm currently playing with Hudson, but it is very Java-centric (although with this guide, I found it easier to setup than Bui...

Unexpected list comprehension behaviour in Python.

I believe I'm getting bitten by some combination of nested scoping rules and list comprehensions. Jeremy Hylton's blog post is suggestive about the causes, but I don't really understand CPython's implementation well-enough to figure out how to get around this. Here is an (overcomplicated?) example. If people have a simpler one that d...

Batch Renaming of Files in a Directory

Is there an easy way to rename a group of files already contained in a directory, using Python? Example: I have a directory full of *.doc files and I want to rename them in a consistent way. X.doc -> "new(X).doc" Y.doc -> "new(Y).doc" ...

Decoding a WBXML SyncML message from an S60 device

I'm trying to decode a WBXML encoded SyncML message from a Nokia N95. My first attempt was to use the python pywbxml module which wraps calls to libwbxml. Decoding the message with this gave a lot of <unknown> tags and a big chunk of binary within a <Collection> tag. I tried running the contents of the <Collection> through by itself but ...

Django Forms - How to Use Prefix Parameter

Say I have a form like: class GeneralForm(forms.Form): field1 = forms.IntegerField(required=False) field2 = forms. IntegerField(required=False) And I want to show it twice on a page within one form tag each time with a different prefix e.g.,: rest of page ... <form ..> GeneralForm(data,prefix="form1").as_table() GeneralForm(d...

Python Disk-Based Dictionary

Hello, I was running some dynamic programming code (trying to brute-force disprove the Collatz conjecture =P) and I was using a dict to store the lengths of the chains I had already computed. Obviously, it ran out of memory at some point. Is there any easy way to use some variant of a dict which will page parts of itself out to disk whe...

How can you extract Hardware ID using Python?

How do you extract an HD and Bios Unique ID, using python script? ...

How do you create a simple Google Talk Client using the Twisted Words Python library?

I am interested in making a Google Talk client using Python and would like to use the Twisted libraries Words module. I have looked at the examples, but they don't work with the current implementation of Google Talk. Has anybody had any luck with this? Would you mind documenting a brief tutorial? As a simple task, I'd like to create ...

Solving an inequality for minimum value

I'm working on a programming problem which boils down to a set of an equation and inequality: x[0]*a[0] + x[1]*a[1] + ... x[n]*a[n] >= D x[0]*b[0] + x[1]*b[1] + ... x[n]*b[n] = C I want to solve for the values of X that will give the absolute minimum of C, given the input D and lists and A and B consisting of a[0 - n] and b[0 - n ]. ...

What's a good resource for learning CGI programming in Python?

I need to write a browser interface for an application running embedded on a single board computer (Gumstix Verdex for anyone who's interested), so I won't be able to use any web frameworks due to space and processor constraints (and availability for the environment I'm running in). I'm limited to the core Python and cgi modules to crea...

ASCII value of a character in python

How do I get the ASCII value of a character as an int in python? ...

Open file, read it, process, and write back - shortest method in Python

Hello, I want to do some basic filtering on a file. Read it, do processing, write it back. I'm not looking for "golfing", but want the simplest and most elegant method to achieve this. I came up with: from __future__ import with_statement filename = "..." # or sys.argv... with open(filename) as f: new_txt = # ...some translatio...

Passing a list while retaining the original

So I'm teaching myself Python, and I'm having an issue with lists. I want to pass my function a list and pop items off it while retaining the original list. How do I make python "instance" the passed list rather that passing a pointer to the original one? Example: def burninate(b): c = [] for i in range(3): c.append(b.pop(...

What's win32con module in python? Where can I find it?

I'm building an open source project that uses python and c++ in Windows. I came to the following error message: ImportError: No module named win32con The same happened in a "prebuilt" code that it's working ( except in my computer :P ) I think this is kind of "popular" module in python because I've saw several messages in other for...

zen of python

There is the Zen of Python written by Tim Peters. It is considered like a summary manual of python's philosophy. Here it is: >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is bet...

Python debugger: Stepping into a function that you have called interactively

Python is quite cool, but unfortunately, its debugger is not as good as perl -d. One thing that I do very commonly when experimenting with code is to call and step into a function interactively, like such: # NOTE THAT THIS PROGRAM EXITS IMMEDIATELY WITHOUT CALLING FOO() ~> cat -n /tmp/show_perl.pl 1 #!/usr/local/bin/perl 2 3 sub fo...

How to get hex string from signed integer

Say I have the classic 4-byte signed integer, and I want something like print hex(-1) to give me something like >> 0xffffffff In reality, the above gives me -0x1. I'm dawdling about in some lower level language, and python commandline is quick n easy. So.. is there a way to do it? ...

How do I iterate through the alphabet in Python?

As an example, lets say I wanted to list the frequency of each letter of the alphabet in a string. What would be the easiest way to do it? This is an example of what I'm thinking of... the question is how to make allTheLetters equal to said letters without something like allTheLetters = "abcdefg...xyz". In many other languages I could j...

SQLite parameter substitution problem

Using SQLite3 with Python 2.5, I'm trying to iterate through a list and pull the weight of an item from the database based on the item's name. I tried using the "?" parameter substitution suggested to prevent SQL injections but it doesn't work. For example, when I use: for item in self.inventory_names: self.cursor.execute("SELECT w...

os.walk without digging into directories below

How do i limit os.walk to only return files in the directory i provide it? def _dir_list(self, dir_name, whitelist): outputList = [] for root, dirs, files in os.walk(dir_name): for f in files: if os.path.splitext(f)[1] in whitelist: outputList.append(os.path.join(root, f)) else: ...