python

How can i write my own aggregate functions with sqlalchemy?

How can I write my own aggregate functions with SQLAlchemy? As an easy example I would like to use numpy to calculate the variance. With sqlite it would look like this: import sqlite3 as sqlite import numpy as np class self_written_SQLvar(object): def __init__(self): import numpy as np self.values = [] def step(self, value)...

Refactor this block cipher keying function

I found a simple pure python blowfish implementation that meets my needs for a particular project. There's just one part of it that bothers me: def initialize(key): """ Use key to setup subkeys -- requires 521 encryptions to set p and s boxes. key is a hex number corresponding to a string of 32 up to 448 1s and 0s -- k...

Unit testing for exceptions in Python constructor

Hello, I'm just a beginner in Python and programming in general, and have some questions about the unittest module. I have a class, and in the __init__ method I am doing some assertions to check for bad arguments. I would like to create a unittest which checks for such AssertionError when creating new instances. In the unittest modul...

Why is my bubble sort in Python so slow?

I have the following code thats use bubble sort to invert a list and has a worst time performance: for i in xrange(len(l)): for j in xrange(len(l)): if l[i]>l[j]: l[i], l[j] = l[j], l[i] In some cases (when len(l) = 100000) the code spend more then 2h to complete execute, I think its so strange, please correct ...

Any yahoo messenger lib for python?

Is there any lib available to connect to yahoo messenger using either the standard protocol or the http way from python? ...

Looking for Windows Text Editor which supports GIT

Hello all, I am looking for a Text Editor on Windows which is integrated with GIT (check out, check in from the UI). Also, it would be nice is this editor could also support Python syntax highlighting. Is there anything like that available? Thanks! ...

python and %s

What does %s mean in python? And does this bit of code translate to? for instance... if len(sys.argv) < 2: sys.exit('Usage: %s database-name' % sys.argv[0]) if not os.path.exists(sys.argv[1]): sys.exit('ERROR: Database %s was not found!' % sys.argv[1]) Thanks ...

System theme icons and PyQt4

I'm writing a basic program in python using the PyQt4 module. I'd like to be able to use my system theme's icons for things like the preference dialog's icon, but i have no idea how to do this. So my question is, how do you get the location of an icon, but make sure it changes with the system's icon theme? If it matters, i'm developing t...

Python urllib2 timeout when using Tor as proxy?

Hi, I am using Python's urllib2 with Tor as a proxy to access a website. When I open the site's main page it works fine but when I try to view the login page (not actually log-in but just view it) I get the following error... URLError: <urlopen error (10060, 'Operation timed out')> To counteract this I did the following: import soc...

Netbeans Python debugger + unittest = attributeError

Running a unittest test using the netbeans debugger gives the error when reaching unittest.main: unittest.main() ', ' File "/Applications/NetBeans/NetBeans 6.7 RC1.app/Contents/Resources/NetBeans/python1/jython-2.5/Lib/unittest.py", line 767, in __init__ self.parseArgs(argv) ', ' File "/Applications/NetBeans/NetBeans 6.7 RC1.a...

Hash method and UnicodeEncodeError

In Python 2.5, I have the following hash function: def __hash__(self): return hash(str(self)) It works well for my needs, but now I started to get the following error message. Any idea of what is going on? return hash(str(self)) UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in position 16: ordinal not in range(...

Unable to have a command line parameter in Python

I run import sys print "x \tx^3\tx^3+x^3\t(x+1)^3\tcube+cube=cube+1" for i in range(sys.argv[2]): // mistake here cube=i*i*i cube2=cube+cube cube3=(i+1)*(i+1)*(i+1) truth=(cube2==cube3) print i, "\t", cube, "\t", cube + cube, "\t", cube3, "\t", truth I get Traceback (most recent call last): File ...

How can I get the order of an element attribute list using Python xml.sax?

How can I get the order of an element attribute list? It's not totally necessary for the final processing, but it's nice to: in a filter, not to gratuitously reorder the attribute list while debugging, print the data in the same order as it appears in the input Here's my current attribute processor which does a dictionary-like pass ...

Make my code handle in the background function calls that take a long time to finish

Certain functions in my code take a long time to return. I don't need the return value and I'd like to execute the next lines of code in the script before the slow function returns. More precisely, the functions send out commands via USB to another system (via a C++ library with SWIG) and once the other system has completed the task, it ...

Django ImportError at / no matter what I do

So I've just started playing around with Django and I decided to give it a try on my server. So I installed Django and created a new project, following the basics outlined in the tutorial on Djangoproject.com Unfortunatly, no matter what I do, I can't get views to work: I constantly get ImportError at / No module named index Here i...

Handle either a list or single integer as an argument

A function should select rows in a table based on the row name (column 2 in this case). It should be able to take either a single name or a list of names as arguments and handle them correctly. This is what I have now, but ideally there wouldn't be this duplicated code and something like exceptions would be used intelligently to choose ...

Is &#x10; a valid character in XML?

On this data: <row Id="37501" PostId="135577" Text="...uses though.&#x10;"/> I'm getting an error with the Python sax parser: xml.sax._exceptions.SAXParseException: comments.xml:29776:332: reference to invalid character number I trimmed the example; 332 points to "&#x10;". Is the parser correct in rejecting this character? ...

Writing tests for Django's admin actions

I'm using Django 1.1 beta and hoping to use admin actions. I have to write unit tests for those, but I don't get it how to write tests for them. For normal view handler functions, I can use Django's TestClient to simulate http request/response, but how should it be done with admin actions? ...

xml.dom.minidom Document() in Python/django outputting memory location

I'm learning Python and django at the same time. I'm trying to create an xml document to return some XML from a view. I'm using the django development server at the moment and I keep getting this information spitting out in my views instead of the document I tried to create. Here's my code from django.http import HttpResponse from ...

Question on importing a GPL'ed Python library in commercial code

Hello all, We're evaluating a couple of Python libraries for Graph manipulation. We tried 'networkx' (http://networkx.lanl.gov/) and 'igraph' (http://igraph.sourceforge.net/). While both are excellent modules, igraph is faster due to its nature - it's a Python wrapper over libigraph - a blistering fast graph C library (uses LAPACK etc...