Aug 262014

Since Python 3.3 venv is a module found in the standard library of Python. It can replace virtualenv / virtualenvwrapper. Creating a virtual environment is as simple as this:

python3 -m venv ~/.pyvenv/iplogger-3.4
source ~/.pyvenv/iplogger-3.4/bin/activate
# I didn't need the step below, don't know when it is required:
#curl | python

### If you want to include site packages of your system:
python3 -m venv --system-site-packages ~/.pyvenv/system-3.4
source ~/.pyvenv/system-3.4/bin/activate
# I didn't need the step below, don't know when it is required:
#curl | python


Dec 022012

Find out more about IP addresses (IPv4 and IPv6) using the Python module IPy:

#!/usr/bin/env python

from IPy import IP

ip_s = raw_input('Please enter an IP address or range: ')
    i = IP(ip_s)
except ValueError:
    print('Could not understand your input %s. Exiting.' % ip_s)
    from sys import exit

print('I understood: %s' % i)
print('This is an IPv%d address.' % i.version())

#if i._prefixlen != 32: # a network
    net = len(i) > 1 # a network
    size = len(i)
    net = True
    size = "large"
if net:
    print('net: %s' %
    print('netmask: %s' % i.netmask())
    print('broadcast: %s' % i.broadcast())
    print('reverse notation of net address: %s' % i.reverseNames()[0])
    print('size of subnet: %s' % size)
else: # a single IP
    print('reverse notation: %s' % i.reverseNames()[0])

if i.version() == 6:
    print('normal notation: %s' % i.strNormal())
    print('full size notation: %s' % i.strFullsize())
print('hexadecimal notation: %s' % i.strHex())
print('string notation of binary value: %s' % i.strBin())
print('type of ip: %s' % i.iptype())
if i.get_mac(): print('found possible mac address: %s' % i.get_mac())
Nov 132012

I found this solution on StackOverflow which worked for me on Windows 7:

  • Install Python (currently I’d use Python 2.7).
  • Add C:\Python27\Scripts to your %PATH% environment variable in order to be able to use Python and the the programs without naming the full path.
    Read more about this here
  • Install easy_install: Get it here.
  • Use easy_install to install pip:
    easy_install pip

Newer versions of Python

If you use Python 3.4+ you don’t need to instal pip as Python ships with pip.exe nowadays. Just add its folder to the path and run it with your command:

Nov 122012

Elegant code to print a hex string in python:

toHex = lambda x: "".join("{:02X}".format(ord(c)) for c in x)

or for Python3:

toHex = lambda x: "".join("{:02X}".format(c) for c in x)
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Aug 252011

There seem to be quite a lot of problems with 16-bit grey scale TIFF images (especially with the Python Imaging Library – PIL). If you can, you may want to use FITS instead of TIFF. There are good and up-to-date libraries for Python: PyFITS.

For me, however, the FreeImage library works great to read 16bit TIFF images.

About the TIFF Format

Every TIFF begins with a 2-byte indicator of byte order: 0x490x49 (“II”) for little-endian Intel style and 0x4d0x4d (“MM”) for big-endian Motorola style byte order. More common is the Intel style.

Aug 232011

MacFSEvents is an up-to-date solution to monitor directories for changes on Mac OS X when using the scripting language Python. It is a binding to FSEvents, Mac OS X’s filesystem monitoring framework.

Install MacFSEvents

pip install macfsevents


from fsevents import Observer, Stream

def file_event_callback(event):
    """This is the function being called when an event on a file is detected."""
    print "Mask: %s, Cookie: %s, Name: %s" % (event.mask, event.cookie,

observer = Observer()
stream = Stream(file_event_callback, '/Users/', file_events=True)

### Watch how your callback function is being called when files in the /Users dir are changed

# Before your Python program exits, you must stop the observer:
Aug 222011

You can use the following Python function to convert a photon energy (in electron volts) to the corresponding wavelength (in nano meters):

def wavelength_from_energy ( electron_volts):
    """Returns a photon wavelength in nm from a photon energy given in eV."""
    hc = 1239.841842144513
    return hc / E

Where the constant h*c is 1239.84 eV nm:

import physcon as pc
print "h*c in [eV  nm]: %.2f" % (pc.h * pc.c / pc.e * 10**9)
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Aug 222011

SciPy (updated to CODATA 2010)

Installation (easiest using pip):

  1. Install Fortran as described for your OS on (or via brew install gfortran on Max OS X)
  2. Install SciPy itself: pip install scipy


from scipy.constants import *
print "The Planck constant h:", h
print "The Avogadro constant N_A:", N_A
print "The muon mass in u:", physical_constants['muon mass in u']

Physcon (updated to CODATA 2006)


Download the Python module and save it as


Aug 222011

Installation of the pre-built binaries for OS X

Read Installing OSX binaries in the matplotlib Installation FAQ.

Installation via pip

This is a good way if you have an up-to-date Python installation on your computer and pip installed. It takes, however, some time (~ 20 mins for me) to install as it builds the stuff from source:

pip install numpy
pip install matplotlib


VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Aug 222011

The builtin Python modules to work with markup languages can be found on For XML these are mainly DOM (incl. minidom), SAX and ElementTree.

A comparison of minidom and ElementTree including good examples can be found on

Other than the default Python modules there is also a very Pythonic module called lxml which behaves similar as ElementTree and is based on Gnome’s libxml2.