python

Inplace substitution from config parser

Hi, I have a very tricky situation (for my standards) in hand. I have a script that needs to read script variable name from configparser. e.g. I need to read self.post.id from .cfg file and use it as a variable in the script. How do i achieve this? --Thanks Mohit EDIT - i suppose i was unclear in my query. The cfg file loo...

convert string to preexisting variable names

How do i convert a string to the variable name in python? e.g. if the program contains a object named, self.post,that contains a variable named, i want to do something like, somefunction("self.post.id") = |Value of self.post.id| ...

Turn a string into a valid filename in Python

I have a string that I want to use as a filename, so I want to remove all characters that wouldn't be allowed in filenames, using Python. I'd rather be strict than otherwise, so let's say I want to retain only letters, digits, and a small set of other characters like "_-.() ". What's the most elegant solution? The filename needs to be ...

How do I add a directory with a colon to PYTHONPATH?

The problem is simple: Using bash, I want to add a directory to my PYTHONPATH for ease of script execution. Unfortunately, the directory I want to use has a : in it. So I try each of the following export PYTHONPATH=${PYTHONPATH}:/home/shane/mywebsite.com:3344/ export PYTHONPATH=${PYTHONPATH}:/home/shane/mywebsite.com\:3344/ export PYTH...

How can one mock/stub python module like urllib

I need to test a function that needs to query a page on an external server using urllib.urlopen (it also uses urllib.urlencode). The server could be down, the page could change; I can't rely on it for a test. What is the best way to control what urllib.urlopen returns? ...

How do I use subprocess.Popen to connect multiple processes by pipes?

How do I execute the following shell command using the Python subprocess module? echo "input data" | awk -f script.awk | sort > outfile.txt The input data will come from a string, so I don't actually need echo. I've got this far, can anyone explain how I get it to pipe through sort too? p_awk = subprocess.Popen(["awk","-f","script.a...

What is a partial class?

What is and how can it be used in C#. Can you use the same concept in Python/Perl? ...

Does python optimize modules when they are imported multiple times?

If a large module is loaded by some submodule of your code, is there any benefit to referencing the module from that namespace instead of importing it again? For example: I have a module MyLib, which makes extensive use of ReallyBigLib. If I have code that imports MyLib, should I dig the module out like so import MyLib ReallyBigLib = M...

How do I copy files with specific file extension to a folder in my python (version 2.5) script?

I'd like to copy the files that have a specific file extension to a new folder. I have an idea how to use os.walk but specifically how would I go about using that? I'm searching for the files with a specific file extension in only one folder (this folder has 2 subdirectories but the files I'm looking for will never be found in these 2 su...

Which is more efficient in Python: standard imports or contextual imports?

I apologize in advance if this question seems remedial. Which would be considered more efficient in Python: Standard import import logging try: ...some code... exception Exception, e: logging.error(e) ...or... Contextual import try: ...some code... exception Exception, e: import logging logging.error(e) ...

Automatic code quality review tool for Python

Hi, I am python newbie. Can ppl point towards good OSS for automated decent python code review tools. I am churning quite some python code these days. Want to pass it thru some quality tool. Thanks Related question: Are there any static analysis tools for Python? ...

How do I remove a specific number of files using python (version 2.5)?

I would like to remove two files from a folder at the conclusion of my script. Do I need to create a function responsible for removing these two specific files? I would like to know in some detail how to use os.remove (if that is what I should use) properly. These two files will always be discarded at the conclusion of my script (which p...

How do I zip the contents of a folder using python (version 2.5)?

Once I have all the files I require in a particular folder, I would like my python script to zip the folder contents. Is this possible? And how could I go about doing it? A point in the right direction (i.e. a link with an example) or an example that I can see would be extremely helpful. Thanks in advance. ...

How do you appropriately value work from developers that provide stuff to the community?

I have been working on an application that relies heavily on work that people have contributed to the open source community. Specifically I created a neat application that I think will help me retain customers and continue to get new customers and might even allow me a little pricing power. The application was developed in Python and r...

Performance comparison of Thrift, Protocol Buffers, JSON, EJB, other?

We're looking into transport/protocol solutions and were about to do various performance tests, so I thought I'd check with the community if they've already done this: Has anyone done server performance tests for simple echo services as well as serialization/deserialization for various messages sizes comparing EJB3, Thrift, and Protocol...

how do i use python libraries in C++?

I want to use the nltk libraries in c++. Is there a glue language/mechanism I can use to do this? Reason: I havent done any serious programming in c++ for a while and want to revise NLP concepts at the same time. Thanks ...

Why doesn't xpath work when processing an XHTML document with lxml (in python)?

I am testing against the following test document: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; <html xmlns="http://www.w3.org/1999/xhtml"&gt; <head> <title>hi there</title> </head> <body> ...

Integrating command-line generated python .coverage files with PyDev

My build environment is configured to compile, run and create coverage file at the command line (using Ned Batchelder coverage.py tool). I'm using Eclipse with PyDev as my editor, but for practical reasons, it's not possible/convenient for me to convert my whole build environment to Eclipse (and thus generate the coverage data directly...

Create a zip file from a generator in Python?

I've got a large amount of data (a couple gigs) I need to write to a zip file in Python. I can't load it all into memory at once to pass to the .writestr method of ZipFile, and I really don't want to feed it all out to disk using temporary files and then read it back. Is there a way to feed a generator or a file-like object to the ZipF...

Dynamically update ModelForm's Meta class

I am hoping to dynamically update a ModelForm's inline Meta class from my view. Although this code seems to update the exclude list in the Meta class, the output from as_p(), as_ul(), etc does not reflect the updated Meta exclude. I assume then that the html is generated when the ModelForm is created not when the as_*() is called. Is th...