python

Speeding up GTK tree view

I'm writing an application for the Maemo platform using pygtk and the rendering speed of the tree view seems to be a problem. Since the application is a media controller I'm using transition animations in the UI. These animations slide the controls into view when moving around the UI. The issue with the tree control is that it is slow. ...

Getting callable object from the frame

Hi! Given the frame object (as returned by sys._getframe, for instance), can I get the underlying callable object? Code explanation: def foo(): frame = sys._getframe() x = some_magic(frame) # x is foo, now Note that my problem is getting the object out of a frame, not the currently called object. Hope that's possible....

Must a secure cryptographic signature reside outside of the file it refers to?

I'm programming a pet project in Python, and it involves users A & B interacting over network, attempting to insure that each has a local copy of the same file from user C. The idea is that C gives each a file that has been digitally signed. A & B trade the digital signatures they have, and check it out on their own copy. If the signatu...

"Least Astonishment" in Python: The Mutable Default Argument

Anyone tinkering with python long enough has been bit (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5]. The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() ...

Django serialization of inherited model

Hi, I have a problem with serialization of Django inherited models. For example class Animal(models.Model): color = models.CharField(max_length=50) class Dog(Animal): name = models.CharField(max_length=50) ... # now I want to serialize Dog model with Animal inherited fields obviously included print serializers.serialize('xml',...

Alternatives to using pack_into() when manipulating a list of bytes?

I'm reading in a binary file into a list and parsing the binary data. I'm using unpack() to extract certain parts of the data as primitive data types, and I want to edit that data and insert it back into the original list of bytes. Using pack_into() would make it easy, except that I'm using Python 2.4, and pack_into() wasn't introduced u...

DateTime in python extracting different bits an pieces

Ok this should very easy task right. Well for some reason its becoming hell to me. I want to extract Year from current date using python. Do something like DateTime a = DateTime.Now() a.Year (this is in C#) Thanks ...

Adding tuples to produce a tuple with a subtotal per 'column'

What is the most pythonic way of adding the values of two or more tuples to produce a total for each 'column'? Eg: >>> a = (10, 20) >>> b = (40, 50) >>> c = (1, 3) >>> ??? (51, 73) I've so far considered the following: def sumtuples(*tuples): return (sum(v1 for v1,_ in tuples), sum(v2 for _,v2 in tuples)) >>> print sumtuples(a,...

Weird Problem with Classes and Optional Arguments

Okay so this was driving me nuts all day. Why does this happen: class Foo: def __init__(self, bla = {}): self.task_defs = bla def __str__(self): return ''.join(str(self.task_defs)) a = Foo() b = Foo() a.task_defs['BAR'] = 1 print 'B is ==> %s' % str(b) print 'A is ==> %s' % str(a) Gives me the output: B is ...

PHP equivalent to Python's yield operator

In Python (and others), you can incrementally process large volumes of data by using the 'yield' operator in a function. What would be the similar way to do so in PHP? For example, lets say in Python, if I wanted to read a potentially very large file, I could work on each line one at a time like so (this example is contrived, as it is ...

Code lines of code in a Django Project

Is there an easy way to count the lines of code you have written for your django project? Edit: The shell stuff is cool, but how about on Windows? ...

Why can't IPython return records with multiple fields when submitting a query to sqlite?

I am trying to write a simple query to an sqlite database in a python script. To test if my parameters were correct, I tried running the query from the ipython command line. It looked something like this: import sqlite3 db = 'G:\path\to\db\file.sqlite' conn = sqlite3.connect(db) results = conn.execute('SELECT * FROM studies').fetchall()...

Python processes stops responding to SIGTERM / SIGINT after being restarted

I'm having a weird problem with some python processes running using a watchdog process. The watchdog process is written in python and is the parent, and has a function called *start_child(name)* which uses subprocess.Popen to open the child process. The Popen object is recorded so that the watchdog can monitor the process using poll() a...

How can I retrieve last x elements in Django

Hello, I am trying to retrieve the latest 5 posts (by post time) In the views.py, if I try blog_post_list = blogPosts.objects.all()[:5] It retreives the first 5 elements of the blogPosts objects, how can I reverse this to retreive the latest ones? Cheers ...

How accurate is python's time.sleep()?

I can give it floating point numbers, such as time.sleep(0.5) but how accurate is it? If i give it time.sleep(0.05) will it really sleep about 50 ms? ...

What do you use Python for?

What do you use Python every day for? If you don't use Python every day, please include any answers with your usage on a non-daily basis. ...

Keep ConfigParser output files sorted

I've noticed with my source control that the content of the output files generated with ConfigParser is never in the same order. Sometimes sections will change place or options inside sections even without any modifications to the values. Is there a way to keep things sorted in the configuration file so that I don't have to commit trivi...

Python accessing web service protected by PKI/SSL

I need to use Python to access data from a RESTful web service that requires certificate-based client authentication (PKI) over SSL/HTTPS. What is the recommended way of doing this? ...

Python Exception handling

C has perror and errno, which print and store the last error encountered. This is convenient when doing file io as I do not have to fstat() every file that fails as an argument to fopen() to present the user with a reason why the call failed. I was wondering what is the proper way to grab errno when gracefully handling the IOError excep...

Simple tray icon application using pygtk

I'm writing a webmail checker in python and I want it to just sit on the tray icon and warn me when there is a new email. Could anyone point me in the right direction as far as the gtk code? I already coded the bits necessary to check for new email but it's CLI right now. ...