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
Tag: python
Convert a string to preexisting variable names
How do I convert a string to the variable name in Python? For example, if the program contains a object named self.post that contains a variable named, I want to do something like: Answer As referenced in Stack Overflow question Inplace substitution from ConfigParser, you’re looking for eval():
Size of an open file object
Is there a way to find the size of a file object that is currently open? Specifically, I am working with the tarfile module to create tarfiles, but I don’t want my tarfile to exceed a certain size. As far as I know, tarfile objects are file-like objects, so I imagine a generic solution would work. Answe…
How do I determine all of my IP addresses when I have multiple NICs?
I have multiple Network Interface Cards on my computer, each with its own IP address. When I use gethostbyname(gethostname()) from Python’s (built-in) socket module, it will only return one of them. How do I get the others? Answer Use the netifaces module. Because networking is complex, using netifaces …
How do I find the location of Python module sources?
How do I learn where the source file for a given Python module is installed? Is the method different on Windows than on Linux? I’m trying to look for the source of the datetime module in particular, but I’m interested in a more general answer as well. Answer For a pure python module you can find t…
Python re.findall with groupdicts
I kind of wish that there were a version of re.findall that returned groupdicts instead of just groups. Am I missing some simple way to accomplish the same result? Does anybody know of a reason that this function doesn’t exist? Answer You could use the finditer() function. This will give you a sequence …
Finding invocations of a certain function in a c++ file using python
I need to find all occurrences of a function call in a C++ file using python, and extract the arguments for each call. I’m playing with the pygccxml package, and extracting the arguments given a string with the function call is extremely easy: What I couldn’t find is a way of getting the calls par…
Virtualenv on Ubuntu with no site-packages
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 …
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–…
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 i…