python

Python generator that returns the same thing forever

I'm looking for a standard function that does this: def Forever(v): while True: yield v It seems so trivial I can't believe there isn't a standard version. For that matter anyone know of a good link to a list of all the standard generator functions? ...

How to protect text when doing INSERT using MySQLdb

This query is easy, but when the text contains some quotes it doesn't work. cursor.execute ("INSERT INTO text (text_key, language_id, text) VALUES ('%s', '%s', '%s')" % (key, language_id, text)) What is the best way to protect my text variable ? ...

programing pseudocode

I'm trying to make the game KenKen in Python. I need some help with the pseudocode. What data types required to store and process the game information as it progresses and completes? ...

Creating a python priority Queue

Hello everyone, I would like to build a priority queue in python in which the queue contains different dictionaries with their priority numbers. So when a "get function" is called, the dictionary with the highest priority(lowest number) will be pulled out of the queue and when "add function" is called, the new dictionary will be added ...

Using serial over lan with python

Hi, I'd like to write a python script to gather data from serial over lan but I can't seem to find a place to start with IPMI. I've looked at OpenIPMI's python bindings but there doesn't seem to be any documentation. Perhaps I can use the subprocess module and ipmitool? I'm not sure that would be simple though. Does anyone have any e...

Make a line into a string

I got a bot from this website, and I've been having a lot of fun with it. However I wanted to add an away command. The way it's gonna work is that when someone write away [reason] then it saves the reason, and when someone else types his name, the bot sais "He is not available, he left a note '[reason]'" or something like that. The ...

twisted, unblock a threads.blockingCallFromThread when the reactor stops

it seems threads.blockingCallFromThread keeps blocking even when the reactor stops. is there any way to un-block it? the deferred that it is blocking on relies on an RPC coming from the other end, and that definitely won't come in with the reactor stopped. ...

How to default to CLoader in PyYaml.load

Is there any way to have the default loader for PyYaml be CLoader. So instead of having to do yaml.load(f, Loader=yaml.CLoader) It would just default to CLoader, so I could do: yaml.load(f) ...

Globbing with python rpm module?

The following code uses the rpm module to query the version of an installed package. What I would like to do is to query a set of packages specified by a glob, for example searching for "python*" rather than "python". Is this possible using the rpm module? 1 #!/usr/bin/python 2 3 import rpm 4 5 ts = rpm.TransactionSet() ...

How can i convert Integer to Signed Word in Python (for use with PySerial)?

Hi I'm having some problems making Python talk to a hardware display using pyserial. Some of the display's functions require Signed Word to be sent as arguments after commands (ie. X or Y on display screen). I've been getting by with chr() previously, but that only works with numbers < 255. I've tried the following for conversion but ...

django URL reverse: When URL reversig a username it fails when username has a '.' literal in it

I didn't expect this to occur [since I didn't know when django changed to allow _ and . in usernames], but when I attempt {% url feed_user entry.username %} I will get a 500 error when the username contains a '.' In this case rob.e as a username will fail. Any ideas how to deal with this? ...

Calling method from a class in a different class in python

Let's say I have this code: class class1(object): def __init__(self): #don't worry about this def parse(self, array): # do something with array class class2(object): def __init__(self): #don't worry about this def parse(self, array): # do something else with array I want to be abl...

Windows Balloon-tooltips in Python

Following the example at http://article.gmane.org/gmane.comp.python.general/541418, I've succeeded in creating a callable class for balloon tooltips, but the greater complexities of that code elude me when it comes to customization. I browsed a bit of how it works through msdn, but being a novice at more windows-esque languagues like c a...

Implementing RSA in python

Hi All, i am trying to implement RSA in python(i am new to python) for my course, the problem i have is the code i have written does not work for numbers with more than 4 digits. any idea why this is happening? please advice p =0 q=0 n=0#modules phyPQ = 0 e = 0 #public key exponent d = 0#private key exponent c = '' m = '' def getPrimes...

Why does an apostrophe in a python docstring break emacs syntax highlighting?

Running GNU Emacs 22.2.1 on Ubuntu 9.04. When editing python code in emacs, if a docstring contains an apostrophe, emacs highlights all following code as a comment, until another apostrophe is used. Really annoying! In other words, if I have a docstring like this: ''' This docstring has an apostrophe ' ''' Then all following code is...

How can you get unittest2 and coverage.py working together?

In theory something like coverage run unit2 discover Should work but it currently just errors out. If you are a nose user which will be the equivalent of nosetests --with-coverage ...

PIL: Composite / merge two images as "Dodge"

How do I use PIL to implement the equivalent of merging a layer in "dodge" mode with another layer (as done in Gimp/Photoshop)? I have my original image as well as the image I'd like to use as the layer to merge with, but I don't how to do the dodge merge/composite: from PIL import Image, ImageFilter, ImageOps img = Image.open(fname) ...

from ... import * with __import__ function

How would I do the from ... import * with the __import__ function? The reason being that i only know the name of the file at runtime and it only has 1 class inside that file. ...

How to Convert Extended ASCII to HTML Entity Names in Python?

I'm currently doing this to replace extended-ascii characters with their HTML-entity-number equivalents: s.encode('ascii', 'xmlcharrefreplace') What I would like to do is convert to the HTML-entity-name equivalent (i.e. &copy; instead of &#169;). This small program below shows what I'm trying to do that is failing. Is there a way to ...

elegant way to test python ASTs for equality (not reference or object identity)

Not sure of the terminology here, but this would be difference between eq? and equal? in scheme, or the difference between == and strncmp with C strings; where in each case the first would return false for two different strings that actually have the same content and the second would return true. I'm looking for the latter operation, f...