python

How to enforce unicode arguments for methods?

I have a model class with getter and setter methods, and the occasional static methods. I would like to enforce the usage of unicode strings as arguments for specific methods and using decorators was the first idea I had. Now I have something like this: import types class require_unicode(object): def __init__(self, function): ...

Get str repr with double quotes Python

I'm using a small Python script to generate some binary data that will be used in a C header. This data should be declared as a char[], and it will be nice if it could be encoded as a string (with the pertinent escape sequences when they are not in the range of ASCII printable chars) to keep the header more compact than with a decimal o...

Fastest Way To Remove Duplicates In Lists Python

I have two very large lists and to loop through it once takes at least a second and I need to do it 200,000 times. What's the fastest way to remove duplicates in two lists to form one? ...

Network analysis and adjacency matrices

I want to try and create a network for several hundred shapefiles that consist of polylines. The polylines are snapped to each other and consistent. Then I want to create an adjacency matrix for this network. What is the best way of doing this? I know how to do it on an individual basis by clicking through options within ArcCatalog, but...

Error while exiting cherrypy server

Guys, I am getting following error while exiting cherrypy server. What is this error about? 2009-11-04 09:32:35,015 WARNING Error in atexit._run_exitfuncs: 2009-11-04 09:32:35,015 WARNING 2009-11-04 09:32:35,015 WARNING Traceback (most recent call last): 2009-11-04 09:32:35,015 WARNING File "atexit.pyc", line 24, in _run_exitfu...

Using Google AppEngine as a "cache" for personal websites (wordpress blogs, wikis)

I read an article of an indie game developer who is using Google AppEngine to cache his main site and blog, to protect provide high-availability during traffic spikes (Digg, Slashdot effect). Wolfire Blog - Google App Engine for Indie Developers There's not a lot of detail on the exactly what they developed in Python on Google AppEngin...

How do I create a namespace package in Python?

In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories Package-1 and Package-2 in PYTHONPATH, Package-1/namespace/__init__.py Package-1/namespace/module1/__init__.py Package-2/namespace/__in...

How to combine Pool.map with Array (shared memory) in Python multiprocessing?

I have a very large (read only) array of data that I want to be processed by multiple processes in parallel. I like the Pool.map function and would like to use it to calculate functions on that data in parallel. I saw that one can use the Value or Array class to use shared memory data between processes. But when I try to use this I get...

Comparing elements in a list in Python's for -loop

What is wrong in the method end in the code? The method end returns always 1 although it should return 0 with the current data. # return 1 if the sum of four consecutive elements equal the sum over other sum of the other three sums # else return 0 # Eg the current sums "35 34 34 34" should return 0 data = "2|15|14|4|12|6|7|9|8|10|...

Is python or ruby good for penetration testing?

Is python or ruby good for penetration testing? I hear python is very good for pentesting. It has got good modules for that. But not a good framework like metasploit. ...

Computing article abstracts

I'm looking for a way to automatically produce an abstract, basically the first few sentances/paragraphs of a blog entry, to display in a list of articles (which are written in markdown). Currently, I'm doing something like this: def abstract(article, paras=3): return '\n'.join(article.split('\n')[0:paras]) to just grab the first...

Feedparser - retrieve old messages from Google Reader

I'm using the feedparser library in python to retrieve news from a local newspaper (my intent is to do Natural Language Processing over this corpus) and would like to be able to retrieve many past entries from the RSS feed. I'm not very acquainted with the technical issues of RSS, but I think this should be possible (I can see that, e.g...

Writing a re-usable unittest.TestCase method

I'm writing tests using the unittest package, and I want to avoid repeated code. I am going to carry out a number of tests which all require a very similar method, but with only one value different each time. A simplistic and useless example would be: class ExampleTestCase(unittest.TestCase): def test_1(self): self.assertEq...

Can you only communicate once with a subprocess?

communicate's documentation says: Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. What do you do if you need to send input to a process more than once ? For example, I spawn a process, send it some data, the process does something with that, ...

How to pass flag to gcc in Python setup.py script?

I'm writing a Python extension in C that requires the CoreFoundation framework (among other things). This compiles fine with: gcc -o foo foo.c -framework CoreFoundation -framework Python ("-framework" is an Apple-only gcc extension, but that's okay because I'm using their specific framework anyway) How do I tell setup.py to pass this...

Pyfacebook from buildout

What is the best way to install the latest version of pyfacebook with buildout? The package is hosted on github and is not on pypi. This system doesn't have git installed, so a git-based recipe isn't unfortunately not an option. The github URL is http://github.com/sciyoshi/pyfacebook. TIA! ...

Trying to upgrade Python to 3.0 on Mac OS 10.5.8

I'm having some problems upgrading Python on my Mac. For my first attempt, I downloaded and installed the 2.6.4 dmg MacPython installer from http://python.org/download/mac/. This did install 2.6.4, and when I ran 'python' from the terminal it says that version. However, I also had a test script where I am doing: import os, json But I...

Python: How do I get a reference to a module inside the module itself?

How can I get a reference to a module from within that module? Also, how can I get a reference to the package containing that module? ...

how to change default dateformat AAAA-MM-DD to dd/mm/yyyy in Django?

My locale dateformat is dd/mm/yyyy and Django is requering AAAA-MM-DD when the user inputs 04/11/2009 Django raises: _('Enter a valid date in YYYY-MM-DD format.')) Im using simple html input field type="text", Not forms.DateField Thanks :) ...

Overriding the newline generation behaviour of Python's print statement

I have a bunch of legacy code for encoding raw emails that contains a lot of print statements such as print >>f, "Content-Type: text/plain" This is all well and good for emails, but we're now leveraging the same code for outputting HTTP request. The problem is that the Python print statement outputs '\n' whilst HTTP requires '\r\n'. ...