python

lazy load or early load for python?

Hi All: We've got the following code sample: big_static_data = { "key1" : { "subkey1" : "subvalue1", ... }, "key2" : ... } class StaticDataEarlyLoad: def __init__(self): self.static_data = big_static_data # other init def handle_use_id(self, id): return complex_handle(self.static_d...

Pass XML fragments as stylesheet paramters with lxml?

I'm starting to use lxml in Python for processing XML/XSL documents, and in general it seems very straight forward. However, I'm not able to find a way to pass an XML fragment as a stylesheet parameter when doing a translation. For example, in PHP it is possible to pass DOMDocument XML fragments as stylesheet parameters, so that one can...

What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?

I just updated Python to 2.6.4 on my Mac. I installed from the dmg package. The binary did not seem to correctly set my Python path, so I added '/usr/local/lib/python2.6/site-packages' in .bash_profile pprint.pprint(sys.path) ['', '/Users/Bryan/work/django-trunk', '/usr/local/lib/python2.6/sit...

How to specify a baseDN when connecting to LDAP via python?

I want to connect to a ldap server with python-ldap using a specific baseDN. import ldap baseDN="ou=unit,o=org.c=xx" # doesn't work #baseDN="" # works host="ldaps://test.org.xx:636" userDN="cn=proxyhlrb,ou=services,o=org,c=xx" passwd="secret" server=ldap.initialize(host+"/"+baseDN) server.bind_s(userDN,passwd,ldap.AUTH_SIMPLE) Wha...

removing the oldest element from a dictionary in python

I would like to know the best way to remove the oldest element in a dictionary in order to control the maximum dictionary size. example: MAXSIZE = 4 dict = {} def add(key,value): if len(dict) == MAXSIZE: old = get_oldest_key() # returns the key to the oldest item del dict[old] dict[key] = value add('a','1') # {'a': '1'} ad...

Checking files retrieved by Twisted's FTPClient.retrieveFile method for completeness.

I'm writing a custom ftp client to act as a gatekeeper for incoming multimedia content from subcontractors hired by one of our partners. I chose twisted because it allows me to parse the file contents before writing the files to disk locally, and I've been looking for occasion to explore twisted anyway. I'm using 'twisted.protocols.ftp.F...

Possible to sandbox Python configuration file?

I'm thinking of implementing a configuration file written in Python syntax, not unlike what Django does. While I've seen one or two SO questions about the merits of using executable code in configuration files, I'm curious whether there is a way to execute the config file code in a "sandbox" to prevent mistakes in the code from locking ...

Detach a subprocess started using python multiprocessing module

Hello, I would like to create a process using the mutliprocessing module in python but ensure it continues running after the process that created the subprocess exits. I can get the required functionality using the subprocess module and Popen, but I want to run my code as a function, not as a script. The reason I want to do this is to ...

Python: Fast extraction of intersections among all possible 2-combinations in a large number of lists

I have a dataset of ca. 9K lists of variable length (1 to 100K elements). I need to calculate the length of the intersection of all possible 2-list combinations in this dataset. Note that elements in each list are unique so they can be stored as sets in python. What is the most efficient way to perform this in python? Edit I forgot to ...

How to append a reading to a python list?

i am using GM862 module and i want to write the cordinates as it is in a file "cordinates.txt" but i get some error, this is the code i wrote: import MDM cordlist = [] f = open("cordinates.txt", 'w') def AcquiredPosition(): res = MDM.send('AT$GPSACP\r', 0) res = MDM.receive(30) if(res.find('OK') != -1): tmp = res.split("\r\...

How can I run a Python program over telnet?

How can I run a Python program so it outputs its STDOUT and inputs its STDIN to/from a remote telnet client? All the program does is print out text then wait for raw_input(), repeatedly. I want a remote user to use it without needing shell access. It can be single threaded/single user. ...

How to produce html unit test output in Python?

I'm looking for any way to display the results of python unit tests in an html summary. There are tools like this for Java and Ruby... haven't yet located any tools that seem to do this for Python. Are there any out there? JUnit html output: Ruby RSpec output: ...

python prompt with a bash like interface

I am using the python prompt to practice some refular expressions. I was wondering if theere was a way to use the up/down arrows (like bash) to cycle through the old commands typed. I know its possible since it works on python on cygwin/windows. thanks ...

What is the right way to organize python unittests into suites?

I have some test case classes organized in directories foo_tests foo_tests1.py foo_tests2.py ... bar_tests bar_tests1.py ... The test cases look like: foo_tests1.py: import unittest class FooTestsOne(unittest.TestCase): def test_1(): assert(1=1) def test_2(): #... How do you organize test s...

How to read line (from a file) and then append + print in python?

for line in file: print line In the code above when I change it to: for line in file: print line + " just a string" This only appends "just a string" to the last line PS: Python newbie ...

Deploying a python CGI app

I have developed a python CGI application which works just fine on my development box. My hosting provider however gives me little control of its server: I use a lot of custom stuff in my python environment (like sqlalchemy and mako templating) and the servers python version is far too old to be used. My question is: how do I set up a is...

Write a data string to a numpy character array?

I want to write a data string to a numpy array. Pseudo Code: d=numpy.zeros(10,dtype=numpy.character) d[1:6]='hello' Example result: d= array(['', 'h', 'e', 'l', 'l', 'o', '', '', '', ''], dtype='|S1') How can this be done with numpy most naturally and efficiently? I don't want for loops, generators, or anything iterative,...

What are the best iPython tutorials?

I've been using iPython for a few months and I'm always learning new features. It seems that many of the features are hidden. What tutorials would you recommend to learn more iPython features? ...

Python: Issue reading data lines multiple times from a file

Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date: 10212009 12 34 56 25 67 90 End Set +++++++++ Set: 2 Date: 10222009 34 56 89 25 67 89 End Set In the above example file, I want to print on...

How to merge duplicates in 2D python arrays

Hi, I have a set of data similar to this: No Start Time End Time CallType Info 1 13:14:37.236 13:14:53.700 Ping1 RTT(Avr):160ms 2 13:14:58.955 13:15:29.984 Ping2 RTT(Avr):40ms 3 13:19:12.754 13:19:14.757 Ping3_1 RTT(Avr):620ms 3 13:19:12.754 Ping3_2 RTT(Avr):210ms 4 13:14:58.955 13:15:29.984 Ping4 RTT(Avr):360ms 5 13:19:1...