python

Binary Search in Python

Is there a library function that performs binary search on a list/tuple and return the position of the item if found and 'False' (-1, None, etc.) if not? I found the functions bisect_left/right in the bisect module, but they still return a position even if the item is not in the list. That's perfectly fine for their intended usage, but ...

Opening a handle to a device in Python on Windows

I'm trying to use the giveio.sys driver which requires a "file" to be opened before you can access protected memory. I'm looking at a C example from WinAVR/AVRdude that uses the syntax: #define DRIVERNAME "\\\\.\\giveio" HANDLE h = CreateFile(DRIVERNAME, GENERIC_READ, 0, NULL, OPEN...

Keyboard interruptable blocking queue in Python

Hi! It seems import Queue Queue.Queue().get(timeout=10) is keyboard interruptible (ctrl-c) whereas import Queue Queue.Queue().get() is not. I could always create a loop; import Queue q = Queue() while True: try: q.get(timeout=1000) except Queue.Empty: pass but this seems like a strange thing to do. S...

Using django-rest-interface

I have a django application that I'd like to add some rest interfaces to. I've seen http://code.google.com/p/django-rest-interface/ but it seems to be pretty simplistic. For instance it doesn't seem to have a way of enforcing security. How would I go about limiting what people can view and manipulate through the rest interface? Norma...

python threadsafe object cache

Hi, I have implemented a python webserver. Each http request spawns a new thread. I have a requirement of caching objects in memory and since its a webserver, I want the cache to be thread safe. Is there a standard implementatin of a thread safe object cache in python? I found the following http://freshmeat.net/projects/lrucache/ Thi...

A good multithreaded python webserver?

Hi, I am looking for a python webserver which is multithreaded instead of being multi-process (as in case of mod_python for apache). I want it to be multithreaded because I want to have an in memory object cache that will be used by various http threads. My webserver does a lot of expensive stuff and computes some large arrays which nee...

How to convert a C string (char array) into a Python string?

I have embedded a Python interpreter in a C program. Suppose the C program reads some bytes from a file into a char array and learns (somehow) that the bytes represent text with a certain encoding (e.g., ISO 8859-1, Windows-1252, or UTF-8). How do I decode the contents of this char array into a Python string? The Python string should ...

Would Python make a good substitute for the Windows command-line/batch scripts?

I've got some experience with Bash, which I don't mind, but now that I'm doing a lot of Windows development I'm needing to do basic stuff/write basic scripts using the Windows command-line language. For some reason said language really irritates me, so I was considering learning Python and using that instead. Is Python suitable for suc...

How to improve Trac's performance

I have noticed that my particular instance of Trac is not running quickly and has big lags. This is at the very onset of a project, so not much is in Trac (except for plugins and code loaded into SVN). Setup Info: This is via a SELinux system hosted by WebFaction. It is behind Apache, and connections are over SSL. Currently the .htpa...

How can I write a wrapper around ngrep that highlights matches?

I just learned about ngrep, a cool program that lets you easily sniff packets that match a particular string. The only problem is that it can be hard to see the match in the big blob of output. I'd like to write a wrapper script to highlight these matches -- it could use ANSI escape sequences: echo -e 'This is \e[31mRED\e[0m.' I'm mo...

Using trellis as a framework for managing UI interaction rules

Does anyone have experience with trellis? Looking at it as a framework for defining rules for field interaction and validation in grids and data entry screens. ...

Python templates for web designers

What are some good templating engines for web designers? I definitely have my preferences as to what I'd prefer to work with as a programmer. But web designers seem to have a different way of thinking about things and thus may prefer a different system. So: Web designers: what templating engine do you prefer to work with? programme...

How to create a numpy record array from C

On the Python side, I can create new numpy record arrays as follows: numpy.zeros((3,), dtype=[('a', 'i4'), ('b', 'U5')]) How do I do the same from a C program? I suppose I have to call PyArray_SimpleNewFromDescr(nd, dims, descr), but how do I construct a PyArray_Descr that is appropriate for passing as the third argument to PyArray_S...

How do you convert YYYY-MM-DDTHH:mm:ss.000Z time format to MM/DD/YYYY time format in Python?

For example, I'm trying to convert 2008-09-26T01:51:42.000Z to 09/26/2008. What's the simplest way of accomplishing this? ...

python module dlls

Is there a way to make a python module load a dll in my application directory rather than the version that came with the python installation, without making changes to the python installation (which would then require I made an installer, and be careful I didn't break other apps for people by overwrting python modules and changing dll ve...

Can you add new statements to Python's syntax?

Can you add new statements (like print, raise, with) to Python's syntax? Say, to allow.. mystatement "Something" Or, new_if True: print "example" Not so much if you should, but rather if it's possible (short of modifying the python interpreters code) ...

Efficient Image Thumbnail Control for Python?

What is the best choice for a Python GUI application to display large number of thumbnails, e.g. 10000 or more? For performance reasons such thumbnail control must support virtual items, i.e. request application for those thumbnails only which are currently visible to user. ...

wxpython - Expand list control vertically not horizontally

I have a ListCtrl that displays a list of items for the user to select. This works fine except that when the ctrl is not large enough to show all the items, I want it to expand downwards with a vertical scoll bar rather than using a horizontal scroll bar as it expands to the right. The ListCtrl's creation: self.subjectList = wx.ListCtr...

How do you fix a Trac installation that begins giving errors relating to PYTHON_EGG_CACHE?

We've been using Trac for task/defect tracking and things were going well enough, but this morning it started serving up a 500 error. Looking in the Apache error_log, I get a stack trace that culminates in: PythonHandler trac.web.modpython_frontend: ExtractionError: Can't extract file(s) to egg cache The following error occurred whi...

What's a good library to manipulate Apache2 config files?

I'd like to create a script to manipulate Apache2 configuration directly, reading and writing its properties (like adding a new VirtualHost, changing settings of one that already exists). Are there any libs out there, for Perl, Python or Java that automates that task? ...