python

How do I write a python HTTP server to listen on multiple ports?

I'm writing a small web server in Python, using BaseHTTPServer and a custom subclass of BaseHTTPServer.BaseHTTPRequestHandler. Is it possible to make this listen on more than one port? What I'm doing now: class MyRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): def doGET [...] class ThreadingHTTPServer(ThreadingMixIn, HTTPSe...

improve my python regex!

What follows is a regular expression I have written to match multi-line pre-processor macros in C / C++ code. I'm by no means a regular expressions guru, so I'd welcome any advice on how I can make this better. Here's the regex: ^\s*#define(.*\\\n)+[\S]+(?!\\) It should match all of this: #define foo(x) if(x) \ doSomething(x) But ...

Poll: Which Python IDE/editor is the best?

I am just getting into Python development and would like to know which single IDE or editor is the best. I mainly use Linux, but don't let that stop you if you think the best one is Win/Mac only. ...

Getting random row through SQLAlchemy

How do I select a(or some) random row(s) from a table using SQLAlchemy? ...

How do you retrieve items from a dictionary in the order that they're inserted?

Is it possible to retrieve items from a Python dictionary in the order that they were inserted? ...

Where do the Python unit tests go?

If you're writing a library, or an app, where do the unit test files go? It's nice to separate the test files from the main app code, but it's awkward to put them into a "tests" subdirectory inside of the app root directory, because it makes it harder to import the modules that you'll be testing. Is there a best practice here? ...

Best Practices for Architecting Large Systems in a Dynamic Language

From my experiences building non-trivial applications in Java and C#, I know that using good modular design based on known patterns and "coding to interfaces" are keys to success. What are the architecture best practices when building large systems in a dynamic language like python or ruby? ...

Python dictionary from an object's fields

Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this: >>> class Foo: ... bar = 'hello' ... baz = 'world' ... >>> f = Foo() >>> props(f) { 'bar' : 'hello', 'baz' : 'world' } NOTE: It should not include methods. Only fields. Thanks ...

Is it pythonic for a function to return multiple values?

In python, you can have a function return multiple values. Here's a contrived example: def divide(x, y): quotient = x/y remainder = x % y return quotient, remainder (q, r) = divide(22, 7) This seems very useful, but it looks like it can also be abused ("Well..function X already computes what we need as an intermediate ...

python cgi on IIS

How do you set up IIS so that you can call python scripts from asp pages? Ok, so I found the answer to that question here: http://support.microsoft.com/kb/276494 So on to my next question: How do you call a cgi script from within classic asp (vb) code? Particularly one which is not in the web root directory. ...

What's a good way to find relative paths in Google App Engine?

So I've done the trivial "warmup" apps with GAE. Now I'd like to build something with a more complex directory structure. Something along the lines of: siteroot/ models/ controllers/ controller1/ controller2/ ... templates/ template1/ template2/ ... ..etc. The controllers will be Python modules handling requests. Th...

The best Django webcasts/videos

I'm currently learning Django though reading the Django Book, but I'm a huge fan of webcasts/screencasts/videos and haven't found any good ones so far. Are there any and which ones would you recommend? ...

Comparing runtimes

I am trying to get some accurate runtime comparisons of PHP vs Python (and potentially any other language that I have to include). Timing within a script is not my problem but timing within a script does not account for everything from the moment the request is made to run the script to output. 1) Is it actually worth taking such things...

Python vs PHP, Python runs slower?

I've heard that Python is meant to be faster than PHP in terms of Runtime, I simply took this as a given and sat down today to make a blog post about it. After being told by Vinko Vrsalovic how to time scripts I took converted some code for getting prime numbers into Python and PHP then ran each 3 times and recorded the numbers. All time...

Ignore case in Python strings

What is the easiest way to compare strings in Python, ignoring case? Of course one can do (str1.lower() <= str2.lower()), etc., but this created two additional temporary strings (with the obvious alloc/g-c overheads). I guess I'm looking for an equivalent to C's stricmp(). [Some more context requested, so I'll demonstrate with a trivi...

Is there a way around coding in Python without the tab, indent & whitespace criteria?

I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off? I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting? ...

Which is the most useful Mercurial hook for programming in a loosely connected team?

I recently discovered the notify extension in Mercurial which allows me quickly send out emails whenever I push changes, but I'm pretty sure I'm still missing out on a lot of functionality which could make my live a lot easier. notify-extension: http://www.selenic.com/mercurial/wiki/index.cgi/NotifyExtension Which Mercurial hook or ...

How create threads under Python for Delphi

I'm hosting Python script with Python for Delphi components inside my Delphi application. I'd like to create background tasks which keep running by script. Is it possible to create threads which keep running even if the script execution ends (but not the host process, which keeps going on). I've noticed that the program gets stuck if t...

Classes in Python

In Python is there any way to make a class, then make a second version of that class with identical dat,a but which can be changed, then reverted to be the same as the data in the original class? So I would make a class with the numbers 1 to 5 as the data in it, then make a second class with the same names for sections (or very similar...

Nice Python wrapper for Yahoo's Geoplanet web service?

Has anybody created a nice wrapper around Yahoo's geo webservice "GeoPlanet" yet? ...