python

Python list filtering: remove subsets from list of lists.

Using Python how do you reduce a list of lists by an ordered subset match [[..],[..],..]? In the context of this question a list L is a subset of list M if M contains all members of L, and in the same order. For example, the list [1,2] is a subset of the list [1,2,3], but not of the list [2,1,3]. Example input: a. [[1, 2, 4, 8], [1, ...

Python/GAE web request error handling

I am developing an application on the Google App Engine using Python. I have a handler that can return a variety of outputs (html and json at the moment), I am testing for obvious errors in the system based on invalid parameters sent to the request handler. However what I am doing feels dirty (see below): class FeedHandler(webapp.Requ...

Parallel Python: What is a callback?

In Parallel Python it has something in the submit function called a callback (documentation) however it doesn't seem to explain it too well. I've posted on their forum a couple days ago and I've not received a response. Would someone explain what a callback is and what it's used for? Thank you. ...

Shorter, more pythonic way of writing an if statements

I have this bc = 'off' if c.page == 'blog': bc = 'on' print bc Is there a more pythonic (and/or shorter) way of writing this in python? ...

What is the most efficient way to add an element to a list only if isn't there yet?

I have the following code in Python: def point_to_index(point): if point not in points: points.append(point) return points.index(point) This code is awfully inefficient, especially since I expect points to grow to hold a few million elements. If the point isn't in the list, I traverse the list 3 times: look for it a...

Combining two lists and removing duplicates, without removing duplicates in original list

I have two lists that i need to combine where the second list has any duplicates of the first list ignored. .. A bit hard to explain, so let me show an example of what the code looks like, and what i want as a result. first_list = [1, 2, 2, 5] second_list = [2, 5, 7, 9] # The result of combining the two lists should result in this lis...

Need Help using XPath in ElementTree

I am having a heck of a time using ElementTree 1.3 in Python. Essentially, ElementTree does absolutely nothing. My XML file looks like the following: <?xml version="1.0"?> <ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2008-08-19"&gt; <Items> <Item> <ItemAttributes> <ListPrice> ...

In Multiple inheritance, which class should be listed first, My-App-Class or Language-Framework-Class

In Python, while designing a multiple inheritance subclass, should I place my app class first or language/framework/third-party class first? Based on your experience which do you recommend: 1) My app class before a native or third party class class MyClass(MyAppBaseClass, SomeLibraryOrNativePythonClass): or 2) A native or third p...

Is this a good approach to avoid using SQLAlchemy/SQLObject?

Rather than use an ORM, I am considering the following approach in Python and MySQL with no ORM (SQLObject/SQLAlchemy). I would like to get some feedback on whether this seems likely to have any negative long-term consequences since in the short-term view it seems fine from what I can tell. Rather than translate a row from the database ...

Proper way to declare custom exceptions in modern Python?

What's the proper way to declare custom exception classes in modern Python? My primary goal is to follow whatever standard other exception classes have, so that (for instance) any extra string I include in the exception is printed out by whatever tool caught the exception. By "modern Python" I mean something that will run in Python 2.5 ...

Key-ordered dict in python

Hello, I am looking for a solid implementation of an ordered associative array (in terms of keys, not of insertion order). More precisely, I am looking for a space-efficent implementation of a int-to-float (or string-to-float for another use case) mapping structure for which: Ordered iteration is O(n) Random access is O(1) The bes...

How to submit data of a flash form? [python]

Hi. I would like to know if it is possible to submit a flash form from python and, if it is, how? I have done form submitting from python before, but the forms were HTML not flash. I really have no idea on how to do this. In my research about this I kept getting 'Ming'. However, Ming is only to create .swf files and that's not what I in...

Boost::tuple's equivalent to Python's itemgetter?

I have some code that looks like this: typedef tuple<int, int, double> DataPoint; vector<DataPoint> data; vector<double> third_column_only; // Code to read in data goes here. transform(data.begin(), data.end(), back_inserter(values), tuples::get<1, DataPoint>); Unfortunately, the last line doesn't compile - it gives me a message like ...

urlopen, BeautifulSoup and UTF-8 Issue

I am just trying to retrieve a web page, but somehow a foreign character is embedded in the HTML file. This character is not visible when I use "View Source." isbn = 9780141187983 url = "http://search.barnesandnoble.com/booksearch/isbninquiry.asp?ean=%s" % isbn opener = urllib2.build_opener() url_opener = opener.open(url) page = url_ope...

How to encrypt a string using the key

I have a 'public key' in a variable named varkey, for gettting the public key i used the urllib and stored that public key in a variable now i want to encrypt a msg/string using the public key . its ok if somebody guide me for any API library Thanks ...

Good graph traversal algorithm

Abstract problem : I have a graph of about 250,000 nodes and the average connectivity is around 10. Finding a node's connections is a long process (10 seconds lets say). Saving a node to the database also takes about 10 seconds. I can check if a node is already present in the db very quickly. Allowing concurrency, but not having more tha...

Count number of files with certain extension in Python

I am fairly new to Python and I am trying to figure out the most efficient way to count the number of .TIF files in a particular sub-directory. Doing some searching, I found one example (I have not tested), which claimed to count all of the files in a directory: file_count = sum((len(f) for _, _, f in os.walk(myPath))) This is fine, ...

Detect and delete locked file in python

I want to detect whether a file is locked, using python on Unix. It's OK to delete the file, assuming that it helps detects whether the file was locked. The file could have been originally opened exclusively by another process. Documentation seems to suggest that os.unlink won't necessarily return an error if the file is locked. Ideas...

How to start a COM server implemented in python?

I am using python for making a COM local server. In fact, the whole COM part is implemented in a dll and my python script is calling that dll thanks to ctypes. It works ok when I run the script manually. I would like to see my server automatically ran when a COM client request it. I know that it is possible by giving the command line as...

How to extend distutils with a simple post install script?

I need to run a simple script after the modules and programs have been installed. I'm having a little trouble finding straight-forward documentation on how to do this. It looks like I need to inherit from distutils.command.install, override some methods and add this object to the setup script. The specifics are a bit hazy though and it s...