python

Python data structure recommendation?

I currently have a structure that is a dict: each value is a list that contains numeric values. Each of these numeric lists contain what (to borrow a SQL idiom) you could call a primary key containing the first three values which are: a year, a player identifier, and a team identifier. This is the key for the dict. So you can get a uniq...

"Slice lists" and "the ellipsis" in Python; slicing lists and lists of lists with lists of slices.

Original question: Can someone tell me how to use "slice lists" and the "ellipsis"? When are they useful? Thanks. Here's what the language definition says about "slice_list" and "ellipsis"; Alex Martelli's answer points out their origin, which is not what I had envisioned. [http://docs.python.org/reference/expressions.html#tok-slici...

local import statements in python

I think putting the import statement as close to the fragment that uses it helps readability by making its dependencies more clear. will python cache this? should I care? is this a bad idea? def Process(): import StringIO file_handle=StringIO.StringIO('hello world') #do more stuff for i in xrange(10): Process() A littl...

UnicodeDecodeError with Django's request.FILES..

Hello, I have the following code in the view call.. def view(request): body = u"" for filename, f in request.FILES.items(): body = body + 'Filename: ' + filename + '\n' + f.read() + '\n' On some cases I get "UnicodeDecodeError: 'ascii' codec can't decode byte 0xf0 in position 7470: ordinal not in range(128)". What a...

What does the brackets mean in python: table[r][pos+i]?

This is the full code: def checkRow(table, r, pos, word): # done for you! for i in range(0, len(word)): if table[r][pos+i] != word[i]: return False return True I know the bracket mean the index value (in this case r some value of the index table) but what does a bracket next to another bracket mean? (table[...

regex in for loop

hi how to use regex with for loop in python example data abc 1 xyz 0 abc 2 xyz 1 abc 3 xyz 2 how to write regex for something like below for i in range(1, 3): re.match(abc +i xyz +(i-1)) Thanks in advance ...

Python file manipulation

Assume I have such folders rootfolder | / \ \ 01 02 03 .... | 13_itemname.xml So under my rootfolder, each directory represents a month like 01 02 03 and under these directories I have items with their create hour and item name such as 16_item1.xml, 24_item1.xml etc, as you may guess there are several items an...

How to retrieve a directory of files from a remote server?

If I have a directory on a remote web server that allows directory browsing, how would I go about to fetch all those files listed there from my other web server? I know I can use urllib2.urlopen to fetch individual files, but how would I get a list of all the files in that remote directory? ...

C# or other .net equivalents of core python modules for IronPython?

I would like to compile and distribute (on .net) some python programs that work well with IronPython, but I am new to .net and am getting errors related to particular python modules. There is a utility for compiling to low level .net and it works well, but I get errors when dealing with common libraries; code that runs in the interprete...

django, uni_form and python's __init__() function - how to pass arguments to a form?

I'm having a bit of difficulty understanding how the python __init__( ) function works. What I'm trying to do is create a new form in django, and use the uni_form helper to display the form in a custom manner using fieldsets, however I'm passing an argument to the form that should slightly change the layout of the form and I can't figure...

Python: complex list comprehensions where one var depends on another (x for x in t[1] for t in tests)

I want to do something like: all = [ x for x in t[1] for t in tests ] tests looks like: [ ("foo",[a,b,c]), ("bar",[d,e,f]) ] So I want to have the result all = [a,b,c,d,e,f] My code does not work, Python says: UnboundLocalError: local variable 't' referenced before assignment Is there any simple way to do that? ...

Editing both sides of M2M in Admin Page

Hello all, First I'll lay out what I'm trying to achieve in case there's a different way to go about it! I want to be able to edit both sides of an M2M relationship (preferably on the admin page although if needs be it could be on a normal page) using any of the multi select interfaces. The problem obviously comes with the reverse sid...

IPC between Python and C#

I want to pass data between a Python and a C# application in Windows (I want the channel to be bi-directional) In fact I wanna pass a struct containing data about a network packet that I've captured with C# (SharpPcap) to the Python app and then send back a modified packet to the C# program. What do you propose ? (I rather it be a fast m...

Any tutorial for Python PalmDB library?

Hello, I've downloaded the Python PalmDB lib, but can't find any info on how to use it. I've tried reading docstrings and so far I've been able to come up with the following code: from pprint import pprint from PalmDB.PalmDatabase import PalmDatabase pdb = PalmDatabase() with open('testdb.pdb','rb') as data: pdb.fromByteArray(data...

How to unquote URL quoted UTF-8 strings in Python

thestring = urllib.quote(thestring.encode('utf-8')) This will encode it. How to decode it? ...

Environment on google Appengine

Hi, does someone have an idea how to get the environment variables on Google-AppEngine ? I'm trying to write a simple Script that shall use the Client-IP (for Authentication) and a parameter (geturl or so) from the URL (for e.g. http://thingy.appspot.dom/index?geturl=www.google.at) I red that i should be able to get the Client-IP via...

How to close not responsive Win32 Internet Explorer COM interface?

Hello, actually this is not hang status, i mean..it slow response, so in that case, i would like to close IE and want to restart from start. so closing is no problem ,problem is ,how to set timeout ,for example if i set 15sec, if not webpage open less than 15 sec i want to close it and restart from start. is this possible to use with I...

What's going on with python 3k ?

Since I'm not strictly python developer please don't flame me just for the question. I'm wondering about Python 3k, that from my point of view, might be some kind of misconception. Or quite in-relevant step forward (I'm taking into account the 2.6 and 3k releases, which was almost one after another). Before the flame will start, I'll ex...

Is it possible to redefine reverse in a Django project?

I have some custom logic that needs to be executed every single time a URL is reversed, even for third-party apps. My project is a multitenant web app, and the tenant is identified based on the URL. There isn't a single valid URL that doesn't include a tenant identifier. I already have a wrapper function around reverse, but now I need a...

embed python in matlab mex file on os x

I'm trying to embed Python into a MATLAB mex function on OS X. I've seen references that this can be done (eg here) but I can't find any OS X specific information. So far I can successfully build an embedded Python (so my linker flags must be OK) and I can also build example mex files without any trouble and with the default options: jm...