python

Solving thread cleanup on paramiko

I have a automated process using python/paramiko anf have this error: Exception in thread Thread-1 (most likely raised during interpreter shutdown) .... .... <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'error' I understand that is a problem in the cleanup/threading, but not see how fix it... I have the l...

Structured programming and Python generators?

Note: This question mutated a bit as people answered and forced me to "raise the stakes", as my trivial examples had trivial simplifications; rather than continue to mutate it here, I will repose the question when I have it clearer in my head, as per Alex's suggestion. Python generators are a thing of beauty, but how can I easily brea...

How to allow a many-to-many relationship to be configured in Python

I have a simple app which requires a many-to-many relationship to be configured as part of its set-up. For example, the app requires a list of repository URLs, a list of users and for each user, a subset of the repository URLs. I first thought of using a config.py file similar to the following: repositories = { 'repo1': 'http://sv...

What's the most Pythonic XHTML/HTML parser/generator/template module that supports DOM like access?

It should be able to create, modify and read X/HTML in a highly object oriented way that still feels DOM like but is not obese, and is really Pythonic. Preferably it would deal with malformed HTML too, but we can skip this for templates. For example, I'd like to do this: >> from someAmazingTemplate import * >> html = Template('<html><h...

QtPython Qtreewidget Problem

Hello Everybody, I trying to do a Qtreewidget to attend a customer design suggestion. I am coding it on QtPython. I did a first try using Qt Designer, then generated the code. But when I try to run it, an error comes out: self.centralwidget.setSortingEnabled(__sortingEnabled) AttributeError: setSortingEnabled I googled around, but d...

Validating Uploaded Files in Django

A Django app that I am working has an Event model. An Event may have associated photos, static html files and pdf files. I would like to allow trusted users to upload these files, but I am wary about security, especially having read the following in the Django docs (link). Note that whenever you deal with uploaded files, you shoul...

how can I filter on the second element in a tuple of tuples?

In my model I have a field: country = models.CharField(_('Country'), max_length=2, choices=COUNTRIES) Where COUNTRIES is a tuple of tuples like this: COUNTRIES = ( ('AF', _('Afghanistan')), ... and so on Now I want to filter an instance of that model, by the country name. This: i = MyModel.objects.filter(country__iexac...

Python tabstop-aware len() and padding functions

Python's len() and padding functions like string.ljust() are not tabstop-aware, i.e. they treat '\t' like any other single-width character, and don't round len up to the nearest multiple of tabstop. Example: len('Bear\tnecessities\t') is 17 instead of 24 ( i.e. 4+(8-4)+11+(8-3) ) and say I also want a function pad_with_tabs(s) such ...

Regex, how to remove all non-alphanumeric except colon in a 12/24 hour timestamp?

I have a string like: Today, 3:30pm - Group Meeting to discuss "big idea" How do you construct a regex such that after parsing it would return: Today 3:30pm Group Meeting to discuss big idea I would like it to remove all non-alphanumeric characters except for those that appear in a 12 or 24 hour time stamp. All help is appreciated. T...

Restful library like httpLib2 For .NET

Anyone know of a library/framework like httplib2 (python) http://code.google.com/p/httplib2/ for .NET? It wouldn't be difficult to duplicate it, but would be a waste if it already existed some where. Thanks. ...

Why won't my Python scatter plot work?

I created a very simple scatter plot using pylab. pylab.scatter(engineSize, fuelMile) pylab.show() The rest of the program isn't worth posting, because it's that line that's giving me the problem. When I change "scatter" to "plot" it graphs the data, but each point is part of a line and that makes the whole things a scribbly mess. I j...

python csv headers

I have set of csv headers that i am trying to match with uploads, it's not really working. Not all headers are required, just have to match what's in file reader = csv.DictReader(open(PathFile)) headers = reader.fieldnames for header in sorted(set(headers)): if (header == 'ip') or (header == 'IP'): pr...

Python in Vim buffer?

Is it possible to have a Python interpreter open in a Vim buffer? Something like: ________________________ | | | | | my python script | | | | | ------------------------ | | | python interpreter | ------------------------ Rig...

Bitwise Operation and Usage

x = 1 # 0001 x << 2 # Shift left 2 bits: 0100 # Result: 4 x | 2 # Bitwise OR: 0011 # Result: 3 x & 1 # Bitwise AND: 0001 # Result: 1 I can understand the arithmetic operators in Python (and other langs), but I never understood 'bitwise' operators quite well. In the above example (from a Python book), I und...

Minimal Python Installation

Various software installations on my laptop each require their own particular version of Python. ViewVC requires Python 2.5 and Blender requires Python 2.6. Mercurial (thankfully) comes with its Python interpreter packaged in a DLL in the Mercurial installation itself. How do I get by without having to install the entire Python environm...

Python ValueError: insecure string pickle

Python problem: When I am trying to load sth I dumped using cPickle, I get the error message: ValueError: insecure string pickle Both the dumping and loading work are done on the same computer, thus same OS: ubuntu 8.04. How could I solve this problem? ...

Help with manage.py syncdb

Hello there, I am a newbie learning Python/Django... Am using the following tutorial located at: http://bit.ly/eIdT Created a mysite database in MySQL 5 running on Snow Leopard. Edited the settings.py file to look like this: DATABASE_ENGINE = 'mysql' DATABASE_NAME = 'mysite' DATABASE_USER = 'root' ...

Is Java or Python better for writing a web-page-source-checking web service on Google App Engines?

Hello everybody!!! I hope you all have a nice day. I want to write a web service that would check some web page HTML code every 20 minutes and e-mail it to my mail box. Here I was given a suggestion to use Google App Engine for this task. Having briefly read through that site I learned that two languages could be used there: Java and P...

python reduce error?

The following is my python code: >>> item = 1 >>> a = [] >>> a.append((1,2,3)) >>> a.append((7,2,4)) >>> sums=reduce(lambda x:abs(item-x[1]),a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: <lambda>() takes exactly 1 argument (2 given) >>> How can I fix it? Thanks! ...

How to kill headless X server started via Python?

I want to get screenshots of a webpage in Python. For this I am using http://github.com/AdamN/python-webkit2png/ . newArgs = ["xvfb-run", "--server-args=-screen 0, 640x480x24", sys.argv[0]] for i in range(1, len(sys.argv)): if sys.argv[i] not in ["-x", "--xvfb"]: newArgs.append(sys.argv[i]) logging.debug(...