python

Segment a list in Python

I am looking for an python inbuilt function (or mechanism) to segment a list into required segment lengths (without mutating the input list). Here is the code I already have: >>> def split_list(list, seg_length): ... inlist = list[:] ... outlist = [] ... ... while inlist: ... outlist.append(inlist[0:seg_length])...

Multiple versions of Python on OS X Leopard

Hi, I currently have multiple versions of Python installed on my Mac, the one that came with it, a version I downloaded recently from python.org, an older version used to run Zope locally and another version that Appengine is using. It's kind of a mess. Any recommendations of using one version of python to rule them all? How would I go a...

How to get the biggest numbers out from huge amount of numbers?

Hi, I'd like to get the biggest 100 elements out from 100000000 numbers(may be more) in a list. So I don't want to do a sort and pick up the first 100 elements to avoid memory issue. Sorting will consume much memory and time. Any existing choice? What I want is following function instead of a pure sort. Actually I don't want waste ...

Can I redirect the stdout in python into some sort of string buffer?

I'm using python's ftplib to write a small FTP client, but some of the functions in the package don't return string output, but print to stdout. I want to redirect stdout to an object which I'll be able to read the output from. I know stdout can be redirected into any regular file with: stdout = open("file", "a") But I prefer a metho...

problem in creating a calendar event using gdata in appengine

I am getting a token upgrade failed error when i try to create a calendar reminder using google's gdata used in an appengine environment. The code fragment is: def InsertSingleEvent(calendar_service, title=None, content=None, where=None, start_time=None, end_time=None): event = gdata.calendar.Calen...

How do I do database transactions with psycopg2/python db api ?

Im fiddling with psycopg2 , and while there's a .commit() and .rollback() there's no .begin() or similar to start a transaction , or so it seems ? I'd expect to be able to do db.begin() # possible even set the isolation level here curs = db.cursor() cursor.execute('select etc... for update') ... cursor.execute('update ... etc.') db.com...

Accessing data files before and after distutils/setuptools

I'm doing a platform independent PyQt application. I intend to use write a setup.py files using setuptools. So far I've managed to detech platform, e.g. load specific options for setup() depending on platform in order to use py2exe on Windows... etc... However, with my application I'm distributing some themes, HTML and images, I need to...

Python Matplotlib hangs when asked to plot a second chart (after closing first chart window)

Weird behaviour, I'm sure it's me screwing up, but I'd like to get to the bottom of what's happening: I am running the following code to create a very simple graph window using matplotlib: >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> ax = fig.add_subplot(111) >>> ax.plot((1, 3, 1)) [<matplotlib.lines.Line2D object at...

java and python equivalent of php's foreach($array as $key => $value)

EDIT: Amazingly, newacct's answer has accumulated 12 points in the four hours since I posted my question. This, despite its being incorrect. As alternatives to php's associative array, newacct offers Java's HashMap and Python's dictionary, neither of which preserves key order. cmcg provided a correct answer for the Java version. That is...

Workarounds when a string is too long for a .join. OverflowError occurs.

I'm working through some python problems on pythonchallenge.com to teach myself python and I've hit a roadblock, since the string I am to be using is too large for python to handle. I receive this error: my-macbook:python owner1$ python singleoccurrence.py Traceback (most recent call last): File "singleoccurrence.py", line 32, in <mo...

Python: prefer several small modules or one larger module?

I'm working on a Python web application in which I have some small modules that serve very specific functions: session.py, logger.py, database.py, etc. And by "small" I really do mean small; each of these files currently includes around 3-5 lines of code, or maybe up to 10 at most. I might have a few imports and a class definition or two...

Python GTK Drag and Drop - Get URL

I'm creating a small app must be able to receive URLs. If the apps window is open, I should be able to drag a link from a browser and drop it into the app - and the app will save the URL to a database. I'm creating this in Python/GTk. But I am a bit confused about the drag and drop functionality in it. So, how do it? Some sample code t...

Storing data and searching by metadata?

Let's say I have a set of data where each row is a pair of coordinates: (X, Y). Associated with each point I have arbitrary metadata, such as {color: yellow} or {age: 2 years}. I'd like to be able to store the data and metadata in such a way that I can query the metadata (eg: [rows where {age: 2 years, color: yellow}]) and in return rec...

"Watching" program being proccessed line-by-line?

I'm looking for a debugging tool that will run my Python app, but display which line is currently being processed -- like an automatically stepping debugger. Basically I want to see what is going on, but be able to jump in if a traceback occurs. Any suggestions? ...

How to make authkit session cookie HttpOnly in pylons?

Hi there! I use authkit module with Pylons and I see that session cookie it sets (aptly named authkit) is not set to be HttpOnly. Is there a simple way to make it HttpOnly? (By "simple" I mean the one that does not involve hacking authkit's code.) ...

Need to get the rest of an iterator in python

Say I have an iterator. After iterating over a few items of the iterator, I will have to get rid of these first few items and return an iterator(preferably the same) with the rest of the items. How do I go about? Also, Do iterators support remove or pop operations (like lists)? ...

App Engine: 13 StringPropertys vs. 1 StringListProperty (w.r.t. indexing/storage and query performance)

Hi all, A bit of background first: GeoModel is a library I wrote that adds very basic geospatial indexing and querying functionality to App Engine apps. It is similar in approach to geohashing. The equivalent location hash in GeoModel is called a 'geocell.' Currently, the GeoModel library adds 13 properties (location_geocell__n_, n=1.....

Creating Date Intervals in Python

I want to use a for loop to print every date between 2 dates. Actually going to merge this with a MySQL query to pass the date into the query itself and into the filename of the output. So, how can I change this: sum = 0 for i in range(1,11): print sum sum += i To this? InputDate = '2009-01-01' for i in range('2009-01-01','2009-0...

Barchart with vertical labels in python/matplotlib

I'm using matplotlib to generate a (vertical) barchart. The problem is my labels are rather long. Is there any way to display them vertically, either in the bar or above it or below it? Note: The best answer will get a 500 bounty. ...

returning a value to a c# program through python script

hello, I have a C# program which executes a python scrip How can I retrieve the python returning value in my C# program ? thanks! ...