python

Python: re..find longest sequence

I have a string that is randomly generated: polymer_str = "diol diNCO diamine diNCO diamine diNCO diamine diNCO diol diNCO diamine" I'd like to find the longest sequence of "diNCO diol" and the longest of "diNCO diamine". So in the case above the longest "diNCO diol" sequence is 1 and the longest "diNCO diamine" is 3. How would I go...

Python accessing multiple webpages at once

I have a tkinter GUI that downloads data from multiple websites at once. I run a seperate thread for each download (about 28). Is that too much threads for one GUI process? because it's really slow, each individual page should take about 1 to 2 seconds but when all are run at once it takes over 40 seconds. Is there any way I can shorten ...

Django, how to make a view atomic?

I have a simple django app to simulate a stock market, users come in and buy/sell. When they choose to trade, the market price is read, and based on the buy/sell order the market price is increased/decreased. I'm not sure how this works in django, but is there a way to make the view atomic? i.e. I'm concerned that user A's actions m...

Getting rows from XML using XPath and Python

I'd like to get some rows (z:row rows) from XML using: <rs:data> <z:row Attribute1="1" Attribute2="1" /> <z:row Attribute1="2" Attribute2="2" /> <z:row Attribute1="3" Attribute2="3" /> <z:row Attribute1="4" Attribute2="4" /> <z:row Attribute1="5" Attribute2="5" /> <z:row Attribute1="6" Attribute2="6" /> </rs:data...

Count occurrence of a character in a Python string

What's the simplest way to count the number of occurrences of a character in a string? e.g. count the number of times 'a' appears in 'Mary had a little lamb' ...

Python: Importing pydoc and then using it natively?

I know how to use pydoc from the command line. However, because of complicated environmental setup, it would be preferable to run it within a python script as a native API call. That is, my python runner looks a bit like this: import pydoc pydoc.generate_html_docs_for(someFile) However, it's not clear to me from the pydoc documentati...

Latin-1 and the unicode factory in Python

I have a Python 2.6 script that is gagging on special characters, encoded in Latin-1, that I am retrieving from a SQL Server database. I would like to print these characters, but I'm somewhat limited because I am using a library that calls the unicode factory, and I don't know how to make Python use a codec other than ascii. The script ...

Print current call stack in Python

In Python, how can I print the current call stack from within a method (for debugging purposes). ...

Composable Regexp in Python

Often, I would like to build up complex regexps from simpler ones. The only way I'm currently aware of of doing this is through string operations, e.g.: Year = r'[12]\d{3}' Month = r'Jan|Feb|Mar' Day = r'\d{2}' HourMins = r'\d{2}:\d{2}' Date = r'%s %s, %s, %s' % (Month, Day, Year, HourMins) DateR = re.compile(Date) Is anybody aware o...

Python search in lists of lists

Hi, I have a list of two-item lists and need to search for things in it. If the list is: list =[ ['a','b'], ['a','c'], ['b','d'] ] I can search for a pair easily by doing ['a','b'] in list Now, is there a way to see if I have a pair in which a string is present in just the second position? I can do this: for i in range (0, len(l...

Algorithm for BFS traveral of an acylic directed graph

I'm looking for an elegant Python program that does a BFS traveral of a DAG: Node A is connected to B (A->B) if A "depends on" B (think of python package Foo "depending upon" Bar: Foo->Bar). In a graph of about 7000 such nodes, I want to sort all nodes such that for all possible (i, j) where 1>=i<j<=7000 .. depends(Ni, Nj) is False. d...

Having Django serve downloadable files

I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded. For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt And on the server, I know that all downloadable files reside in a folder "/home/user/files/". Is there a way to mak...

Import Dynamically Created Python Files

I am creating python files through the course of running a python program. I then want to import these files and run functions that were defined within them. The files I am creating are not stored within my path variables and I'd prefer to keep it that way. Originally I was calling the execFile(<script_path>) function and then calling t...

Seeding random in django

In a view in django I use random.random(). How often do I have to call random.seed()? One time for every request? One time for every season? One time while the webserver is running? ...

Forced to use inconsistent file import paths in Python (/Django)

Hi I've recently been having some problems with my imports in Django (Python)... It's better to explain using a file diagram: - project/ - application/ - file.py - application2/ - file2.py In project/application/file.py I have the following: def test_method(): return "Working" The problem occurs in proj...

Looking for Python gui

I'm looking for the right Python gui library for a program I'm writing. I'm looking for one that is: 1. free 2. licensed so that I can sell the product I create 3. good looking (could you link to screen shot examples, if possible?) Thank you! ...

How to a query a set of objects and return a set of object specific attribute in SQLachemy/Elixir?

Suppose that I have a table like: class Ticker(Entity): ticker = Field(String(7)) tsdata = OneToMany('TimeSeriesData') staticdata = OneToMany('StaticData') How would I query it so that it returns a set of Ticker.ticker? I dig into the doc and seems like select() is the way to go. However I am not too familiar with the sql...

Fast, searchable dict storage for Python

Current I use SQLite (w/ SQLAlchemy) to store about 5000 dict objects. Each dict object corresponds to an entry in PyPI with keys - (name, version, summary .. sometimes 'description' can be as big as the project documentation). Writing these entries (from JSON) back to the disk (SQLite format) takes several seconds, and it feels slow. ...

Remove all occurences of a value from a Python list

In Python remove() will remove the first occurrence of value in a list. How to remove all occurrences of a value from a list, without sorting the list? This is what I have in mind. >>> x = [1, 2, 3, 4, 2, 2, 3] >>> def remove_values_from_list(the_list, val): while val in the_list: the_list.remove(val) >>> remove_v...

How to update an older C extension for Python 2.x to Python 3.x

I'm wanting to use an extension for Python that I found here, but I'm using Python 3.1 and when I attempt to compile the C extension included in the package (_wincon), it does not compile due to all the syntax errors. Unfortunately, it was written for 2.x versions of Python and as such includes methods such as PyMember_Get and PyMember_S...