python

request.POST pylons, getting array like in php

Hi. I have a dynamic form, i must bulid some post array to field witch some id. For example: <input type="checkbox" name="field[124][]" value="1"> <input type="checkbox" name="field[124][]" value="2"> In php i can simply get value and key. foreach($_POST as $key => $value){ if(is_array($value){ foreach($value as $key2 => $value2...

Add elements in a list of dictionaries

I have a very long list of dictionaries with string indices and integer values. Many of the keys are the same across the dictionaries, though not all. I want to generate one dictionary in which the keys are the union of the keys in the separate dictionaries and the values are the sum of all the values corresponding to that key in each of...

arranging for python's sorted(set( <collection> ))

I'm trying to eliminate duplicates of an array containing a few thousands of custom objects. For this I'm implementing the __hash__ and __cmp__ functions for these objects. Is it safe to delegate these functions to one of the objects' fields (a guaranteed unique string) ? I.e. class A: def __init__(self, key, etc...): s...

Notepad++ indentation messes up

Hi, I'm coding in Python and I really like notepad++. However, off late when I use tab to indent, it seems fine in notepad++, but when I run the program I get an indentation error, and when I check my code in emacs or something, I find that notepad++ actually adds more tab spaces than it shows on screen. Would anyone have any idea whats...

How to specify Check Constraint on Varchar Datatype column in SQLALchemy?

Hi, I am trying to specify this check constraint on a column via SQLAlchemy. delete_flag char(1) not null CHECK(delete_flag in('Y','N')), I am not able to figure out the syntax to achieve this. Any link, tutorial, advise will be of great help. Thanks Tara Singh ...

Setting dbus properties in Python

I've found the excellent answer in How can I listen for ‘usb device inserted’ events in Linux, in Python? to get basic D-Bus connectivity and read properties. I assumed it would be easy to modify properties but I can't seem to get that working. pdevice.SetProperty("thing", dbus.Boolean(0)) gives the unenlightening DBusException: org...

Why isn't the socket binding correctly in Python?

I have written some Python bindings for a third party library written in C++. When I use the library in Python it sends data over the network but it does not receive anything (it does both in C++). If I run the same test program in two different C++ processes it crashes because they are both trying to bind a socket to the same port. Bu...

ImageKit in Django

I am implementing ImageKit in a Django app and I have everything set up properly to my knowledge. When I run the command $python manage.py ikflush main the command seems to run fine but nothing appears to happen. None of the images get resized or stored and cannot be accessed. main.models.py: class ProductImage(models.Model): pr...

Integrity check going from dev to testing Django app using Git

I'm using Git to push my code from my development machine to my testing server. Dev: Mac, Python 2.6, sqllite and Test: Linux, Python 2.7, MySQL I took an early dev database and exported it to MySQL for initial testing. So now I'm regularly pushing new code to the testing server. In general it seems to be working well, but I'm ge...

Configure Django URLS.py to keep #anchors in URL after it rewrites it with a end /

In my django application I have my URLS.PY configured to accept requests to /community/user/id and /community/user/id/ with: url(r'^(?P<username>[\w-]+)/(?P<cardId>\d+)/$', 'singleCard.views.singleCard', name='singleCardView'), I did this as some times people will add an ending "/" and I didn't want to raise a 404. However parts of my...

insert variable values into a string in python

hello, my question is: As introduces a variable [i] into a string in python. For example look at the following script, I just want to be able to give a name to the image, for example geo [0]. Tiff ... to geo [i]. tiff, or if you use an accountant as I can replace a portion of the value chain to generate a counter. data = self.cm...

Basic python arithmetic - division

I have two variables : count, which is a number of my filtered objects, and constant value per_page. I want to divide count by per_page and get integer value but I no matter what I try - I'm getting 0 or 0.0 : >>> count = friends.count() >>> print count 1 >>> per_page = 2 >>> print per_page 2 >>> pages = math.ceil(count/per_pages) >>> p...

Django Pagination on a Changing Queryset

Hi everyone. On one of my pages I would like to display a subset of a sorted list of objects. The entire list of relevant objects is extremely long and has a good chance of changing while the user is viewing the page. I don't mind that the page is not up to date, but I do need to ensure that when the user goes to the next page, the user ...

howto extract simple string from tuple in python (newbie question)

I have a rather large tuple which contains: [('and', 44023), ('cx', 37711), ('is', 36777) .... ] I just want to extract the first string delimited by the single quotes, so the output for the above tuple would be: and cx is How do I code this (with extensibilty built in to some degree)? ...

Problem with 2D collision detection from beginner

I've taken an introductory course in Computer Science, but a short while back I decided to try and make a game. I'm having a problem with collision detection. My idea was to move an object, and if there is a collision, move it back the way it came until there is no longer a collision. Here is my code: class Player(object): ... ...

Ascending/descending ordering of Django QuerySet when one attribute is a model method

I have a QuerySet of teams ordered by school name. One of the attributes is a model method that keeps track of the team's winning percentage. I want to order the teams from highest winning percentage to lowest. If teams have the same winning percentage, I want them to be ordered alphabetically by school. How do I get something like this:...

converting structures in c to pywin32?

i am trying to use TTM_GETTEXT via SendMessage using pywin32. the problem is, the structure of the lparam, which is where the text should be stored, has to be TOOLINFO, which is well documented in MSDN, but has no counterpart in pywin32. is there a way to create the same structure using python and pywin32? Edit: here's the code i came u...

using python to add images to powerpoint files

has anyone used python to add images to powerpoint 2007 files? can someone please help me get started with this ...

how to convert python/cython unicode string to array of long integers, to do levenshtein edit distance

I have the following Cython code (adapted from the bpbio project) that does Damerau-Levenenshtein edit-distance calculation: #--------------------------------------------------------------------------- cdef extern from "stdlib.h": ctypedef unsigned int size_t size_t strlen(char *s) void *malloc(size_t size) void *calloc(size_t n...

is python the preferred language for mobile applications?

a lot of my friends are using Python to build their mobile applications and games. is python the preferred language for mobile applications for developers? if so, why? ...