python

datastore transaction restrictions

in my google app application, whenever a user purchases a number of contracts, these events are executed (simplified for clarity): user.cash is decreased user.contracts is increased by the number contracts.current_price is updated. market.no_of_transactions is increased by 1. in a rdms, these would be placed within the same transacti...

Form Submission in Python Without Name Attribute

Background: Using urllib and urllib2 in Python, you can do a form submission. You first create a dictionary. formdictionary = { 'search' : 'stackoverflow' } Then you use urlencode method of urllib to transform this dictionary. params = urllib.urlencode(formdictionary) You can now make a url request with urllib2 and pass the var...

How do I invoke Python code from Ruby?

Does a easy to use Ruby to Python bridge exist? Or am I better off using system()? ...

Find the oldest file (recursively) in a directory.

I'm writing a Python backup script and I need to find the oldest file in a directory (and its sub-directories). I also need to filter it down to *.avi files only. The script will always be running on a Linux machine. Is there some way to do it in Python or would running some shell commands be better? At the moment I'm running df to get...

python random.random() causes "'module' object is not callable" when used in custom template tag

If I start python from the command line and type: import random print "Random: " + str(random.random()) It prints me a random number (Expected, excellent). If I include the above-two lines in my django application's models.py and start my django app with runserver I get the output on the command line showing me a random number (Grea...

Working with subdomain in google app engine

How can I work with sub domain in google app engine (python). I wanna get first domain part and take some action (handler). Example:      product.example.com -> send it to products handler      user.example.com -> send it to users handler Actually, using virtual path I have this code: application = webapp.WSGIApplication( ...

Python dictionary deepcopy

Hello there, I was wondering in how does exactly deepcopy work in the following context: from copy import deepcopy def copyExample: self.myDict = {} firstPosition = "First" firstPositionContent = ["first", "primero"] secondPosition = "Second" secondPositionContent = ["second"] self.myDict[firstPosition] = fir...

Python cgi and stdin

I'm using pycurl to upload a file via put and python cgi script to receive the file on the server side. Essentially, the code on the server side is: while True: next = sys.stdin.read(4096) if not next: break #.... write the buffer This seems to work with text, but not binary files (I'm on windows). With binary files, t...

cursor.rowcount always -1 in sqlite3 in python3k

Hi, I am trying to get the rowcount of a sqlite3 cursor in my Python3k program, but I am puzzled, as the rowcount is always -1, despite what python3 docs say (actually it is contradictory, it should be None). Even after fetching all the rows, rowcount stays at -1. Is it a Sqlite3 implementation bug? A Sqlite3 bug? I have already checked ...

Twisted and p2p applications

Can you tell me: could I use twisted for p2p-applications creating? And what protocols should I choose for this? ...

Best Practices for Python Exceptions?

What are the best practices for creating exceptions? I just saw this, and i don't know if i should be horrified, or like it. I read several times in books that exceptions should never ever hold a string, because strings themselves can throw exceptions. Any real truth to this? Basically from my understanding from the scripts is that this...

Looking for interesting and instructive python code (web programming)

I'm learning python and my current focus is on web programming. Thx to some help from stackoverflow, I recently made some reasonable progress. While still at basic level, I can now "somewhat" comfortably use html templates, web frameworks (haven't done anything significant yet), sqlite (no mySQL yet), etc. Before I start another proje...

How to use time > year 2038 on official Windows Python 2.5

The official Python 2.5 on Windows was build with Visual Studio.Net 2003, which uses 32 bit time_t. So when the year is > 2038, it just gives exceptions. Although this is fixed in Python 2.6 (which changed time_t to 64 bit with VS2008), I'd like to use 2.5 because many modules are already compiled for it. So here's my question - is the...

Extracting a URL in Python

In regards to: http://stackoverflow.com/questions/720113/find-hyperlinks-in-text-using-python-twitter-related How can I extract just the url so I can put it into a list/array? Edit Let me clarify, I don't want to parse the URL into pieces. I want to extract the URL from the text of the string to put it into an array. Thanks! ...

What is the best source for a clear explanation of how to use the Python help?

A couple of days ago Bendin was kind enough to answer a question I had on how to handle some files that were uuencoded. When I described my response to his solution he went even further and showed me how to use a special function from the codecs module to better meet my needs. import codecs uudecode=codecs.getdecoder("uu") decoded_Gr...

Popen log management question

Problem: I have a monitor program in Python that uses subprocess' Popen to start new processes. These processes have the potential to run for a very long time (weeks-months). I'm passing a file handle to stdout variable in Popen and I'm worried that this file will get huge easily. Is there a way I can safely move or remove the data i...

Sort lexicographically?

I am working on integrating with the Photobucket API and I came across this in their api docs: "Sort the parameters by name lexographically [sic] (byte ordering, the standard sorting, not natural or case insensitive). If the parameters have the same name, then sort by the value." What does that mean? How do I sort something...

How do you alias a python class to have another name without using inheritance?

If I have a python class, how can I alias that class-name into another class-name and retain all it's methods and class members and instance members? Is this possible without using inheritance? e.g. I have a class like: class MyReallyBigClassNameWhichIHateToType: def __init__(self): <blah> [...] I'm creating an inte...

Computing the second (mis-match) table in the Boyer-Moore String Search Algorithm

For the Boyer-Moore algorithm to be worst-case linear, the computation of the mis-match table must be O(m). However, a naive implementation would loop through all suffixs O(m) and all positions in that that suffix could go and check for equality... which is O(m3)! Below is the naive implementation of table building algorithm. So this qu...

how do you store raw bytes as text without losing information in python 2.x?

Suppose I have any data stored in bytes. For example: 0110001100010101100101110101101 How can I store it as printable text? The obvious way would be to convert every 0 to the character '0' and every 1 to the character '1'. In fact this is what I'm currently doing. I'd like to know how I could pack them more tightly, without losi...