python

Method to save networkx graph to json graph?

Seems like there should be a method in networkx to export the json graph format, but I don't see it. I imagine this should be easy to do with nx.to_dict_of_dicts(), but would require a bit of manipulation. Anyone know of a simple and elegant solution? ...

Google App Engine: "Cannot create a file when that file already exists"

I'm running the Google App Engine devserver 1.3.3 on Windows 7. Usually, this method works fine, but this time it gave an error: def _deleteType(type): results = type.all().fetch(1000) while results: db.delete(results) results = type.all().fetch(1000) The error: File "src\modelutils.py", line 38, in _delete...

How to locate and access named ranges (global, nd per-worksheet) using xlrd, Python?

The documentation for xlrd here http://www.python-excel.org/ mentions that it is now possible in latest version, but does not say how. ...

Python2.6 XML reading

Hey Guys, I want to migrate my system from Active Python 2.4 to Python 2.6.5. However I face some problem in parsing XML files. The I/O is very slow. My sample xml file <config><dicts><dictName>EnvDict</dictName><dictElems><key>AppServerIP</key> <value>localhost</value><key>DBServerIP</key> <value>localhost</value><key>DBServerName...

Escape arguments for paramiko.SSHClient().exec_command

What is the best way to escape a string for safe usage as a command-line argument? I know that using subprocess.Popen takes care of this using list2cmdline(), but that doesn't seem to work correctly for paramiko. Example: from subprocess import Popen Popen(['touch', 'foo;uptime']).wait() This creates a file named literally foo;uptime,...

Better Way to Write This List Comprehension?

I'm parsing a string that doesn't have a delimiter but does have specific indexes where fields start and stop. Here's my list comprehension to generate a list from the string: field_breaks = [(0,2), (2,10), (10,13), (13, 21), (21, 32), (32, 43), (43, 51), (51, 54), (54, 55), (55, 57), (57, 61), (61, 63), (63, 113), (113, 163), (163, 213...

Can't build readline when trying to install Python 2.6.5 in Debian 4.3.2

I am trying to install Python 2.6.5 on my web server running Debian 4.3.2.1-1. I unpacked the tarball, ran "./configure --prefix /usr/", then ran "make". I saw this message. Failed to find the necessary bits to build these modules: _bsddb _hashlib _ssl _tkinter bsddb185 bz2 ...

what next after pyparsing?

I have a huge grammar developed for pyparsing as part of a large, pure python application. I have reached the limit of performance tweaking and I'm at the point where the diminishing returns make me start to look elsewhere. Yes, I think I know most of the tips and tricks and I've profiled my grammar and my application to dust. What next...

python application development resources (as in books/online guides)

can anyone recommend a resource (book,tutorial,etc.) that focuses on application development in python? something similar to Practical Django Projects, but for stand alone applications instead of web apps (for now). ...

Fast lookup for dictionary vector to a given vector. High dimensions

I'm looking for an answer that scales, but for my specific purpose, I have a 48th dimension vector. This could be represented as an array of 48 integers all between 0 and 255. I have a large dictionary of these vectors, approximately 25 thousand of them. I need to be able to take a vector that may or may not be in my database, and qui...

Storing Application Sensitive Data

Hi everybody, I am implementing a python application that will connect to our different servers and computers. They all have different logins and passwords. I want to store all these information in the app directly and ask for one master login/password only. How can I store all these sensitive data in the application so that someone who...

How can I manage these subprocesses efficiently?

I've got a script that downloads and then converts videos. I'm trying to do all of the downloads at once(a lot of wgets) and then when they're done, convert them. Right now I have to wait while each file downloads individually and then convert when done. I want all the download requests to run concurrently. Here's the part in my 'downlo...

Thread safety in Python (Question how it works)

I've read through the documentation on threading for python and as I've pereceived it the following should hold true: You can access (read) any PoD or python specific object (such as an array) without causing failure in a multi-threaded program trying the same thing at the same time, but you can not change them and accept thread integrit...

Optimizing a Partition Function

Here is the code, in python: # function for pentagonal numbers def pent (n): return int((0.5*n)*((3*n)-1)) # function for generalized pentagonal numbers def gen_pent (n): return pent(int(((-1)**(n+1))*(round((n+1)/2)))) # array for storing partitions - first ten already stored partitions = [1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42] #...

How to handle post request in python

Hello, how can I handle post request in python script? Somewhere I want to send it from ajax to the python script with given params. What's correct way to handle that data in python? ...

Cherrypy multithreading example

I do know that cherrypy is a multithreaded and also has a threadpool implementation. So I wanted to try an example showing multithreaded behaviour. Now lets say I've my some function in the root class and rest all things are configured def testPage(self, *args, **kwargs): current = threading.currentThread() print 'Starting ' , c...

How to bypass url mapping in django & make url directly point to file

Hello, I'm new to web development using python. I've good amount of experience in building dynamic websites using PHP. Also, I've never used MVC on PHP. For the first time I'm using MVC (or MTV to be more correct). I'm following One thing different from PHP world is that. URLs doesn't point to files but to functions. This single point...

opening a pcap file in python

I'm trying to open a .pcap file in python. can anyone help with this? each time I try this, it gives an error message that "IOError: [Errno 2] No such file or directory: 'test.pcap'" import dpkt f = open('test.pcap') pcap = dpkt.pcap.Reader(f) ...

Stop generator from block in Python

I have a generator that yields nodes from a Directed Acyclic Graph (DAG), depth first: def depth_first_search(self): yield self, 0 # root for child in self.get_child_nodes(): for node, depth in child.depth_first_search(): yield node, depth+1 I can iterate over the nodes like this for node, depth in graph.d...

Storing 'struct' data to binary file

Dear all, I need to store a binary file with a 12 byte header composed of 4 fields. They are namely: sSamples (4-bytes integer), sSampPeriod (4-bytes integer), sSampSize (2-bytes integer), and finally sParmKind (2-bytes integer). I'm using 'struct' to my variables to the desired fields. Now that I have them defined separately, how could...