Posts with the tag Python

Setting up Python venvs on Arch Linux ARM

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...

Read on

IP Address Analysis Using Python

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: ')
try:
    i =...

Read on

Installing `pip` on Windows

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...

Read on

Elegant Way to Display Bytes as Hex Values in Python

Elegant code to print a hex string in python:

toHex = lambda x: "".join("{:02X}".format(ord(c)) for c in x)
print(toHex(b'\x03\xac23\n<span...

Read on

Handle 16bit TIFF Images in Python

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...

Read on

Use the Python Module MacFSEvents to Monitor Directories for Changes on Mac OS X

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

<span...

Read on

Light Wavelength from Energy

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."""
 ...

Read on

Physical Constants in Python

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:", <span...

Read on

Install matplotlib on Mac OS X

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...

Read on

Easy Usage of XML with Python

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...

Read on

Use the Python Module Watchdog to Monitor Directories for Changes

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...

Read on

Watching Directories for Changes using Python : An Overview

Operating System Independent Solutions

Read on

`python-sipgate-xmlrpc` – Easy to use Python bindings for the Sipgate XML-RPC API

The German VoIP provider Sipgate has a great offer for people who want to replace their landline with a modern internet based SIP telephony account. Sipgate offers an XML-RPC API to all its customers (you must at least have created a free 'basic' account). With the API, you...

Read on

Install as many python modules as you like - in virtual environments

su
easy_install virtualenv

And create your virtualenv :

mkdir ~/venv
virtualenv --no-site-packages ~/venv/django-cooking
source ~/venv/django-cooking/bin/activate

Now you can install a whole bunch of stuff to that virtual python environment:

easy_install django psycopg2 django-extensions werkzeug south reportlab pypdf html5lib pisa pil

When using virtualenv, you should also consider using virtualenvwrapper! It provides...

Read on

Python 2.7 on Mac OS X

Praveen Gollakota shed light on Installing Python 2.7, easy_install, pip, virtualenv and virtualenvwrapper on Mac OS X. He gives you all the information you need if you want to use an up-to-date Python on Mac OS X! Thx!

Alternative way of installing python using homebrew:

brew install python
...

Read on

Communicate with the identi.ca API using Python via tweepy

Install tweepy via PyPI:

easy_install tweepy

Example usage with identi.ca:

import tweepy
username, password = "identi.ca_username", "identi.ca_password"
auth = tweepy.BasicAuthHandler(username, password)
api = tweepy.API(auth, <span...

Read on