User Tools

Site Tools



Dynamic programming language

Get Python

Python 3

getting at it

Linux Mint 17 Xfce (64-bit):

  • dpkg-query -l python* - reveals what's installed
  • python enters interpreter for Python 2.7.6
  • python3 enters interpreter for Python 3.4.0


Advanced > Compile .py files to byte code after installation - not worth the effort according to Matthew Trevor's answer.

Python 3.4.x: python-3.4.x.msi puts itself in C:\Python34\, and can add that directory to command path (which needs a system restart to come into effect)

Package Management

get pip

Anthony DeBarros - kindly points us to Setuptools, where, if you know what you're doing, you download, unpack it with something like 7zip, then open the README.txt to discover that in fact you just have to run, which is in there, so:


- which first downloads another copy of that, but somehow has the capacity to run it, adding (among other things) the folder C:\Python34\Scripts, in which there is easy_install-3.4.exe, which you can use at an MSWin command prompt thusly:

 C:\Python33\Scripts>easy_install-3.4 pip 

- which drops a load of pip scripts into that same folder.

Or, following the pip installation guide, once you've got Setuptools in, you can directly, and:



The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal).

Strawberry Perl

  • An open source distribution for Windows ( It provides easy access to CPAN modules (including the XS modules that need a C compiler).
  • Offers to install itself to C:\strawberry and indeed the README.txt states that it must be a path without space. Comes with C:\strawberry\c\bin\dmake.exe.

PerlMagick API Installation on Windows 7

cpanm Image::Magick fails for me (“Perl interpreter has stopped working” after “Building and testing…” has begun), but I found awesomess3's trick, all of which is not now needed, just this:

First I grab a more recent version of ImageMagick from a Download Mirror and unzip it, which gets me an ImageMagick-6.8.9-10 directory. I open a Terminal in the sub-directory PerlMagick, and issue perl Makefile.PL, which first reports:

Gonna create 'libMagickCore.a' from 'C:\Program Files\ImageMagick-6.8.9-Q16\CORE_RL_magick_.dll'

- so, obviously, I needed to've first installed ImageMagick to my system. I get a warning that “No library found for -lMagickCore-6.Q16”, but no need to go further, as my jpgMagick now works.

The Perl Foundation



  • Acme::EyeDrops - crazy!
  • /home/$USER/.cpan/CPAN/
  • Strawberry Perl: cpanm Image::ExifTool - took ages to 'build and test' on my N130, and installed C:\Strawberry\perl\site\bin\exiftool.bat, which then works everywhere.
the Comprehensive Perl Archive Network, is an archive of over 124,000 modules of software written in the Perl programming language, as well as documentation for them.

which modules are installed on my system?

Use Perl


 perl -de 42 

- and q quits.

 DB<1> print $ENV{'PATH'}; 

prints out my Win7 environment variable:

%SystemRoot%\system32\WindowsPowerShell\v1.0\; C:\Perl\site\bin;C:\Perl\bin;C:\windows\system32; C:\windows;C:\windows\System32\Wbem; C:\windows\System32\WindowsPowerShell\v1.0\

read in filenames filtered by pattern

The answers to Ether's question helped me to get this (though I still don't exactly understand it):

 # Now work through the directory collecting jpeg file names:
opendir(DIR, '.');
my @jpegs = grep { /\.jpg$/i && -f "./$_" } readdir(DIR);

These lines fill my array @jpegs with all the filenames of jpegs in the current directory.


Use Python


the Python Interpreter

Typing an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes the interpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command: quit().


C:\pythonXX\Lib\idlelib\idle.pyw -r

Or in MSWin, first append C:\Python33\Lib\idlelib; to $PATH, then, from anywhere:

idle -r

The Python Tutorial

Regular Expressions

working with files

Built-in Types

