python

WSGI Middleware recommendations

I have heard that there is lots of interesting and useful WSGI middleware around. However, I am not sure which ones (apart from the ones that are part of pylons) are useful and stable. What is your favourite WSGI middleware? ...

Python: can I have a list with named indices?

In PHP I can name my array indicies so that I may have something like: $shows = Array(0 => Array('id' => 1, 'name' => 'Sesaeme Street'), 1 => Array('id' => 2, 'name' => 'Dora The Explorer')); Is this possible in Python? ...

Paver 0.8.1 compatibility with python 2.6

Hi, Does anyone manage to bootstrap its development area using paver with python 2.6 ? I have install python 2.6, install paver with easy_install-2.6, everything looks fine. But when I try to launch the bootstrap method it raises an urllib2.HTTPError (: HTTP Error 404: Not Found) while trying to download http://pypi.python.org/packages...

Python, unit-testing and mocking imports

Hello, I am in a project where we are starting refactoring some massive code base. One problem that immediately sprang up is that each file imports a lot of other files. How do I in an elegant way mock this in my unit test without having to alter the actual code so I can start to write unit-tests? As an example: The file with the funct...

How do I perform an IMAP search in Python (using Gmail and imaplib)?

In Gmail, I have a bunch of labeled messages. I'd like to use an IMAP client to get those messages, but I'm not sure what the search incantation is. c = imaplib.IMAP4_SSL('imap.gmail.com') c.list() ('OK', [..., '(\\HasNoChildren) "/" "GM"', ...]) c.search(???) I'm not finding many examples for this sort of thing. ...

What is the best way to change text contained in an XML file using Python?

Let's say I have an existing trivial XML file named 'MyData.xml' that contains the following: <?xml version="1.0" encoding="utf-8" ?> <myElement>foo</myElement> I want to change the text value of 'foo' to 'bar' resulting in the following: <?xml version="1.0" encoding="utf-8" ?> <myElement>bar</myElement> Once I am done, I want to s...

How do I abort the execution of a Python script?

Possible Duplicate: Terminating a Python script I have a simple Python script that I want to stop executing if a condition is met. For example: done = True if done: # quit/stop/exit else: # do other stuff Essentially, I am looking for something that behaves equivalently to the 'return' keyword in the body of a func...

Average difference between dates in Python

I have a series of datetime objects and would like to calculate the average delta between them. For example, if the input was (2008-10-01 12:15:00, 2008-10-01 12:25:00, 2008-10-01 12:35:00), then the average delta would be exactly 00:10:00, or 10 minutes. Any suggestions on how to calculate this using Python? ...

What is MATLAB good for? Why is it so used by universities? When is it better than Python?

I've been recently asked to learn some MATLAB basics for a class. What does make it so cool for researchers and people that works in university? I saw it's cool to work with matrices and plotting things... (things that can be done easily in Python using some libraries). Writing a function or parsing a file is just painful. I'm still at...

Python class factory ... or?

We have a database library in C# that we can use like this: DatabaseConnection conn = DatabaseConnection.FromConnectionString("..."); this library hides many of the differences between different database engines, like sql function names, parameter names and specifications, etc. Internally, the DatabaseConnection class is an abstract ...

How to handle a broken pipe (SIGPIPE) in python?

I've written a simple multi-threaded game server in python that creates a new thread for each client connection. I'm finding that every now and then, the server will crash because of a broken-pipe/SIGPIPE error. I'm pretty sure it is happening when the program tries to send a response back to a client that is no longer present. What i...

How do I convert a list of ascii values to a string in python?

I've got a list in a Python program that contains a series of numbers, which are themselves ASCII values. How do I convert this into a "regular" string that I can echo to the screen? ...

In the Django admin interface, is there a way to duplicate an item?

Just wondering if there is an easy way to add the functionality to duplicate an existing listing in the admin interface? In data entry we have run into a situation where a lot of items share generic data with another item, and to save time it would be very nice to quickly duplicate an existing listing and only alter the changed data. Us...

Any good cherrypy tutorials or videos?

I am looking for a good in-depth tutorial to get me going on CherryPy and haven't been able to find one on Google. The CherryPy website has a tutorial, but it is rather simple and doesn't discuss more than the basic aspects of CherryPy. I am not interested in the book at this time, as I need to get some progress in the next few days an...

What is the difference between Python's re.search and re.match?

What is the difference between the search() and match() functions in the Python re module? I've read the documentation, but I never seem to remember it. I keep having to look it up and re-learn it. I'm hoping that someone will answer it clearly with examples so that (perhaps) it will stick in my head. Or at least I'll have a better p...

Python find() and newlines

While editing someone else's abandoned Python script, I was trying to use mystring.find("\n", i) to get the index of the next newline in a string. But this consistently returns -1 (even when I try using "\r\n" instead of "\n"). I know there are newlines in the string, because I had just loaded it from a file. I am not very familiar wi...

Python style: multiple-line conditions in IFs

Hello, Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is: if (cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): do_something Isn't very very appealing visually, because the action blends with the conditions. However, it is the natural way using corr...

What is the problem with reduce()?

There seems to be a lot of heated discussion on the net about the changes to the reduce() function in python 3.0 and how it should be removed. I am having a little difficulty understanding why this is the case; I find it quite reasonable to use it in a variety of cases. If the contempt was simply subjective, I cannot imagine that such a ...

py2exe including MSVC DLLs in the .exe

Hello, When using py2exe to distribute Python applications with wxPython, some MSVC DLLs are usually needed to make the .exe work on freshly installed machines. In particular, the two most common DLLs are msvcp71.dll and msvcr71.dll The former can be included in the .exe using this tip. However, the latter is just placed in the dist di...

wxPython: displaying multiple widgets in same frame

I would like to be able to display Notebook and a TxtCtrl wx widgets in a single frame. Below is an example adapted from the wxpython wiki; is it possible to change their layout (maybe with something like wx.SplitterWindow) to display the text box below the Notebook in the same frame? import wx import wx.lib.sheet as sheet class MyShe...