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.
Python Django Templates and testing if a variable is null or empty string
I am pretty new to django, but have many years experience coding in the java world, so I feel ridiculous asking this question – I am sure the answer is obvious and I am just missing it. I can’t seem to find the right way to query this in google or something… I have searched through the djang…
How can I convert a string with dot and comma into a float in Python
How can I convert a string like 123,456.908 to float 123456.908 in Python? For ints, see How to convert a string to a number if it has commas in it as thousands separators?, although the techniques are essentially the same. Answer Just remove the , with replace():
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 …
How to stitch several PDF pages into one big canvas-like PDF?
I have a 32-page PDF of my family tree. Instead of having the family tree all on one really big PDF page (which is what I want), it is formatted so a group of 8 individual US letter-sized pages are supposed to be stitched across the width; 4 rows of this completes the tree. The margins of each page are
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…