Skip to content

Tag: python

Use the python interpreter packaged with py2exe

I have a python script I want to distribute to Windows, where people might not have python installed. So I use py2exe. The problem is in the script I run other python scripts by using subprocess, which requires python interpreter as the program to execute. As I don’t have python interpreter installed on…

How to convert an iterable to a stream?

If I’ve got an iterable containing strings, is there a simple way to turn it into a stream? I want to do something like this: Answer Here’s my streaming iterator an experimental branch of urllib3 supporting streaming chunked request via iterables: Source with context: https://github.com/shazow/url…

What is the Python way to walk a directory tree?

I feel that assigning files, and folders and doing the += [item] part is a bit hackish. Any suggestions? I’m using Python 3.2 Answer Take a look at the os.walk function which returns the path along with the directories and files it contains. That should considerably shorten your solution.

Convert SVG to PNG in Python

How do I convert an svg to png, in Python? I am storing the svg in an instance of StringIO. Should I use the pyCairo library? How do I write that code? Answer The answer is “pyrsvg” – a Python binding for librsvg. There is an Ubuntu python-rsvg package providing it. Searching Google for its …

Installing multiple versions of a package with pip

In my application I would like to use: packageA, which requires packageX==1.3 packageB, which requires packageX==1.4 packageX==1.5 How can I install multiple versions of packageX with pip to handle this situation? Answer pip won’t help you with this. You can tell it to install a specific version, but it…

How can I make a deepcopy of a function in Python?

I would like to make a deepcopy of a function in Python. The copy module is not helpful, according to the documentation, which says: This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow a…

Regular expression to find any number in a string

What’s the notation for any number in re? Like if I’m searching a string for any number, positive or negative. I’ve been using d+ but that can’t find 0 or -1 Answer Searching for positive, negative, and/or decimals, you could use [+-]?d+(?:.d+)? This isn’t very smart about leadin…