Skip to content

Stop parsing on first unknown argument

Using argparse, is it possible to stop parsing arguments at the first unknown argument? I’ve found 2 almost solutions; parse_known_args, but this allows for known parameters to be detected after the first unknown argument. nargs=argparse.REMAINDER, but this won’t stop parsing until the first non-o…

Paginator for inline models in django admin

I have this simple django model consisting of an sensor and values for the specific sensor. The number of values per Pyranometer is high (>30k). Is it somehow possible to paginate PyranometerValues by a specific day or generell apply a paginator to the admin inline view? Answer Have you checked raw_id_fiel…

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…