python

python tuple division

TypeError: unsupported operand type(s) for /: 'tuple' and 'tuple' I'm getting above error , while I fetched a record using query "select max(rowid) from table" and assigned it to variable and while performing / operation is throws above message. How to resolve this. ...

Catching errors when logging with SocketHandler in Python

My web application runs on multpile apache instances and I am having multiprocess logging issues because of this. I am currently using a SocketHandler for logging to a daemon using SocketServer that then writes logs to a single log file (similar to this example). Now that I am using a SocketHandler for logging I am having trouble disco...

delete xml node using lxml

hello friends, <login> <user> <userid>admin</userid> </user> . . . . <user> <userid>admin</userid> </user> </login> this my xml file. when i user clear()or del method it will clear all the child and a blank node is creating <user/> How can i avoid creating this blank node it will make problem when i use...

writing to data to excel

Hello, I have data that I need to export to excel, I just don't know how to go about it, here's the view I'm using, I've commented out my attempts.A push to the right direction will be greatly appreciated. def month_end(request): """ A simple view that will generate a month end report as a PDF response. """ current_dat...

what would I use stackless python for?

There are many questions related to Stackless Python. But none answering this my question, I think (correct me if wrong - please!). There's some buzz about it all the time so I curious to know. What would I use Stackless for? How is it better than CPython? Yes it has green threads (stackless) that allow quickly create many lightweight t...

Does Django have `__not_equal`?

Does Django have a field lookup like __not_equal? (Field lookups are __exact, __contains, etc.) ...

What's the difference between eval, exec, and compile in Python?

I've been looking at dynamic evaluation of Python code, and come across the eval() and compile() functions, and the exec statement. Can someone please explain the difference between eval and exec, and how the different modes of compile() fit in? ...

Rasterizing a GDAL layer

Edit Here is the proper way to do it, and the documentation: import random from osgeo import gdal, ogr RASTERIZE_COLOR_FIELD = "__color__" def rasterize(pixel_size=25) # Open the data source orig_data_source = ogr.Open("test.shp") # Make a copy of the layer's data source because we'll need to # modify its attribu...

Django: Can't set ForeignKey value to None from admin

I have a model Category which has a ForeignKey to a SimplePage model. null and blank are set to True. The problem is, when I edit a Category from the admin interface, I can't change the ForeignKey to --------- (Which looks like the admin's way of saying None.) The value can be None initially, and I can change it to an actual value throug...

Equivalent functionality to Google Charts API from python ? (Venn Diagrams also needed!)

Hey all, is there any library (for C or python) that I can use to get roughly the same functionality as I can get from Google Charts ? I specifically need the pie diagrams (standard), multi-dataset-pie-diagrams (not-so-standard), and venn diagrams (rare)... ...

Python: Setting an element of a Numpy matrix

I am a pretty new to python. I have created an empty matrix a = numpy.zeros(shape=(n,n)) Now I can access each element using a.item(i,j) How do I set an index (i,j)? ...

Logging in and using cookies in pycurl

I need to download a file that is on a password protected page. To get to the page manually I first have to authenticate via an ordinary login page. I want to use curl to fetch this page in script. My script first logins. It appears to succeed--it returns a 200 from a PUT to /login. However, the fetch of the desired page fails, with a...

Why doesn't this loop display an updated object count every five seconds?

I use this python code to output the number of Things every 5 seconds: def my_count(): while True: print "Number of Things: %d" % Thing.objects.count() time.sleep(5) my_count() If another process generates a new Thing while my_count() is running, my_count() will keep printing the same number, even though it ...

Custom instance unpickling in Python: should the object dictionary be updated, or is replacing it OK?

When defining how objects of a certain class should be unpickled, via __setstate__, I gather that it is safe to do def __setstate__(self, dict_returned_by_pickle): self.__dict__.update(dict_returned_by_pickle) when the pickled state is a dictionary. This is what I have seen in an answer here on stackoverflow. However, is this a ...

Django admin inline form error

Hi. I have an inline formset in my admin site. I also have save_as = True in admin.py. My models are, for example: class Poll(models.Model): question = models.CharField(max_length=200, unique = True) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice = mod...

${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}} syntax don't work in mako template

In mako template, I need to do something like that : ${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}} When A do that, I've this error : SyntaxException: (SyntaxError) unexpected EOF while parsing (, line 1) ("'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'") in file… Do you know a tip to fix this issue ? I need to use this syntax in t...

How to refer to a method name from with a method in Python?

Say I have the following class defined with the method foo: class MyClass: def foo(self): print "My name is %s" % __name__ Now when I call foo() I expect/want to see this printed out My name is foo However I get My name is __main__ And if I was to put the class definition into a module called FooBar I would g...

Printing to a file from a list of lists in Python

I am trying to print to a file that will look like: 'A' '1' 'B' '2' 'C' '3' Given the code below, however, the result is : ['A'] ['B'] ['C'] This is probably a 'softball' question, but what am I doing wrong here? l1 = ['1'] l2 = ['A'] l3 = ['2'] l4 = ['B'] l5 = ['3'] l6 = ['C'] listoflists = [l1,l2,l3,l4,l5,l6] itr = iter(listof...

Python: defining functions on the fly

I have the following code: funcs = [] for i in range(10): def func(): print i funcs.append(func) for f in funcs: f() The problem is that func is being overriden. Ie the output of the code is: 9 9 9 ... How would you solve this without defining new functions? The optimal solution would be to change the name of th...

Python sort parallel arrays in place?

Is there an easy (meaning without rolling one's own sorting function) way to sort parallel lists without unnecessary copying in Python? For example: foo = range(5) bar = range(5, 0, -1) parallelSort(bar, foo) print foo # [4,3,2,1,0] print bar # [1,2,3,4,5] I've seen the examples using zip but it seems silly to copy all your data from...