python

apache user can not write to .python-eggs

I have read that I need to set the PYTHON_EGG_CACHE environment variable, or install the python library as an uncompressed .egg Which do you suggest? ...

Install MySQLdb (for python) as non-compressed egg

The install instructions are: $ python setup.py build $ sudo python setup.py install # or su first This gives me an .egg file. How do I tell the install to dump the files as a normal, uncompressed library? Thanks! ...

Getting key with maximum value in dictionary?

I have a dictionary: keys are strings, values are integers. Example: stats = {'a':1000, 'b':3000, 'c': 100}. I'd like to get 'b' as an answer, since it's the key with a higher value. I did the following, using an intermediate list with reversed key-value tuples: inverse = [(value, key) for key, value in stats.items()] print ma...

How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?

The python documentation states that BaseHTTPServer.HTTPServer is a subclass of SocketServer.TCPServer, which dows support a shutdown method - but it is missing in HTTPServer. I am running my HTTPServer in a separate thread (using the threading module which has no way to stop threads...) and want to stop serving requests when the main t...

Replacing multiple occurrences in nested arrays

I've got this python dictionary "mydict", containing arrays, here's what it looks like : mydict = dict( one=['foo', 'bar', 'foobar', 'barfoo', 'example'], two=['bar', 'example', 'foobar'], three=['foo', 'example']) i'd like to replace all the occurrences of "example" by "someotherword". While I can already think of a f...

How can you emulate a mailing list in Django?

I'm on a project which is trying to write what amounts to a Mailing List app in Django, and we're running into a couple of problems/questions. The code we have so far doesn't set various List headers, and re-sets the To header to be the person we're sending it to, instead of the list address. Now, we can work our way through all these ...

Is there a Python library function which attempts to guess the character-encoding of some bytes?

I'm writing some mail-processing software in Python that is encountering strange bytes in header fields. I suspect this is just malformed mail; the message itself claims to be us-ascii, so I don't think there is a true encoding, but I'd like to get out a unicode string approximating the original one without throwing a UnicodeDecodeError...

What mime-type should I return for a python string

I have a web API that returns python dictionaries or lists as a response that I eval() in python scripts that use the API, for completness I wanted to set a proper content-type but not sure what would be best to use "text/x-python" or maybe "application/python", or something else? [edit] I'm also outputting JSON, I'm doing Python as an...

How can I Cause a Deadlock in MySQL for Testing Purposes

I want to make my Python library working with MySQLdb be able to detect deadlocks and try again. I believe I've coded a good solution, and now I want to test it. Any ideas for the simplest queries I could run using MySQLdb to create a deadlock condition would be? system info: MySQL 5.0.19 Client 5.1.11 Windows XP Python 2.4 / MySQ...

Using Python to authenticate against raw username, hash, salt in DB created by ASP.NET roles/membership

Hello all. We have a current application where user login credentials are stored in a SQL Server DB. These are, basically, stored as a plain text username, a password hash, and an associated salt for this hash. These were all created by built in functions in ASP.NET's membership/role system. Here's a row for a user named 'joe' and a ...

How do I find the location of Python module sources?

How do I learn where the source file for a given Python module is installed? (Is the method is different on Windows than Linux?) (I want to look at the datetime module sources, but I thought I'd ask a more general question.) ...

C++ string diff (a la Python's difflib)

I'm trying to diff two strings to determine whether or not they solely vary in one numerical subset of the string structure; for example, varies_in_single_number_field('foo7bar', 'foo123bar') # Returns True, because 7 != 123, and there's only one varying # number region between the two strings. In Python I can use the difflib to accom...

DTD Validation With Python?

I was wondering which of the built in python libraries, support DTD validation. I've been looking around and haven't been able to get a straight answer. I know ElementTree does NOT Support DTD validation. Which ones do? ...

TkInter Invoke Event in Main Loop

How do you invoke a tKInter event from a separate object? I'm looking for something like wxWidgets wx.CallAfter. For example, If I create a child object and pass it my root Tkinter.Tk() and then try to call a method of that root window from the child object my app locks up. The best I can come up with is to use the .after method and...

How do I determine all of my IP addresses when I have multiple NICs?

I have multiple Network Interface Cards on my computer, each with its own IP address. When I use gethostbyname(gethostname()) from Python's (built-in) socket module, it will only return one of them. How do I get the others? ...

Efficiently updating database using SQLAlchemy ORM

I'm starting a new application and looking at using an ORM -- in particular, SQLAlchemy. Say I've got a column 'foo' in my database and I want to increment it. In straight sqlite, this is easy: db = sqlite3.connect('mydata.sqlitedb') cur = db.cursor() cur.execute('update table stuff set foo = foo + 1') I figured out the SQLAlchemy S...

How can I apply authenticated proxy exceptions to an opener using urllib2?

When using urllib2 (and maybe urllib) on windows python seems to magically pick up the authenticated proxy setting applied to InternetExplorer. However, it doesn't seem to check and process the Advance setting "Exceptions" list. Is there a way I can get it to process the exceptions list? Or, ignore the IE proxy setting and apply my ow...

Django - How to do tuple unpacking in a template 'for' loop

In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this: [ Product_Type_1, [ product_1, product_2 ], Product_Type_2, [ product_3, product_4 ]] In plain old Python, I could iteration the list like this: for product_type, products in list: print product_type for product...

Linking languages

Hi, I asked a question earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the interface bits. So, this leads me on to another question. How easy is it to link these languages togethe...

Interactive console using Pydev in Eclipse?

I'm debugging my Python code in Eclipse using the Pydev plugin. I'm able to open a Pydev console and it gives me two options: "Console for currently active editor" and "Python console". However none of them is useful to inspect current variable status after a breakpoint. For example, the code stopped at a breakpoint and I want to inspec...