python

Get all related Django model objects

How can I get a list of all the model objects that have a ForeignKey pointing to an object? (Something like the delete confirmation page in the Django admin before DELETE CASCADE). I'm trying to come up with a generic way of merging duplicate objects in the database. Basically I want all of the objects that have ForeignKeys points to ...

How to input an integer tuple from user?

Presently I am doing this print 'Enter source' source = tuple(sys.stdin.readline()) print 'Enter target' target = tuple(sys.stdin.readline()) but source and target become string tuples in this case with a \n at the end ...

Can I catch error in a list comprehensions to be sure to loop all the list items

I've got a list comprehensions which filter a list: l = [obj for obj in objlist if not obj.mycond()] but the object method mycond() can raise an Exception I must intercept. I need to collect all the errors at the end of the loop to show which object has created any problems and at the same time I want to be sure to loop all the list e...

how do I iterate over a "gslist" in Python?

Let's say I get a glib gpointer to a glib gslist and would like to iterate over the latter, how would I do it? I don't even know how to get to the gslist with the gpointer for starters! Update: I found a workaround - the python bindings in this instance wasn't complete so I had to find another solution. ...

building dynamic forms in django

I'm trying to build a form dynamically based on the field and its definitions stored in a database. In my db, I have defined 1 checkbox with some label and 1 textfield with some label. How do I build a form dynamically in my view from the data in the db? Thanks ...

How do I prevent duplicating code with pylons html table updated via ajax?

I have a pylons web-page with a table. I have created python functions in the template which help with the construction of the table html. One of these functions takes an 'item' and generates an html row while also adding css zebra striping. The other def generates the header row's html. This works perfectly for loading the initial ...

Django Group By relation's data

In Django, I can do value(), and then distinct() to group by. A { Foreign Key B } B { String name } However, is it possible to group using a related object's data? I.e. In the above relation, can I group A by B's name? ...

Unicode parsing problem using json.loads

Hi What is the best way to load JSON string in Python. Here is my code which give problem for loading json strings... import json json.loads(str_to_load) I also tried supplying 'encoding' parameter with value 'utf-16', but that didn't work either... Can you please help me solve this problem? Thanks ...

Emulate floating point string conversion behaviour of Linux on Windows

I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"! I originally encountered the problem in Python, but further investigation showed that the diff...

Python design question

I'm a C programmer and I'm getting quite good with Python. But I still have some problems getting my mind around the OO awesomeness of Python. Here is my current design problem: The end "product" is a JSON data structure created in Python (and passed to Javascript code) containing different types of data like: { type:url, {urlpayload...

PyEval_CallObject failing in loop occasionally

Hi. I am struggling a bit with the Python C API. I am calling a python method to do some game AI at about 60hz. It works most of the time but every second or so the call to PyEval_CallObject results in a NULL return value. If I correctly detect the error and continue looping, all is well for the next second or so, whereupon the error o...

Manipulate table values with PyObjC and Core Data

hi all, I recently started programming with xCode and PyObjC and I'm trying to create a logistics application. I have a table with the store's items and a second table with the expenses. Until now everything worked ok by using just the interface builder to link values to core data information and I didn't have to write any code. Now the ...

draw text to center of the image using PIL

hi I have a few stings I would like to draw to imagea. the thing is that I want each string to be displayed in the middle of each image. how do I do it? or how can I know the length in pixels of a string (giving ofcourse the font)? thanks ...

Why both, import logging and import logging.config are needed?

Should not it be handled by a single import? i.e. import logging. If I do not include import logging.config in my script, it gives : AttributeError: 'module' object has no attribute 'config' ...

Probem with links Embedding the app engine's shell app within my own app, for help coding

In my own App Engine App, at the browser, I wanted to mess around with new code by simulating having access to the python interpretor's >> within a browser window by embdedding this app --> http://shell.appspot.com/ . One can get shell_20091112.tar.gz at http://code.google.com/p/google-app-engine-samples/downloads/detail?name=shell_200...

Easiest way to display a pdf, ps or dvi file from python in linux.

I have created an application with python and wxpython. I would like to display a help file in pdf, ps or dvi format in GNU/Linux. Could use the distribution pdf viewer, but not so easy when you don't know which they have. Any ideas on how to solve this? ...

Automate file download from http using python

I want to automatically save a file from a website. I don't know how to bypass the Download File prompt in python and save it directly to my c: drive. Any help is appreciated, Elliott ...

How to split a list into a given number of sub-lists in python

Possible Duplicates: splitting a list of arbitrary size into only roughly N-equal parts How do you split a list into evenly sized chunks in Python? I need to create a function that will split a list into a list of list, each containing an equal number of items (or as equal as possible). e.g. def split_lists(mainlist, split...

Python Subclass Builtin List

Hi everyone, I want to subclass the list type and have slicing return an object of the descendant type, however it is returning a list. What is the minimum code way to do this? If there isn't a neat way to do it, I'll just include a list internally which is slightly more messy, but not unreasonable. Thank you! Edit: My code so far: c...

Python NTLM proxy authentication problem using python-ntlm

I'm trying to use urllib2 and python-ntlm to fetch an url behind and NTLM-authenticated proxy (although it should also suppoprt Basic and Digest authentication), and I'm getting a 407 HTTP error. The code is: import urlib2 from ntlm import HTTPNtlmAuthHandler from urlparse import urlparse, urlunparse user = { 'id' : '...