python

Nice exception handling when re-trying code

I have some test cases. The test cases rely on data which takes time to compute. To speed up testing, I've cached the data so that it doesn't have to be recomputed. I now have foo(), which looks at the cached data. I can't tell ahead of time what it will look at, as that depends a lot on the test case. If a test case fails cause it doe...

Is there a FileIO in Python?

I know there is a StringIO stream in Python, but is there such a thing as a file stream in Python? Also is there a better way for me to look up these things? Documentation, etc... I am trying to pass a "stream" to a "writer" object I made. I was hoping that I could pass a file handle/stream to this writer object. ...

Basic tree in Python with a Django QuerySet

Here's where I'm exposed as the fraud of a programmer I am. I've never created a data tree. Basically, I have a table with four fields: A, B, C, and D. I need to create a tree of unordered lists based on these fields. Ultimately, it would look something like this: A1 B1 C1 D1 D2 C2 D3 D4 B2 C2 D5 D6 C3 D7 D8 A2 B2 C1...

Django hitting MySQL even after select_related() ?

I'm trying to optimize the database calls coming from a fairly small Django app. At current I have a couple of models, Inquiry and InquiryStatus. When selecting all of the records from MySQL, I get a nice JOIN statement on the two tables, followed by many requests to the InquiryStatus table. Why is Django still making individual reque...

How to explicitly specify a path to Firefox for Selenium?

I got Selenium IDE, followed this post, got to python test_default_server.py and it complains Firefox is not in my path: Please add the directory containing ''firefox.exe'' to your PATH environment variable, or explicitly specify a path to Firefox 3 like this: *firefox3c:\blah\firefox.exe I could change my PATH environment variable...

Condensing code in Python with Mappings

I seem to be using this block of code alot in Python. if Y is not None: obj[X][0]=Y How do I establish a mapping from X=>Y and then iterate through this entire mapping while calling that block of code on X and Y ...

In Python, find item in list of dicts, using bisect

I have a list of dicts, something like this: test_data = [ { 'offset':0, 'data':1500 }, { 'offset':1270, 'data':120 }, { 'offset':2117, 'data':30 }, { 'offset':4055, 'data':30000 }, ] The dict items are sorted in the list according to the 'offset' data. The real data could be much longer. What I want to do is lookup a...

Django vs. Pylons

I've recently become a little frustrated with Django as a whole. It seems like I can't get full control over anything. I love Python to death, but I want to be able (and free) to do something as simple as adding a css class to an auto-generated form. One MVC framework that I have really been enjoying working with is Grails (groovy). It...

Programming a Self Learning Music Maker

I want to learn how to program a music application that will analyze songs. How would I get started in this and is there a library for analyzing soundwaves? I know C, C++, Java, Python, some assembly, and some Perl. Related question: Algorithm for music imitation ...

Replacing leading and trailing hyphens with spaces?

What is the best way to replace each occurrence of a leading or trailing hyphen with a space? For example, I want ---ab---c-def-- to become 000ab---c-def00 (where the zeros are spaces) I'm trying to do this in Python, but I can't seem to come up with a regex that will do the substitution. I'm wondering if there is another, better wa...

Simple recursive descent in PyParsing

I've tried taking this code and converting it to something for a project I'm working on for programming language processing, but I'm running into an issue with a simplified version: op = oneOf( '+ - / *') lparen, rparen = Literal('('), Literal(')') expr = Forward() expr << ( Word(nums) | ( expr + op + expr ) | ( lparen + expr + rparen)...

How to set timeout detection on a RabbitMQ server?

I am trying out RabbitMQ with this python binding. One thing I noticed is that if I kill a consumer uncleanly (emulating a crashed program), the server will think that this consumer is still there for a long time. The result of this is that every other message will be ignored. For example if you kill a consumer 1 time and reconnect, th...

How can I convert a list of strings into numeric values?

How can I convert a list of strings (each of which represents a number, i.e. [‘1’, ‘2’, ‘3’]) into numeric values. ...

How to efficiently determine if webpage comes from a website

I have some unknown webpages and I want to determine which websites they come from. I have example webpages from each website and I assume each website has a distinctive template. I do not need complete certainty, and don't want to use too much resources matching each webpage. So crawling each website for the webpage is out of the questi...

command line arg parsing through introspection

I'm developing a management script that does a fairly large amount of work via a plethora of command-line options. The first few iterations of the script have used optparse to collect user input and then just run down the page, testing the value of each option in the appropriate order, and doing the action if necessary. This has resulted...

How to quickly (easy to script) preview 3D vectors / lines?

I am busy reading 3D building models from a tool and thus generating a bunch of Line(p1, p2) objects, each consisting of two Point(x, y, z) objects. I would like to display these things in a simple 3D viewer, kind of like SVG (which, as I understand, only supports 2D). The reading is done in Python, specifically IronPython. I could use ...

Determine if an executable (or library) is 32 -or 64-bits (on Windows)

I am trying to find out if a given executable (or library) is compiled for 32-bits or 64-bits from Python. I am running Vista 64-bits and would like to determine if a certain application in a directory is compiled for 32-bits or 64-bits. Is there a simple way to do this using only the standard Python libraries (currently using 2.5.4)? ...

How do I find the time difference between two datetime objects in python?

How do I tell the time difference in minutes between two datetime objects? ...

What's the most Pythonic way of determining endianness?

I'm trying to find the best way of working out whether the machine my code is running on is big-endian or little-endian. I have a solution that works (although I haven't tested it on a big-endian machine) but it seems a bit clunky: import struct little_endian = (struct.pack('@h', 1) == struct.pack('<h', 1)) This is just comparing a 'n...

Threads in python

I am beginar in python script. I want read msaccess database records and write into XML file. Access database table have more than 20000 records. Now i am able to do but , it is taking 4 to 5 minutes. So i implement threading concept. But threading also taking more than 5 to 6 minutes. Because each thread open datasource reading records...