Data types determine whether an object can do something, or whether it just would not make sense. Other programming languages often determine whether an operation makes sense for an object by making sure the object can never be stored somewhere where the operation will be performed on the object (this type system is called static typing). Python does not do that. Instead it stores the type of an object with the object, and checks when the operation is performed whether that operation makes sense for that object (this is called dynamic typing).


When we assign a data type in Python, its value is saved as an object in memory which has an identity:

 >>> a=5; print(id(a))

- the built-in function id(object) returns a long integer that is unique for the lifetime of the object. If we assign another object with exactly the same value, it gets the same identity. This optimises memory because values are only stored once.

  • Immutable objects can't be changed, only reassigned, thus acquiring a new identity - the integer returned by id(object) changes.
  • Mutable objects can be changed in place with methods, and the identity is retained, such that anything assigned to them acquires that new value:
 >>> a=b=[1,2]; print(a)
 [1, 2]
 >>> a.append(3), print(b)
 [1, 2, 3] 

- mutating a changed b!

George Herbert Lewis has a longer discussion, with helpful examples, and there's a good Stack Overflow discussion on this.

Mutable Sequence Types


  • List length can change dynamically.
  • Arrays are easily made up from lists in lists (but there's also an Array method).


String Methods:


sudo apt-get install python-flake8
sudo apt-get install python3-flake8

on MSWin

Needs to be installed from an MSWin Command Prompt:

 C:\Python34\Scripts>pip3.4 install flake8 

- which populates C:\Python34\Lib\site-packages. Uses pyflakes for logical error checking, PEP 8 for style checking, and a function complexity warning script:

Successfully installed flake8 pyflakes pep8 mccabe

Then ensure it's in the Path (eg for gVim's Syntastic to access):

(Win-Brk for) System > Advanced system settings (= System Properties > Advanced) > Environment Variables > System Variables > Path > Edit

& append the Path text with ;C:\Python33\Scripts\, and then restart Windows 7 to invoke it.


An advanced script to repair a unicode mess, from luminosoinsight, a text understanding company in Cambridge, MA.

Python 2 strings are all in ASCII, so there are tricks:

text = u"I'm a Unicode string"
print text

Python 3

J.A. Roberts Tunney provides a humorous explanation.

Even though Python 3 is in UTF-8 already, and MSWin runs in UTF-16, this will throw a Python UnicodeEncodeError in an MSWin Console:


- MSWin's Console encodes in its own sweet way. If you're in a hurry, and you just want an output, you could wrap the string:


which at least won't throw an error…



Interprocess Communication and Networking:

Numeric and Mathematical Modules:

Optional Operating System Services:


Importing Modules

Module datetime has method which gets you an output like 2012-12-30 20:58:58.619284, but to use it, first you need to get that method into your script.

You can import the whole datetime module:

 import datetime 

and now you can call its datetime method to output a date & time string:


Or, if you're sure that you will only need method datetime from module datetime, you can import the method alone:

 from datetime import datetime

- which may be easier to read, but is not faster code, and not always a good idea, particularly if the method has an ambiguous name.

time — Time access and conversions

  >>> import time 


 >>> time.gmtime(5) 

> time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=5, tm_wday=3, tm_yday=1, tm_isdst=0) - which is a bulky way of saying 5 seconds past the beginning of the epoch (1970)

It rounds down any fractional seconds, eg 5.99 becomes 5 seconds

To convert a struct.time t to a human-readable time:

 >>> time.asctime(t) 

'Thu Jan 1 00:00:05 1970'

Generic Operating System Services

Unicode replacement character

I had some files which an Android couldn't handle, so it replaced some diacritics with ''%%\ufffd%%''. When I instructed Python 3.3 to write these filepaths to a file, it failed on UnicodeEncodeError. Ferry Boender discusses this, but I still can't figure out what to do about it, other than catch those filepaths, and ASCII them.

os — Miscellaneous operating system interfaces

cross_platform/encoding/dpl.txt · Last modified: 2016/09/01 17:55 (external edit)