Dec 022012
 

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

#!/usr/bin/env python

from IPy import IP, IPSet

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

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

#if i._prefixlen != 32: # a network
if len(i) > 1: # a network
    print('net: %s' % i.net())
    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' % len(i))
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
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Nov 122012
 

Elegant code to print a hex string in python:

toHex = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])

# you can use it like this:
print [toHex(byte) for byte in '\x03\xac23\n']
# it should print
# ['03', 'ac', '32', '33', '0a']
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

Quickstart

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, event.name)

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

### 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:
observer.unschedule(stream)
observer.stop()
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 http://www.scipy.org/Installing_SciPy (or via brew install gfortran on Max OS X)
  2. Install SciPy itself: pip install scipy

Usage:

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)

Installation:

Download the Python module http://www.hjcb.nl/python/physcon.txt and save it as physcon.py.

Usage:

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

Usage

Aug 222011
 

The builtin Python modules to work with markup languages can be found on http://docs.python.org/library/markup.html. For XML these are mainly DOM (incl. minidom), SAX and ElementTree.

A comparison of minidom and ElementTree including good examples can be found on http://mike.hostetlerhome.com/present_files/pyxml.html.

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.

MiniDom

ElementTree

Aug 212011
 

Watchdog is a cross platform solution to monitor directories for changes on Python and has been initiated by Yesudeep Mangalapilly. Very helpful is the readme file of the watchdog module.

Requirements and Installation

You may want to have a look at its source code on GitHub.

The dependencies of watchdog can be found here.

Installing the requirements:

As watchdog depends on PyYAML, you should also install LibYAML which will give PyYAML a performance boost:

On Mac OS X you want to install Homebrew and then install LibYAML: