PDFColorParser – a python script for detecting PDF pages containing color

The following script was written to find out which pages of a PDF file contain color. I used it for my thesis. Save it to a file and call it with script.py INPUTPDF.pdf


Python, Subprocess and Multiple Arguments

Quelle: http://blog.giladnaor.com/2009/09/python-subprocess-and-multiple.html

Calling external programs from within a python script is a pretty common task. However, most of the times when I want to pass some arguments to the external program, I usually get stuck. The official Python documentation for the subprocess module is a bit lacking, in my humble opinion, in its treatment of argument passing.

This is not to say that all the information isn’t available in the official documentation. In fact, it is. It’s just not where I always look for it. The first thing I look for are examples. I simply do not have time to read the entire documentation page for something which should not be too difficult.

The basic idea is this:

  1. Either provide a string of the entire command and argument, but use Shell=True
  2. Provide a string for only the command
  3. Provide a list of strings, where the first item is the command and the other items are the arguments

It’s the third option that keeps on giving me trouble, so here is a simple working example for reference:

Passing Arguments with Quotes
If you want to pass arguments that are passed with quotes in the shell, then just pass them as a single list item, without the quotes.

Choosing The SSL Version In Python Requests

Quelle: http://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/

Over the last few months (and probably for quite a while before then too), a few issues have been raised on the Requests GitHub page asking how to select the version of SSL used by Requests. This is actually simple once you know how, so I thought I’d write a short post to show you how it’s done.

A quick note before we begin: it is not possible to select the version of SSL you want to use before Requests v1.0.0 without changing the underlying library code. The following set of instructions will not work if you’re running an earlier version.

How It’s Done

Altogether this is relatively simple. To change the SSL version used in HTTPS, you are expected to subclass the HTTPAdapter class and mount it to a Session object. If, for example, you wanted to force the use of TLSv1, your new Transport Adapter will look like this:

With that done, you can mount it to a Requests Session object:

Of course, this is so easy that it’s simple to write a Transport Adapter that can take an arbitrary SSL type from the ssl package in the constructor and use that. Whack this in a file and import it into whatever you’re doing:

You can mount it to a Session object and just go to town.

Hopefully this will be of use to people. If you have any problems or improvements, leave a note in the comments or drop me a line on Twitter (the url is in the sidebar).