python

Are zlib.compress on Python and Deflater.deflate on Java (Android) compatible?

I am porting a Python application to Android and, at some point, this application has to communicate with a Web Service, sending it compressed data. In order to do that it uses the next method: def stuff(self, data): "Convert into UTF-8 and compress." return zlib.compress(simplejson.dumps(data)) I am using the next method to ...

Testing for Inactivity in Python on Mac

Is there a way to test, using Python, how long the system has been idle on Mac? Or, failing that, even if the system is currently idle? Answer Using the information from the accepted solution, here is an ugly but functional and fairly efficient function for the job: from subprocess import * def idleTime(): '''Return idle time in ...

How to write a generator that returns ALL-BUT-LAST items in the iterable in Python?

I asked some similar questions [1, 2] yesterday and got great answers, but I am not yet technically skilled enough to write a generator of such sophistication myself. How could I write a generator that would raise StopIteration if it's the last item, instead of yielding it? I am thinking I should somehow ask two values at a time, and s...

Determine current Mac Chrome web page using Python

Is there a way to determine programmatically, using Python, which web page is currently active in Google Chrome? ...

How to look ahead one element in a Python generator?

I can't figure out how to look ahead one element in a Python generator. As soon as I look it's gone. Here is what I mean: gen = iter([1,2,3]) next_value = gen.next() # okay, I looked forward and see that next_value = 1 # but now: list(gen) # is [2, 3] -- the first value is gone! Here is a more real example: gen = element_generato...

How to dynamically access class properties in Python?

Let's say I create an instance of a class and want to assign some values to its public properties. Usually, this would be done like this: class MyClass: def __init__(self): self.name = None self.text = None myclass = MyClass() myclass.name = 'My name' But, what if a write a function that takes a class as parameter...

How do I find the baseline of a line of text in Reportlab?

How do I find the baseline for a line of text in Reportlab so I can align other elements on the page with the baseline of the text? I am using canvas.drawString() for these elements. ...

Python: list assignment out of range

This module is part of a simple todo app I made with Python... def deleteitem(): showlist() get_item = int(raw_input( "\n Enter number of item to delete: \n")) f = open('todo.txt') lines = f.readlines() f.close() lines[get_item] = "" f = open('to...

How do I select from multiple tables in one query with Django?

I have two tables, one "Company" and one "Employee": class Company(models.Model): name = models.CharField(max_length=60) class Employee(models.Model): name = models.CharField(max_length=60) company = models.ForeignField(Company) And I want to list every Employee in a table, with the Company next to it. Which is simple eno...

How to prevent a function from being overridden in python

Is there a way to make a class function unoverriddable? something like java's final keyword. i.e, any overriding class cannot override that method. ...

django forms MultipleChoiceField reverts to original value on save

Hi, I have wrote a custom MultipleChoiceField. I have everything working ok but when I submit the form the selected values go back to the original choices even though the form validates ok. my code looks something like this: class ProgrammeField(forms.MultipleChoiceField): widget = widgets.SelectMultiple class ProgrammeForm(forms...

Django deployment. Error loading MySQLdb module. Trouble reading/writing from /tmp directory

I'm deploying my Django app to another host/server using mod_wsgi and MySQLdb. Right now, I'm getting a 500 error with the following log: ImproperlyConfigured: Error loading MySQLdb module: /tmp/MySQL_python-1.2.3c1-py2.4-linux-i686.egg-tmp/_mysql.so: failed to map segment from shared object: Operation not permitted Did some research a...

Python: PSP & HTML tables

I have a python psp page code is shown below. Currently it only prints out the characters in single rows of 60, with the character count in the left column. <table> <% s = ''.join(aa[i] for i in table if i in aa) for i in range(0, len(s), 60): req.write('<tr><td><TT>%04d</td><td><TT>%s</TT></td></tr>' % (i+1, s[i:i+60])); #end %> </...

Timeout for xmlrpclib client requests

I am using Python's xmlrpclib to make requests to an xml-rpc service. Is there a way to set a client timeout, so my requests don't hang forever when the server is not available? I know I can globally set a socket timeout with socket.setdefaulttimeout(), but that is not preferable. ...

Requesting a JavaScript property in Python (GAE)

Hello again! I'm currently making an iphone web app based on Google App Engine (python). I need to check if the user is browsing not trough safari but by the home screen. I can check this with an read-only 'window.navigator.standalone' Boolean JavaScript property as read on :http://developer.apple.com/safari/library/documentation/AppleA...

Compare DB row values efficiently

I want to loop through a database of documents and calculate a pairwise comparison score. A simplistic, naive method would nest a loop within another loop. This would result in the program comparing documents twice and also comparing each document to itself. Is there a name for the algorithm for doing this task efficiently? Is there ...

Install TurboGears on windows xp

I've been trying to get TurboGears installed on Windows by following this site. I've installed virtualenv but when I execute the command "virtualenv --no-site-packages testproj", I get the following message: New python executable in testproj\Scripts\python.exe Traceback (most recent call last): File "C:\Python26\Scripts\virtualenv-sc...

Python server pages, tables and lists

Hi, I am using MySQL and python server pages to show the data in a database. In the db I have selected this data: a list x =[1, 61, 121, 181, 241, 301] and a list of lists z = (['a','b'],['c','d'],['e','f'],['g','h'],['i','j'],['k','l']) and I would like to put these in a table to look like: 001 a b 061 c d 121 e f 181 g h 241 ...

Dynamic mass hosting using mod_wsgi

Hi, I am trying to configure an apache server using mod_wsgi for dynamic mass hosting. Each user will have it's own instance of a python application located in /mnt/data/www/domains/[user_name] and there will be a vhost.map telling me which domain maps to each user's directory (the directory will have the same name as the user). What i d...

How to create instances of a class from a static method?

Hello. Here is my problem. I have created a pretty heavy readonly class making many database calls with a static "factory" method. The goal of this method is to avoid killing the database by looking in a pool of already-created objects if an identical instance of the same object (same type, same init parameters) already exists. If some...