python

Datastore access optimization

I'm writing a small program to record reading progress, the data models are simple: class BookState(db.Model): isbn = db.StringProperty() title = db.StringProperty(required=True) pages = db.IntegerProperty(required=True) img = db.StringProperty() class UpdatePoint(db.Model): book = db.ReferenceProperty(BookState)...

How can I extract the call graph of a function from Python source files?

Do you know an integrated tool that will generate the call graph of a function from Python sources? I need one that is consistent and can run on Windows OS. ...

How to include a python .egg library that is in a subdirectory (relative location)?

How do you import python .egg files that are stored in a relative location to the .py code? For example, My Application/ My Application/library1.egg My Application/libs/library2.egg My Application/test.py How do you import and use library1 and library2 from within test.py, while leaving the .egg libraries in-place? ...

How do I read a date in Excel format in Python?

How can I convert an Excel date (in a number format) to a proper date in Python? ...

Returning MatPotLib image as string

I am using matplotlib in a django app and would like to directly return the rendered image. So far I can go plt.savefig(...), then return the location of the image What I want to do is: return HttpResponse(plt.renderfig(...), mimetype="image/png") Any ideas? ...

How to store an IP in mySQL

strong textWe've got a healthy debate going on in the office this week. We're creating a Db to store proxy information, for the most part we have the schema worked out except for how we should store IPs. One camp wants to use 4 smallints, one for each octet and the other wants to use a 1 big int,INET_ATON. These tables are going to be h...

Finding the coordinates of tiles that are covered by a rectangle with x,y,w,h pixel coordinates

Say I have a tile based system using 16x16 pixels. How would you find out what tiles are covered by a rectangle defined by floating point pixel units? for eg, rect(x=16.0,y=16.0, w=1.0, h=1.0) -> tile(x=1, y=1, w=1, h=1) rect(x=16.0,y=16.0, w=16.0, h=16.0) -> tile(x=1, y=1, w=1, h=1) (still within same tile) rect(x=24.0,y=24.0, w=8....

Issues with inspect.py when used inside Jython

Hi, I am using an application developed in Jython. When I try to use the inspect.py in that, it shows error message. My code goes like this import inspect,os,sys,pprint,imp def handle_stackframe_without_leak(getframe): frame = inspect.currentframe() try: function = inspect.getframeinfo(getframe) print inspect.g...

Separation of ORM and validation

Hi all, I use django and I wonder in what cases where model validation should go. There are at least two variants: Validate in the model's save method and to raise IntegrityError or another exception if business rules were violated Validate data using forms and built-in clean_* facilities From one point of view, answer is obvious: o...

switch versions of python

Story: One of the app that i have works on python 2.4 and other on 2.6. I tried to do a sym link of python2.4 to python and things started to break loose on ubuntu jaunty. Now i am downloading every dependency of 2.4 and installing it using python2.4 setup.py install. The dependencies seem to be endless. Question1: How will i tell any ...

Getting list of pixel values from PIL

Guys, I'm looking for a bit of assistance. I'm a newbie programmer and one of the problems I'm having at the minute is trying to convert a black & white .jpg image into a list which I can then modulate into an audio signal. This is part of a lager project to create a python SSTV program. I have imported the PIL module and am trying to c...

What's a good way to render outlined fonts?

I'm writing a game in python with pygame and need to render text onto the screen. I want to render this text in one colour with an outline, so that I don't have to worry about what sort of background the the text is being displayed over. pygame.font doesn't seem to offer support for doing this sort of thing directly, and I'm wondering ...

Does python have a sorted list?

By which I mean a structure with: O(log n) complexity for x.push() operations O(log n) complexity to find an element O(n) complexity to compute list(x) which will be sorted I also had a related question about performance of list(...).insert(...) which is now here. ...

python singleton into multiprocessing

How can I code to share the same instance of a "singletonic" class among processes? ...

Python clock function on FreeBSD

While testing Pythons time.clock() function on FreeBSD I've noticed it always returns the same value, around 0.156 The time.time() function works properly but I need a something with a slightly higher resolution. Does anyone the C function it's bound to and if there is an alternative high resolution timer? I'm not profiling so the Tim...

swfupload failing in my django runserver

Hi All, I have copied and pasted the code from http://demo.swfupload.org/v220/simpledemo/ into a django template, but when I upload a photo, the demo says "Server (IO) Error" before it actually uploads the entire file. The runserver is getting the request and returning a 200. Is there something I am missing here? What steps should I tak...

What is the most efficent way to store a list in the Django models?

Currently I have a lot of python objects in my code similar to the following: class MyClass(): def __init__(self, name, friends): self.myName = name self.myFriends = [str(x) for x in friends] Now I want to turn this into a Django model, where self.myName is a string field, and self.myFriends is a list of strings. from d...

How can I tell if my script is being run from a cronjob or from the command line?

I have a script and it's display show's upload progress by writing to the same console line. When the script is run from a cron job, rather than writing to a single line, I get many lines: *** E0710091001.DAT *** [0.67%] *** E0710091001.DAT *** [1.33%] *** E0710091001.DAT *** [2.00%] *** E0710091001.DAT *** [2.66%] *...

Performance of list(...).insert(...)

I thought about the following question about computer's architecture. Suppose I do in Python from bisect import bisect index = bisect(x, a) # O(log n) (also, shouldn't it be a standard list function?) x.insert(index, a) # O(1) + memcpy() which takes log n, plus, if I correctly understand it, a memory copy operation for x[...

Python ftplib - uploading multiple files?

I've googled but I could only find how to upload one file... and I'm trying to upload all files from local directory to remote ftp directory. Any ideas how to achieve this? ...