python

How to run one last function before getting killed in Python?

Is there any way to run one last command before a running Python script is stopped by being killed by some other script, keyboard interrupt etc. Thanks for your help! ...

Python AppEngine; get user info and post parameters?

Im checking the examples google gives on how to start using python; especifically the code posted here; http://code.google.com/appengine/docs/python/gettingstarted/usingdatastore.html The thing that i want to lean is that, here: class Guestbook(webapp.RequestHandler): def post(self): greeting = Greeting() if users.get_curren...

Django Shell shortcut in Windows

I'm trying to write a bat file so I can quickly launch into the Interactive Shell for one of my Django projects. Basically I need to write a python script that can launch "manage.py shell" and then be able to print from mysite.myapp.models import * The problem is manage.py shell cannot take additional arguments and launching into "mana...

Python & parsing IRC messages

What's the best way to parse messages received from an IRC server with Python according to the RFC? I simply want some kind of list/whatever, for example: :[email protected] PRIVMSG #channel :Hi! becomes this: { "sender" : "[email protected]", "target" : "#channel", "message" : "Hi!" } And so on? (Edit: I want to parse IRC mes...

Python regex - conditional matching?

I don't know if that's the right word for it, but I am trying to come up with some regex's that can extract coefficients and exponents from a mathematical expression. The expression will come in the form 'axB+cxD+exF' where the lower case letters are the coefficients and the uppercase letters are the exponents. I have a regex that can ...

Referencing another project

In a simple program I made, I wanted to get a list from another project and access the elements from it. Since I'm new to python, I don't really have any idea what to do. In my project, I checked the box for the project name I wanted to reference and... I don't know what to do. A few google searched did me no good, so I'm hoping someone ...

how to sort by a computed value in django

Hey I want to sort objects based on a computed value in django... how do I do it? Here is an example User profile model based on stack overflow that explains my predicament: class Profile(models.Model): user = models.ForeignKey(User) def get_reputation(): ... return reputation reputation = property(get_rep...

assertEquals vs. assertEqual in python

Is there a difference between assertEquals and assertEqual in the python unittest.TestCase. And if it is not why are there two functions? Only for convenience? ...

Reverse a string in Python

There is no built in "reverse" function in Python's str object. What is the best way of implementing this? If supplying a very concise answer, please elaborate on it's efficiency. Is the str converted to a different object, etc. ...

Debug some Python code

Edit: You can get the full source here: http://pastebin.com/m26693 Edit again: I added some highlights to the pastebin page. http://pastebin.com/m10f8d239 I'm probably going to regret asking such a long question, but I'm stumped with this bug and I could use some guidance. You're going to have to run this code (edit: not anymore. I cou...

Coverage not showing executed lines in virtualenv

I have a project and I am trying to run nosetests with coverage. I am running in a virtualenv. When I run $ python setup.py nosetests The tests run fine but coverage is not showing that any code is executed (coverage is all 0%). Name Stmts Exec Cover Missing --------------------------------------...

Getting friends within a specified degree of separation

Hi, all. I'm a very, very new programmer. My language of choice at the moment is Python, and I feel like I have a decent feel for it. I'm just now starting to learn about recursion. (By the way, if anyone could recommend a good guide on this, please let me know!) Just so you all know, this question is very elementary, and the code I'm...

Is there a Python equivalent to the PHP function htmlspecialchars() ?

Is there a similar or equivalent function in Python to the PHP function htmlspecialchars()? The closest thing I've found so far is htmlentitydefs.entitydefs(). ...

Replace __str__ method on list object in Python

This seems like it should be simple: I want a list like any other list, except it has a different .__str__ method. Trying to set object.__str__ = foo results in a read-only error Trying to subclass list means you need some way to convert an existing list to an instance of the subclass. This requires either copying all attributes manu...

py2exe - generated executable freezes when connecting to socket

Pardon my ignorance as I'm still a beginner in coding. I'm trying to convert a python script I wrote to a Windows executable program using py2exe. However, though I am able to successfully convert the script, the executable doesn't seem to be fully functional. After much debugging, I have isolated the cause and the following code seems...

Inaccurate Logarithm in Python

I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd. I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / log10(2)' and I got a round 3...

Building a minimal plugin architecture in Python.

I have an application, written in Python, which is used by a fairly technical audience (scientists). I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. I am looking for something extremely lightweight. Most scripts, or plugins, are not going to be developed and distribute...

Implementing the Koch Curve?

I was looking at the wikipedia page for the Koch Snowflake (here) and was bothered by the all the examples all being in the logo/turtle style. So i set out to make my own that returned a list or coordinates. My implementation is in python and i basically ripped off the python turtle implementation but replaced the turtle specific stuff...

Search functionality for Django

I'm developing a web app using Django, and I'll need to add search functionality soon. Search will be implemented for two models, one being an extension of the auth user class and another one with the fields name, tags, and description. So I guess nothing too scary here in context of searching text. For development I am using SQLite and...

Python: defining my own operators?

I have a bot in python that allows the users to evaluate mathematical expressions (via a set of safe functions), but I would like to define my own operator. Does python support such a thing? ...