User Tools

Site Tools


cross_platform:encoding:dpl

  

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

Installation

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 setuptools-7.0.zip, unpack it with something like 7zip, then open the README.txt to discover that in fact you just have to run ez_setup.py, which is in there, so:

 python ez_setup.py 

- which first downloads another copy of that setuptools-7.0.zip, 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 get-pip.py directly, and:

 python get-pip.py 

Perl

http://en.wikipedia.org/wiki/Perl

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 http://strawberryperl.com (http://en.wikipedia.org/wiki/Strawberry_Perl). 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

Modules

CPAN

  • Acme::EyeDrops - crazy!
  • /home/$USER/.cpan/CPAN/MyConfig.pm
  • 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

perldebug

 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);
closedir(DIR); 

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

Variables

Use Python

Guides:

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

IDLE

http://en.wikipedia.org/wiki/IDLE_(Python)

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

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

idle -r yourscript.py

The Python Tutorial

Regular Expressions

working with files

Built-in Types

http://en.wikibooks.org/wiki/Python_Programming/Data_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).

Immutability

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))
 32195408 

- 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

http://docs.python.org/2/library/stdtypes.html#typesseq-mutable

Lists:

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

Strings

String Methods:

Flake8

https://pypi.python.org/pypi/flake8

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.

Unicode

An advanced script to repair a unicode mess, from http://blog.luminoso.com/2012/08/20/fix-unicode-mistakes-with-python/ 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:

 print("\u20ac") 

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

 print(ascii(trickytextstring)) 

which at least won't throw an error…

Modules

Datetime:

Interprocess Communication and Networking:

Numeric and Mathematical Modules:

Optional Operating System Services:

sys:

Importing Modules

Module datetime has method datetime.now() 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:

 print datetime.datetime.now() 

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
 print datetime.now() 

- 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

http://docs.python.org/3.4/library/time.html

  >>> import time 

gmtime http://docs.python.org/2/library/time.html#time.gmtime

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