python

Interrupt method execution after arbitrary time in Python

I have some method doA() that occasionally hangs for a while. Are there any common modules in Python that can control time of doA() execution and interrupt it? Of course, it may be implemented via threads, so simple wrapper around threading module may be good solution. In other words i'd like to have code like: import CoolAsyncControl ...

django, python and link encryption

Hello I need to arrange some kind of encrpytion for generating user specific links. Users will be clicking this link and at some other view, related link with the crypted string will be decrypted and result will be returned. For this, I need some kind of encryption function that consumes a number(or a string) that is the primary key of ...

MongoDB: Limiting results from a $gt query (from pymongo)

Hi there, I'm gathering some statistics from a web service, and storing it in a collection. The data looks similar to this (but with more fields): {"downloads": 30, "dt": "2010-02-17T16:56:34.163000"} {"downloads": 30, "dt": "2010-02-17T17:56:34.163000"} {"downloads": 30, "dt": "2010-02-17T18:56:34.163000"} {"downloads": 30, "dt": "201...

Django decorator getting a WSGIRequest and not the expected function argument

I'm creating a decorator for Django views that will check permissions in a non-Django-managed database. Here is the decorator: def check_ownership(failure_redirect_url='/', *args, **kwargs): def _check_ownership(view): def _wrapper(request, csi=None): try: opb_id=request.user.get_profile().opb_id ...

Why dictionaries appear to be reversed?

Why dictionaries in python appears reversed? >>> a = {'one': '1', 'two': '2', 'three': '3', 'four': '4'} >>> a {'four': '4', 'three': '3', 'two': '2', 'one': '1'} How can I fix this? ...

Who is throwing (and catching) this MySQL Exception?

i'm using Python with MySQL and Django. I keep seeing this error and I can't figure out where the exception is being thrown: Exception _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") in > ignored I have many "try" and "exception" blocks in my code--if the exception occurred within...

Overloading embedded Python functions using PyArg_ParseTuple.

If I'm trying to overload an embedded Python function so that the second argument can be a long or an Object, is there a standard way to do it? Is this it? What I'm trying now (names changed to protect the innocent): bool UseLongVar2 = true; if (!PyArg_ParseTuple(args, "ll:foo", &LongVar1, &LongVar2)) { PyErr_Clear(); ...

Virtual Serial Device in Python?

I know that I can use e.g. pySerial to talk to serial devices, but what if I don't have a device right now but still need to write a client for it? How can I write a "virtual serial device" in Python and have pySerial talk to it, like I would, say, run a local web server? Maybe I'm just not searching well, but I've been unable to find an...

Django. Retrieving and opening a zip file from HDD

I have a path to a zip file. I don't know how to retrieve the file from the hard drive or open that zip file. Does anyone know? The zip file is a zip file, but it's really a .epub file. ...

python timer mystery

Well, at least a mystery to me. Consider the following: import time import signal def catcher(signum, _): print "beat!" signal.signal(signal.SIGALRM, catcher) signal.setitimer(signal.ITIMER_REAL, 2, 2) while True: time.sleep(5) Works as expected i.e. delivers a "beat!" message every 2 seconds. Next, no output is produced: ...

Blurring a picture (Python, Jython, picture editing)

Trying to blur a picture in Jython. What I have does run but does not return a blurred picture. I'm kinda at a loss of what is wrong with it. FINAL (WORKING) CODE EDITED IN BELOW. THANKS FOR HELP GUYS! def main(): pic= makePicture( pickAFile() ) show( pic ) blurAmount=10 makeBlurredPicture(pic,blurAmount) show(makeBlurredPictur...

ID3 Decision Tree with Numeric Values

I'm looking for a ID3 decision tree implementation in Python or any languages which takes a validation and a testing file as an input and returns predictions. I found this and this but I couldn't adapt them to numeric values, e.g. to Iris dataset. Do you know any ID3 tree implementation that works from console or any written in Python?...

How do you order Models with M2M relationship to avoid NameSpace errors?

I can't figure out how I'm supposed to order these models that have a M2M relationship. When I try to syncdb, I can't because the Model related model is not in the namespace yet, so I get: NameError: name 'FavoriteQuestion' is not defined If I switch places, I get: NameError: name 'UserProfile' is not defined class UserProfile(models....

how can i get the executable's current directory in py2exe?

I use this bit of code in my script to pinpoint, in a cross-platform way, where exactly it's being run from: SCRIPT_ROOT = os.path.dirname(os.path.realpath(__file__)) Pretty simple. I then go on to use SCRIPT_ROOT in other areas of my script to make sure everything is properly relative. My problem occurs when I run it through py2exe, ...

What Python/IronPython web development framework work on the Microsoft technology stack?

I started learning Python using the IronPython implementation. I'd like to do some web development now. I'm looking for a python web development framework that works on the Microsoft technology stack (IIS + MS SQL Server). Django looks like an interesting framework but based on what I have read, getting it to work on the Microsoft techno...

How to set cookies with redirect in Pylons

In light of the cookie-handling bugs affecting Safari and Chrome (see this thread), and Pylons implementation of redirect_to as an exception, is it possible to reliably set a tracking cookie and redirect at the same time? Is the META refresh method looked down upon? ...

the number of images, using "len()"

I need to count the number of images (in this case 1 image). Apparently using "len()"? Here is HTML: <div class="detail-headline"> Fotogal&#233;ria </div> <div class="detail-indent"> <table id="ctl00_ctl00_ctl00_containerHolder_mainContentHolder_innnerContentHolder_ZakazkaControl_ZakazkaObrazky1_ObrazkyDataList" cellspa...

python script for robust multi-array average on microarray data

I have tried google with no luck. I have seen some weak references to robust multi-array averaging done with python but no code. I am not so interested in reinventing the wheel. Any suggestions on a python module, script .... If I could find a nice explanation or example of the algorithm I would write a python implementation to share. ...

How to generate code objects from modules in Python?

I have a .pyc file with no corresponding Python source code. I want to see the disassembly of the module using dis. I can import my module just fine with import dis import foo But to call dis.dis on it, I can't use the module object. I need the corresponding code object which backs foo. How do I create it? It seems that the compile bu...

Django memory usage going up with every request

I moved my first Django project from DjangoEurope to Webfaction, and that started an issue looking like a memory leak. With every single request memory usage of the server process goes up about 500kb. It never goes down. This goes on until Webfaction kills it for using too much memory. I can clearly see this when I refresh the Django's ...