python

Passing a pre_delete() or post_delete() signal arguments ?

I am using signals to perform an action after an object has been deleted; however, sometimes I want to perform a different action (not the default one) depending on an arugment. Is there a way to pass an argument to my signal catcher? Or will I have to abandon the signal and instead hard code what I want to do in the models ? What I...

Killing Python webservers

I am looking for a simple Python webserver that is easy to kill from within code. Right now, I'm playing with Bottle, but I can't find any way at all to kill it in code. If you know how to kill Bottle (in code, no Ctrl+C) that would be super, but I'll take anything that's Python, simple, and killable. ...

Grouping data on year

mydata = [{'date': datetime.datetime(2009, 1, 31, 0, 0), 'value': 14, 'year': u'2009'}, {'date': datetime.datetime(2009, 2, 28, 0, 0), 'value': 84, 'year': u'2009'}, {'date': datetime.datetime(2009, 3, 31, 0, 0), 'value': 77, 'year': u'2009'}, {'date': datetime.datetime(2009, 4, 30, 0, 0), 'value': 80, 'y...

Customising Install location for Django (or any Python module)

I'd like to to install Django into a custom location, I've read the distutils documentation and it suggests that I should be able to do something like the following to install under my home directory (when run from an unpacked django tarball). > python setup.py install --home=~/code/packages/install --install-purelib=modules --install-p...

How to test for multiple command line arguments (sys.argv

Hi, I want to test againts multiple command line arguments in a loop > python Read_xls_files.py group1 group2 group3 No this code tests only for the first one (group1). hlo = [] for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value == sys.argv[1]: hlo.append(sh.cell(i, 8).value) How should I modify this that I ca...

Forward slash in a Python regex

I'm trying to use a Python regex to find a mathematical expression in a string. The problem is that the forward slash seems to do something unexpected. I'd have thought that [\w\d\s+-/*]* would work for finding math expressions, but it finds commas too for some reason. A bit of experimenting reveals that forward slashes are the culprit. ...

Django tagging migration to GAE

I have a Django app that use a django-tagging. I need to port this application to GAE. So, the main problem is to migrate tagging part. It has a complicated model, that should be rewritten to work with Google store. I think tagging is very popular django app and someone has the same problem before. Has someone a rewritten model? ...

Is there any way to use a strftime-like function for dates before 1900 in Python?

I didn't realize this, but apparently Python's strftime function doesn't support dates before 1900: >>> from datetime import datetime >>> d = datetime(1899, 1, 1) >>> d.strftime('%Y-%m-%d') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: year=1899 is before 1900; the datetime strftime() methods requi...

best way to parse a line in python to a dictionary

I have a file with lines like account = "TEST1" Qty=100 price = 20.11 subject="some value" values="3=this, 4=that" There is no special delimiter and each key has a value that is surrounded by double quotes if its a string but not if it is a number. There is no key without a value though there may exist blank strings which are represen...

Unable to find solution for a practice problem in codechef

Here is the problem from code chef : A set of N dignitaries have arrived in close succession at Delhi and are awaiting transportation to Roorkee to participate in the inaugural ceremony of Cognizance. Being big sponsors of Cognizance, it has been deemed unsuitable by the organizing team to arrange for more than one dignitary to travel ...

How do I raise an http error/exception from a python CGI script?

How do I raise an http error/exception from a python CGI script? Is all that is necessary is to print the appropriate header: print '''Status: 501 Not Implemented Content-type: text/html ''' That doesn't seem to work right. I have a very basic setup, namely IIS7 routing *.py CGI scripts to python25.exe for execution. I am not using...

CPython is bytecode interpreter?

I don't really get the concept of "bytecode interpreter" in the context of CPython. Can someone shed some light over the whole picture? Does it mean that CPython will compile and execute pyc file (bytecode file?). Then what compile py file to pyc file? And how is Jython different from CPython (except they are implemented in different la...

How to handle Unicode (non-ASCII) characters in Python?

I'm programming in Python and I'm obtaining information from a web page through the urllib2 library. The problem is that that page can provide me with non-ASCII characters, like 'ñ', 'á', etc. In the very moment urllib2 gets this character, it provokes an exception, like this: (more stack trace) File "c:\Python25\lib\httplib.py", line ...

Close all opened xml tags

Hello, I have a file, which change it content in a short time. But I'd like to read it before it is ready. The problem is, that it is an xml-file (log). So when you read it, it could be, that not all tags are closed. I would like to know if there is a possibility to close all opened tags correctly, that there are no problems to show it...

Trace Table for Python Programs

Is there a way to get the trace table for a Python program? Or for a program to run another program and get its trace table? I'm a teacher trying to flawlessly verify the answers to the tracing problems that we use on our tests. So, for example, assuming I have a Python program named problem1.py with the following content: problem1.py ...

Concurrency control in Django model

How do I handdle concurrency in a Django model? I dont want the changes to the record being overwritten by another user who reads the same record ...

Django db reset without loading fixtures

Is there an easy way to reset a django database (i.e. drop all data/tables, create new tables and create indexes) without loading fixture data afterwords? What I want to have is just an empty database because all data is loaded from another source (a kind of a post-processed backup). I know that this could be achieved by piping the out...

insert string in the middle of a file given a file object

I am working on a problem and got stuck at a wall I have a (potentially large) set of text files, and I need to apply a sequence of filters and transformations to it and export it to some other places. so I roughly have def apply_filter_transformer(basepath = None, newpath = None, fts= None): #because all the raw studies in basepa...

How this keyword is provided for object instances in C#?

When you have an object instance in C#, you can use the this keyword inside the instance scope. How does the compiler handles it? Is there any assistance for this at runtime? I am mainly wondering how C# does it vs in python you have to provide self for every function manually. ...

better version of fakemodal?

I found a code snippet (http://aspn.activestate.com/ASPN/Mail/Message/wxpython-users/3695248) for a "fakemodal" dialog. This works for what I am doing, but I would like to make it act more like a modal dialog. My problem - I have a main window and a 2nd dialog window. The 2nd dialog is non-modal. I would like to open a sub-window of ...