python

How can I convert a Perl regex with named groups to Python?

I am trying to convert the following Perl regex I found in the Video::Filename Perl module to a Python 2.5.4 regex to parse a filename # Perl > v5.10 re => '^(?:(?<name>.*?)[\/\s._-]*)?(?<openb>\[)?(?<season>\d{1,2})[x\/](?<episode>\d{1,2})(?:-(?:\k<season>x)?(?<endep>\d{1,2}))?(?(<openb>)\])(?:[\s._-]*(?<epname>[^\/]+?))?$', I would ...

Testing for ImportErrors in Python

We're having a real problem with people checking in code that doesn't work because something's been refactored. Admittedly, this is partly because our developers don't really have any good tools for finding these kinds of mistakes easily. Are there any tools to help find ImportErrors in Python? Of course, the correct answer here is "y...

psycopg2 on OSX: do I have to install PostgreSQL too?

Hello, I want to access a postgreSQL database that's running on a remote machine, from Python in OS/X. Do I have to install postgres on the mac as well? Or will psycopg2 work on its own. Any hints for a good installation guide for psycopg2 for os/x? ...

Is there any way to create a "project file" in emacs?

I say "project file" in the loosest sense. I have a few python projects that I work on with ropemacs using emacs W32 for Windows. What would be ideal is if I could have an icon I could click on on my desktop to open up emacs, open up the rope project, and set the speed bar in the top-level directory of that project. Then I could also ...

Can you access a subbed-classed model from within the super-class model in the Django ORM?

Lets say I have model inheritance set up in the way defined below. class ArticleBase(models.Model): title = models.CharField() author = models.CharField() class Review(ArticleBase): rating = models.IntegerField() class News(ArticleBase): source = models.CharField() If I need a list of all articles regardless of type ...

How do I convert a Python float to a hexadecimal string in python 2.5? Nonworking solution attached.

What I really need to do is to export a floating point number to C with no precision loss. I did this in python: import math import struct x = math.sqrt(2) print struct.unpack('ii', struct.pack('d', x)) # prints (1719614413, 1073127582) And in C I try this: #include <math.h> #include <stdio.h> int main(void) { unsigned long long ...

Modeling a complex relationship in Django

I'm working on a Web service in Django, and I need to model a very specific, complex relationship which I just can't be able to solve. Imagine three general models, let's call them Site, Category and Item. Each Site contains one or several Categories, but it can relate to them in one of two possible ways: one are "common" categories, wh...

Regex and a sequences of patterns?

Is there a way to match a pattern (e\d\d) several times, capturing each one into a group? For example, given the string.. blah.s01e24e25 ..I wish to get four groups: 1 -> blah 2 -> 01 3 -> 24 4 -> 25 The obvious regex to use is (in Python regex: import re re.match("(\w+).s(\d+)e(\d+)e(\d+)", "blah.s01e24e25").groups() ..but I al...

What does `@` mean in Python?

like @login_required? ...

Changing python interpreter windows

Hi guys. I have two python installations, 2.5 and 2.6 I want to change the default python interpreter from 2.5 to 2.6. Anyone know how? ...

Why print statement is not pythonic?

This question was bugging me for quite a while (as evidenced by my previous question): why exactly is print(x) better (which is defined as being more pythonic) than print x? For those who don't know, the print statement was changed into function in Python 3.0. The formal documentation is in PEP 3105 and motivation is in Guido van Rossum...

Python Numpy Very Large Matrices

Numpy is an extremely useful library, and from using it I've found that it's capable of handling matrices which are quite large (10000x10000) easily, but begins to struggle with anything much larger (trying to create a matrix of 50000x50000 fails). Obviously, this is because of the massive memory requirements. I was wondering if there ...

With Python unittest, how do I create and use a "callable object that returns a test suite"?

I'm learning Python and have been trying to understand more about the details of Python's unittest module. The documentation includes the following: For the ease of running tests, as we will see later, it is a good idea to provide in each test module a callable object that returns a pre-built test suite: def suite(): sui...

printing bit representation of numbers in python

Hi I want to print the bit representation of numbers onto console, so that I can see all operations that are being done on bits itself. How can I possibly do it in python? Thanks ...

Python Implementation of RubyonRails UrlEncodedPairParser / Rack::Utils.parse_nested_query

Does anyone know of a python implementation of RubyonRails UrlEncodedPairParser or Rack::Utils.parse_nested_query? I think I remember seeing a version in one of the python frameworks (not django) but can't seem to find it? ...

formencode invalid return type

if an exception occurs in form encode then what will be the return type?? suppose if(request.POST): formvalidate = ValidationRule() try: new = formvalidate.to_python(request.POST) data = Users1( n_date = new['n_date'], heading = new['heading'], desc = new['desc'], link...

How frequently should Python decorators be used?

I recently started experimenting with Python decorators (and higher-order functions) because it looked like they might make my Django unit tests more concise. e.g., instead of writing: def visit1(): login() do_stuff() logout() I could instead do @handle_login def visit1(): do_stuff() However, after some experimentin...

How to import a Python class that is in a directory above?

For example, I want to inherit from a class in a file that lies in a directory above the current one. Is it possible to relatively import that file? thanks! ...

How can you read keystrokes when the python program isn't in the foreground?

I'm trying to analyze my keystrokes over the next month and would like to throw together a simple program to do so. I don't want to exactly log the commands but simply generate general statistics on my key presses. I am the most comfortable coding this in python, but am open to other suggestions. Is this possible, and if so what python...

compound sorting in python

I have a python script which outputs lots of data, sample is as below. the first of the 4 fields always consists of two letters, one digit, a slash and one or two digits Gi3/2 --.--.--.-- 0024.e89b.c10e Dell Inc. Gi5/4 --.--.--.-- 0030.c1cd.f038 HEWLETTPACKARD Gi4/3 --.--.--.-- 0020.ac00.6703 INTERFLEX DATENSYSTEME GMBH Gi3/7 --....