I’ve been using virtualenv lately while developing in python. I like the idea of a segregated development environment using the –no-site-packages option, but doing this while developing a PyGTK app can be a bit tricky. The PyGTK modules are installed on Ubuntu by default, and I would like to make a virtualenv (with –no-site-packages) aware of specific modules that are
When is not a good time to use python generators?
This is rather the inverse of What can you use Python generator functions for?: python generators, generator expressions, and the itertools module are some of my favorite features of python these days. They’re especially useful when setting up chains of operations to perform on a big pile of data–I often use them when processing DSV files. So when is it
Starting python debugger automatically on error
This is a question I have wondered about for quite some time, yet I have never found a suitable solution. If I run a script and I come across, let’s say an IndexError, python prints the line, location and quick description of the error and exits. Is it possible to automatically start pdb when an error is encountered? I am
os.walk without digging into directories below
How do I limit os.walk to only return files in the directory I provide it? Answer Use the walklevel function. It works just like os.walk, but you can pass it a level parameter that indicates how deep the recursion will go.
How to get method parameter names?
Given that a function a_method has been defined like Starting from a_method itself, how can I get the argument names – for example, as a tuple of strings, like (“arg1”, “arg2”)? Answer Take a look at the inspect module – this will do the inspection of the various code object properties for you. The other results are the name of
Binary search (bisection) in Python
Is there a library function that performs binary search on a list/tuple and return the position of the item if found and ‘False’ (-1, None, etc.) if not? I found the functions bisect_left/right in the bisect module, but they still return a position even if the item is not in the list. That’s perfectly fine for their intended usage, but
How can I make a dictionary (dict) from separate lists of keys and values?
I want to combine these: Into a single dictionary: Answer Like this: Voila :-) The pairwise dict constructor and zip function are awesomely useful.
List of IP addresses/hostnames from local network in Python
How can I get a list of the IP addresses or host names from a local network easily in Python? It would be best if it was multi-platform, but it needs to work on Mac OS X first, then others follow. Edit: By local I mean all active addresses within a local network, such as 192.168.xxx.xxx. So, if the IP
What is the difference between re.search and re.match?
What is the difference between the search() and match() functions in the Python re module? I’ve read the Python 2 documentation (Python 3 documentation), but I never seem to remember it. I keep having to look it up and re-learn it. I’m hoping that someone will answer it clearly with examples so that (perhaps) it will stick in my head.
Python: can I have a list with named indices?
In PHP I can name my array indices so that I may have something like: Is this possible in Python? Answer This sounds like the PHP array using named indices is very similar to a python dict: See http://docs.python.org/tutorial/datastructures.html#dictionaries for more on this.