python

Implementing a "[command] [action] [parameter]" style command-line interfaces?

What is the "cleanest" way to implement an command-line UI, similar to git's, for example: git push origin/master git remote add origin git://example.com master Ideally also allowing the more flexible parsing, for example, jump_to_folder app theappname v2 jump_to_folder app theappname source jump_to_folder app theappname source v2 ju...

Serializing a Python object to/from a S60 phone

I'm looking for a way to serialize generic Python objects between a CherryPy-based server and a Python client running on a Symbian phone.. Since pyS60 doesn't implement the pickle module, how would you do it? I know about Cerealizer but it requires you to register classes before use (which I'd like to avoid) and doesn't look very mature...

How do I add a guard ring to a matrix in NumPy?

Using NumPy, a matrix A has n rows and m columns, and I want add a guard ring to matrix A. That guard ring is all zero. What should I do? Use Reshape? But the element is not enough to make a n+1 m+1 matrix. Or etc.? Thanks in advance I mean an extra ring of cells that always contain 0 surround matrix A.Basically there is a Matrix B h...

Switching from python-mode.el to python.el

I recently tried switching from using python-mode.el to python.el for editing python files in emacs, found the experience a little alien and unproductive, and scurried back. I've been using python-mode.el for something like ten years, so perhaps I'm a little set in my ways. I'd be interested in hearing from anyone who's carefully evalu...

django is very slow on my machine

I have a fresh install of django 1.0 and a simple page served from it takes 5 secs to load. On my colleague's computer it takes almost no time. I start the server using python manage.py testserver I can see each GET request (PNGs and style sheets) take about half a second. Another weird thing, which I think is related, is that the...

Best way to organize the folders containing the SQLAlchemy models

I use SQLAlchemy at work and it does the job really fine. Now I am thinking about best practices. For now, I create a module holding all the SQLA stuff : my_model |__ __init__.py |__ _config.py <<<<< contains LOGIN, HOST, and a MetaData instance |__ table1.py <<<<< contains the class, the model and the mappe...

Regex Problem Group Name Redefinition?

So I have this regex: (^(\s+)?(?P<NAME>(\w)(\d{7}))((01f\.foo)|(\.bar|\.goo\.moo\.roo))$|(^(\s+)?(?P<NAME2>R1_\d{6}_\d{6}_)((01f\.foo)|(\.bar|\.goo\.moo\.roo))$)) Now if I try and do a match against this: B048661501f.foo I get this error: File "C:\Python25\lib\re.py", line 188, in compile return _compile(pattern, flags) ...

Equivalent of an HTML multiple SELECT box in wxPython

I'd like to create a ListBox in wxPython with the same semantics as a multiple select box in HTML. Specifically I'd like the following semantics When the user clicks on an entry in the list, all other entries become de-selected and the clicked entry becomes selected. If the entry was already selected then it stays selected. Wh...

Python idiom to return first item or None

I'm sure there's a simpler way of doing this that's just not occurring to me. I'm calling a bunch of methods that return a list. The list may be empty. If the list is non-empty, I want to return the first item; otherwise, I want to return None. This code works: list = get_list() if len(list) > 0: return list[0] return None It seem...

Best way to monitor services on a few servers with python

What would be the best way to monitor services like HTTP/FTP/IMAP/POP3/SMTP for a few servers from python? Using sockets and trying to connect to service port http-80, ftp-21, etc... and if connection successful assume service is ok or use python libs to connect to specified services and handle exceptions/return codes/etc... For example...

MVC and django fundamentals

Pretty new to this scene and trying to find some documentation to adopt best practices. We're building a fairly large content site which will consist of various media catalogs and I'm trying to find some comparable data / architectural models so that we can get a better idea of the approach we should use using a framework we've never ma...

Markup-based GUI for python

I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them. I think GUI design should be done in a separate text-based file in some markup fo...

Threaded code on mod_python

I have written a Django app that makes use of Python threading to create a web spider, the spider operates as a series of threads to check links. When I run this app using the django test server (built in), the app runs fine and the threads seem to start and stop on time. However, running the app on Apache it seems the threads aren't k...

Which Eclipse distribution is good for web development using Python, PHP, or Perl?

I'd like to try out Eclipse, but I'm a bit baffled with all the different distributions of it. I mainly program in Python doing web development, but I also need to maintain PHP and Perl apps. It looks like EasyEclipse is a bit behind. Should I just grab the base Eclipse and start loading plug-ins? ...

In Python, how to I iterate over a dictionary in sorted order?

There's an existing function that ends in: return dict.iteritems() that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that? ...

Python - get position in list

I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this? Example: testlist = [1,2,3,5,3,1,2,1,6] for item in testlist: if item == 1: print position ...

Which blog engines, written in Python, do you recommend?

Even though I'm quite happy with WordPress, I'm mainly coding in Python nowadays, and wanted to eat my own dog food, ie improve my Python knowledge by studying the source code and contributing with plugins (if the engine allows it). After searching, I've already encountered Byteflow and PyBlosxom. Byteflow works using Django, which for...

How do I use my standard python path when running python scripts from xcode macros

I'm trying to run Python scripts using Xcode's User Scripts menu. The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which means I can't do things like #!/...

generator comprehension

May i just a quick question.what does generator comprehension do?And how does it work?i couldnt find a tutorial about it. Thanks in advance ...

Detect windows logout in Python

How can I detect, or be notified, when windows is logging out in python? Edit: Martin v. Löwis' answer is good, and works for a full logout but it does not work for a 'fast user switching' event like pressing win+L which is what I really need it for. Edit: im not using a gui this is running as a service ...