python

Traversing foreign key related tables in django templates

View categories = Category.objects.all() t = loader.get_template('index.html') v = Context({ 'categories': categories }) return HttpResponse(t.render(v)) Template {% for category in categories %} <h1>{{ category.name }}</h1> {% endfor %} this works great. now im trying to print each company in that category. the company table...

Logging All Exceptions in a pyqt4 app

What's the best way to log all of the exceptions in a pyqt4 application using the standard python logging api? I've tried wrapping exec_() in a try, except block, and logging the exceptions from that, but it only logs exceptions from the initialization of the app. As a temporary solution, I wrapped the most important methods in try, ex...

Is it possible to override the method used to call Django's admin delete confirmation page?

On Django's admin pages, I'd like to perform an action when the administrator clicks the Delete button for an object. In other words, I'd like to execute some code prior to arriving on the "Are you sure?" delete confirmation page. I realize I could override the template page for this object, but I was hoping for something easier (i.e.,...

Running Python code contained in a string

I'm writing a game engine using pygame and box2d, and in the character builder, I want to be able to write the code that will be executed on keydown events. My plan was to have a text editor in the character builder that let you write code similar to: if key == K_a: ## Move left pass elif key == K_d: ## Move right pass ...

Python: Bind an Unbound Method?

In Python, is there a way to bind an unbound method without calling it? I am writing a wxPython program, and for a certain class I decided it'd be nice to group the data of all of my buttons together as a class-level list of tuples, like so: class MyWidget(wx.Window): buttons = [("OK", OnOK), ("Cancel", OnCancel)] ...

Continuous unit testing with Pydev (Python and Eclipse)

Is there a way to integrate background unit tests with the Pydev Eclipse environment? My unit tests run well, but I would like to integrate them to run in the background based on source file changes (e.g. with nose) and to integrate the result back to Eclipse (I'm thinking big red X when tests fail with a console and trace log view). N...

Why is `self` in Python objects immutable?

Why can't I perform an action like the following: class Test(object): def __init__(self): self = 5 t = Test() print t I would expect it to print 5 since we're overwriting the instance with it, but instead it doesn't do anything at all. Doesn't even throw an error. Just ignores the assignment. I understand that there woul...

How do I compile Python C extensions using MinGW inside a virtualenv?

When using virtualenv in combination with the MinGW compiler on Windows, compiling a C extension results in the following error: C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot find -lpython25 collect2: ld returned 1 exit status error: Setup script exited with error: command 'gcc' failed with exit status ...

Passing Formatted Text Through XSLT

I have formatted text (with newlines, tabs, etc.) coming in from a Telnet connection. I have a python script that manages the Telnet connection and embeds the Telnet response in XML that then gets passed through an XSLT transform. How do I pass that XML through the transform without losing the original formatting? I have access to the...

python importing relative modules

hi, I have the Python modules a.py and b.py in the same directory. How can I reliably import b.py from a.py, given that a.py may have been imported from another directory or executed directly? This module will be distributed so I can't hardcode a single path. I've been playing around with __file__, sys.path and os.chdir, but it feels m...

Synthesis of general programming language (Python) with tailored language (PureData/MaxMSP/ChucK)

I am learning Python because it appeals to me as a mathematician but also has many useful libraries for scientific computing, image processing, web apps, etc etc. It is frustrating to me that for certain of my interests (eletronic music or installation art) there are very specific programming languages which seem better suited to thes...

Cross-platform subprocess with hidden window

I want to open a process in the background and interact with it, but this process should be invisible in both Linux and Windows. In Windows you have to do some stuff with STARTUPINFO, while this isn't valid in Linux: ValueError: startupinfo is only supported on Windows platforms Is there a simpler way than creating a separate Pope...

finding firefox version

How to find Firefox version using python? ...

How to use cookielib with httplib in python?

In python, I'm using httplib because it "keep-alive" the http connection (as oppose to urllib(2)). Now, I want to use cookielib with httplib but they seem to hate each other!! (no way to interface them together). Does anyone know of a solution to that problem? ...

What to do with "Unexpected indent" in python?

How do I rectify the error "unexpected indent" in python? ...

django development add-ons

I have come across various django development add ons, particularly, django-extensions django-annoying django-debug-toolbar django-tools I haven't exactly used all of these. I think it is hard to beat the simplicity and power obtained by the combination of django's pretty error pages combined with iPythonEmbed shell. Which of the...

Generate from generators

I have a generator that takes a number as an argument and yields other numbers. I want to use the numbers yielded by this generator and pass them as arguments to the same generator, creating a chain of some length. For example, mygenerator(2) yields 5, 4 and 6. Apply mygenerator to each of these numbers, over and over again to the numbe...

Python cgi performance

I own a legacy python application written as CGI. Until now this works OK, but the number of concurrent users will increment largely in the very near future. Here on SO I read: "CGI is great for low-traffic websites, but it has some performance problems for anything else". I know it would have been better to start in another way, but CGI...

UnicodeDecodeError when reading dictionary words file with simple Python script

First time doing Python in a while, and I'm having trouble doing a simple scan of a file when I run the following script with Python 3.0.1, with open("/usr/share/dict/words", 'r') as f: for line in f: pass I get this exception: Traceback (most recent call last): File "/home/matt/install/test.py", line 2, in <module> f...

simple update in sqlalchemy

Hello all, UserTable is: id (INT) name (STR) last_login (DATETIME) Serving a web page request i have a user id in hand and I only wish to update the last_login field to 'now'. It seems to me that there are 2 ways: issue a direct SQL using db_engine (losing the mapper) OR query the user first and then update the object Both work...