python

What's a good general way to look SQLAlchemy transactions, complete with authenticated user, etc?

I'm using SQLAlchemy's declarative extension. I'd like all changes to tables logs, including changes in many-to-many relationships (mapping tables). Each table should have a separate "log" table with a similar schema, but additional columns specifying when the change was made, who made the change, etc. My programming model would be some...

Message instance has no attribute 'read' in Google app engine mail receive

Code in receive handler class LogSenderHandler(InboundMailHandler): def receive(self, mail_message): logging.info("Received a message from: " + mail_message.sender) #logging.info("Received a message from: " + mail_message.attachments) logging.info("Received a message from: " + mail_message.date) logging.info("Receiv...

Django 1.1.1: How should I store an empty IP address using PostgreSQL?

I am writing a Django application that stores IP addresses with optional routing information. One of the fields for the IP model I have created is nexthop (for next-hop routes), which will usually be empty. Originally we intended to use MySQL, but now project requirements have changed to use PostgreSQL. Here is a stripped down version...

Regular Expression search/replace help needed, Python

One rule that I need is that if the last vowel (aeiou) of a string is before a character from the set ('t','k','s','tk'), then a : needs to be added right after the vowel. So, in Python if I have the string "orchestras" I need a rule that will turn it into "orchestra:s" edit: The (t, k, s, tk) would be the final character(s) in the str...

How to use Emacs with Python

I am new to emacs and I want to use emacs for python development. I am using Ubuntu 9.10. I frustrated to getting emacs work with python. I use GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0). Here what I did. * Emacs come with python mode but it is confusing there are two types of mode one is python-mode.el and other o...

running a method within another method. python

I am calling a method within another. and the error for this script i am getting is NameError: name 'mnDialog' is not defined Is there a reason for it? I think it has something to do with executing a command which isn't on the global level. (i didn't have the impression that python has a global and local variable declaration.) What i...

Python: How do I read and parse a unicode utf-8 text file?

I am exporting UTF-8 text from Excel and I want to read and parse the incoming data using Python. I've read all the online info so I've already tried this, for example: txtFile = codecs.open( 'halout.txt', 'r', 'utf-8' ) for line in txtFile: print repr( line ) The error I am getting is: UnicodeDecodeError: 'utf8' codec can't dec...

grep -r in python

i'd like to implement the unix command 'grep -r' in a python function. i know about commands.getstatusoutput(), but for now i don't want to use that. i came up with this: def grep_r (str, dir): files = [ o[0]+"/"+f for o in os.walk(dir) for f in o[2] if os.path.isfile(o[0]+"/"+f) ] return [ l for f in files for l in open(f) if...

Python elevator simulation problem

I have a homework assignment that's really baking my noodle. It involves an elevator simulation that takes user inputs for the number of floors and the number of people using the elevator. the people's starting floor and destination floors are random numbers within the floors. I realize that my code is very sparse and that there's qui...

Problem with import curses.ascii

Hi, I am trying from curses.ascii import * to django project, but I get: No module named _curses, I am using Python 2.5, any suggestion? Anyway I only need isalpha() function to use.... ...

Working with bit streams

I have a base64 encoded bit stream, I want to work with. After decoding it with base64.b64decode I get a bytes object (Py3k btw) containing the decoded code. The problem is now, that I would like to work on that bytes object with bit-wise operations, such as shifting, bit wise and etc, but that is not possible as it is a bytes sequence. ...

Reasons for this disparity in execution speed?

I wrote a quick Python script to compare two files, each containing unordered hashes, in order to verify that both files are identical aside from order. Then I rewrote it in Ruby for educational purposes. The Python implementation takes seconds, while the Ruby implementation takes around 4 minutes. I have a feeling this is most likely ...

Using Python Yacc\Lex as a formula parser

At the moment i'm working on using the python implementation of Yacc/Lex to build a formula parser for converting strings of formulae into a set of class defined operands. So far i've been mostly successful but i've come to an empasse in defining the parsing rules due to ambiguity with parentheses and several shift/reduce errors. The B...

Signing messages with PyMe (GnuPG Python Wrapper)

Hi, I use PyMe to sign and verify messages. I have a problem setting a pass-phrase callback that will return a password for the private signature key whenever needed for signing. My code looks like this: def passphrase_callback(hint='', desc='', prev_bad=''): return 'password' class CryptoEngine: def init(self, user_id, passph...

WMD Preview Doesn't Match Output

I am using WMD in a google app situation whereby the site administrator can update the pages of the site and the users see the information. The preview function is working fine and I can see the text the way I want it to appear, but when I am in the users section, the markdown is being returned without the formatting - how can i fix th...

Where is the python imp module's source code? Or the common location of the file.

I'm using Python 2.6 on Ubuntu. ...

How to deal with deflated response by urllib2?

I currently use following code to decompress gzipped response by urllib2: opener = urllib2.build_opener() response = opener.open(req) data = response.read() if response.headers.get('content-encoding', '') == 'gzip': data = StringIO.StringIO(data) gzipper = gzip.GzipFile(fileobj=data) html = gzipper.read() Does it handle de...

Python Selector (URL routing library), experience/opinions?

Does anyone have opinions about or experience with Python Selector? It looks great, but I'm a bit put off by its "Alpha" status on pypi and lack of unit tests. I mostly like that its simple, self contained, and pure WSGI. All other url routers I've found assume I'm using django, or pylons, or paste, or pull in lots of other dependenci...

Re-format items inside list read from CSV file in Python

I have some lines in a CSV file like this: 1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.00",Some string,Some string 2 If you notice, some numbers are enclosed in " " and has a thousand separator ",". I want to remove the thousand separator and the double quote enclosure. For the qoute enclosure, I'm t...

Strip final 0 off a python string

#!/usr/bin/env python import os, sys, subprocess, time while True: print subprocess.call("xsel", shell=True); time.sleep(1); Takes an entry from the clipboard and prints it, every 1 second. Result: copied0 entry0 from0 clipboard0 I do not know why it returns the final 0, but it apparently stops me from using string stri...