python

Use Python 2.6 subprocess module in Python 2.5

I would like to use Python 2.6's version of subprocess, because it allows the Popen.terminate() function, but I'm stuck with Python 2.5. Is there some reasonably clean way to use the newer version of the module in my 2.5 code? Some sort of from __future__ import subprocess_module? ...

Django Model returning NoneType

I have a model Product it has two fields size & colours among others colours = models.CharField(blank=True, null=True, max_length=500) size = models.CharField(blank=True, null=True, max_length=500) In my view I have current_product = Product.objects.get(slug=title) if len(current_product.size) != 0 : current_product.size = cur...

python ide vs cmd line detection?

When programming in the two IDEs i used, bad things happen when i use raw_input. However on the command line it works EXACTLY how i expect it to. Typically this app is ran in cmd line but i like to edit and debug it in an IDE. Is there a way to detect if i executed the app in an IDE or not? ...

How do I profile memory usage in Python?

I've recently become interested in algorithms and have begun exploring them by writing a naive implementation and then optimizing it in various ways. I'm already familiar with the standard Python module for profiling runtime (for most things I've found the timeit magic function in IPython to be sufficient), but I'm also interested in me...

python exit a blocking thread?

in my code i loop though raw_input() to see if the user has requested to quit. My app can quit before the user quits, but my problem is the app is still alive until i enter a key to return from the blocking function raw_input(). Can i do to force raw_input() to return? by maybe sending it fake input? could i terminate the thread its on? ...

python excel making reports

I've been given the task of connecting my code (fortran) with py (2.5), and generate a number of excel reports if possible. The first part went fine - and is done with, but now I'm working on the second. What are my choices for making excel (2007 if possible) sheets from python ? In general, I would need to put some array values in t...

Generate a random date between two other dates.

How would I generate a random date that has to be between two other given dates? The functions signature should something like this- randomDate("1/1/2008 1:30 PM", "1/1/2009 4:50 AM", 0.34) ^ ^ ^ date generated has date generated has random number to be after th...

Windows Server 2008 or Vista?

What is an easy (to implement) way to check whether I am on Windows Vista or Windows Server 2008 from a Python script? platform.uname() gives the same result for both versions. ...

Can you use a string to instantiate a class in python?

I'm using a builder pattern to seperate a bunch of different configuration possibilities. Basically, I have a bunch of classes that are named an ID (something like ID12345). These all inherit from the base builder class. In my script, I need to instantiate an instance for each class (about 50) every time this app runs. So, I'm trying to ...

Can anyone provide a more pythonic way of generating the morris sequence?

I'm trying to generate the morris sequence in python. My current solution is below, but I feel like I just wrote c in python. Can anyone provide a more pythonic solution? def morris(x): a = ['1', '11'] yield a[0] yield a[1] while len(a) <= x: s = '' count = 1 al = a[-1] for i in range(0,le...

How do I prevent Python's urllib(2) from following a redirect

I am currently trying to log into a site using Python however the site seems to be sending a cookie and a redirect statement on the same page. Python seems to be following that redirect thus preventing me from reading the cookie send by the login page. How do I prevent Python's urllib (or urllib2) urlopen from following the redirect? ...

Stackless python network performance degrading over time?

So i'm toying around with stackless python, writing a very simple webserver to teach myself programming with microthreads/tasklets. But now to my problem, when I run something like "ab -n 100000 -c 50 http://192.168.0.192/" (100k requests, 50 concurrency) in apache bench I get something like 6k req/s, the second time I run it I get 5.5k,...

Parameterised regular expression in Python

In Python, is there a better way to parameterise strings into regular expressions than doing it manually like this: test = 'flobalob' names = ['a', 'b', 'c'] for name in names: regexp = "%s" % (name) print regexp, re.search(regexp, test) This noddy example tries to match each name in turn. I know there's better ways of doing t...

Euler problem number #4

Using Python, I am trying to solve problem #4 of the Project Euler problems. Can someone please tell me what I am doing incorrectly? The problem is to Find the largest palindrome made from the product of two 3-digit numbers. Here is what I have thus far. import math def main(): for z in range(100, 1000): for y in range(100,...

Multiple output files

edit: Initially I was trying to be general but it came out vague. I've included more detail below. I'm writing a script that pulls in data from two large CSV files, one of people's schedules and the other of information about their schedules. The data is mined and combined to eventually create pajek format graphs for Monday-Sat of peop...

Match series of (non-nested) balanced parentheses at end of string

How can I match one or more parenthetical expressions appearing at the end of string? Input: 'hello (i) (m:foo)' Desired output: ['i', 'm:foo'] Intended for a python script. Paren marks cannot appear inside of each other (no nesting), and the parenthetical expressions may be separated by whitespace. It's harder than it might seem...

Getting a list of all modules in the current package

Here's what I want to do: I want to build a test suite that's organized into packages like tests.ui, tests.text, tests.fileio, etc. In each __init__.py in these packages, I want to make a test suite consisting of all the tests in all the modules in that package. Of course, getting all the tests can be done with unittest.TestLoader, bu...

SQLAlchemy/Elixir validation rules?

I just found out how to validate my database input before saving it, but I'm kinda bummed to find there are no premade rules (like validate email, length, etc) that are found in some web based frameworks. Are there any validation libraries laying around anywhere or somewhere that some premade validation lists are hiding that I haven't fo...

Character Translation using Python (like the tr command)

Is there a way to do character translation (kind of like the tr command) using python ...

ImportError: No module named copy_reg pickle

I'm trying to unpickle an object stored as a blob in a MySQL database. I've manually generated and stored the pickled object in the database, but when I try to unpickle the object, I get the following rather cryptic exception: ImportError: No module named copy_reg Any ideas as to why this happens? Method of Reproduction Note: Must do...