To install pip on Win­dows, you need to first install its pre­de­ces­sor – easy install. How­ever, if you’re using a 64-bit dis­tri­b­u­tion of  Python then the .exe installers here will give you this error when you run it:

image

This is due to a dis­tu­tils installer com­pat­i­bil­ity issue, but all’s not lost, you just need to down­load the ez_setup.py and run it, it will then down­load the appro­pri­ate .egg file and install it for you.

You then need to add the Scripts folder in your Python dis­tri­b­u­tion, e.g. C:\Python27\Scripts to the Win­dows path, and open up a DOS prompt and run easy_install pip and voila!

Share
  • Use 4-space inden­ta­tion, no tabs

 

  • Wrap lines so that they don’t exceed 79 characters

 

  • Use blank lines to sep­a­rate func­tions and classes, and larger blocks of code inside functions

 

  • When pos­si­ble, put com­ments on a line of their own

 

  • Use doc­strings

 

  • Use spaces around oper­a­tors and after commas

 

  • Name your classes and func­tions con­sis­tently; the con­ven­tion is to use Camel­Case for classes and lower_case_with_underscores for func­tions and methods

 

  • Always use self as the name for the first method argument

 

  • Don’t use fancy encod­ings if your code is meant to be used in inter­na­tional environments

    Nam­ing conventions:

    Vari­ables / func­tions — use_lower_case_separated_by_underscore

    Class names — UseCamelCase

    Error classes end in Error, i.e. MyError

Share

Fore­words

A while back I decided to try and learn Python for the hell of it as it seems like an inter­est­ing lan­guage and has some of the most con­cise and user-friendly syn­tax. Hav­ing spent some time going through a num­ber of dif­fer­ent learn­ing sources and mate­ri­als (like the offi­cial site python.org which has a very help­ful tuto­r­ial sec­tion) I have put together a set of notes I made as I was learn­ing and hope­fully they can be use­ful to you as a quick list of how-to code snippets.

All the code snap­shots I’m show­ing here are taken from the IDLE Python shell.

Input and Output

The repr() func­tion gen­er­ates rep­re­sen­ta­tions that can be read by the inter­preter, str() func­tion gen­er­ates a human-readable of the value:

clip_image001

 

If an object doesn’t have a par­tic­u­lar rep­re­sen­ta­tion for human con­sump­tion, str() will return the same value as repr(), e.g. lists and dictionaries:

clip_image002

 

You can use the string.rjust(), string.ljust() or string.center() func­tion to left-justify, right-justify or cen­ter a string of given width by padding it with spaces:

clip_image003

 

You can use the string.zfill() func­tion to pad a numeric string on the left with zeros:

clip_image004

 

You can use string.format() method to for­mat the string out­put of your code:

clip_image005

 

You can also use named argu­ments:

clip_image006

 

An optional ‘:’ and for­mat spec­i­fier can fol­low the field name to allow greater con­trol over how the value is formatted:

clip_image007

 

Pass­ing an inte­ger after the ‘:’ will cause that field to be a min­i­mum num­ber of char­ac­ters wide:

clip_image008

 

For a very long string, you could even ref­er­ence vari­ables to be for­mat­ted by name using a dictionary:

clip_image009

Or you could unpack the dic­tio­nary first:

clip_image010

 

You can use the open() func­tion to get a file object, to open a file for writing:

clip_image011

The first para­me­ter being the file name, the sec­ond the mode which can be one of the following:

clip_image012

 

To open a file and read its contents:

clip_image013

 

To read one line at a time

clip_image014

 

To read the lines in the file as an array of strings:

clip_image015

 

You can also read x num­ber of bytes at a time:

clip_image016

 

But remem­ber, you always read start­ing from the posi­tion where your last read finished:

clip_image017

 

To write to a file:

clip_image018

 

You can only write string con­tents to a file, which means you’d need to con­vert other objects to string first:

clip_image019

 

To flush the file buffer and actu­ally write them to the file, use the close() func­tion, after which you’ll need to open the file again:

clip_image020

 

You can use the tell() func­tion to find out your cur­rent posi­tion in the file:

clip_image021

 

You can use the seek() func­tion to change the file object’s cur­rent posi­tion, seek(offset, from_what):

clip_image022

from_what can be one of three values:

  • 0 — begin­ning of file (default)
  • 1 — cur­rent position
  • 2 — end of the file

clip_image023

 

It’s good prac­tice to use the with key­word when deal­ing with file objects, which ensures that the file is closed after its suite fin­ishes. (this works like the using key­word in C#).

clip_image024

 

To deal with com­plex types (or indeed any non-string types like int) you can use the pickle mod­ule to con­vert it to and from string.

The process of con­vert­ing a com­plex type to string is called pick­ling.

The process of con­struct­ing a com­plex type from string is called unpick­ling.

clip_image025

 

Excep­tions

try-except block:

clip_image001[10]

 

You can han­dle more than one excep­tion if you name them in a tuple:

clip_image002[4]

 

You can han­dle a list of excep­tions in a cer­tain pri­or­ity list and if you don’t spec­ify a par­tic­u­lar excep­tion type it’ll serve as a wildcard:

clip_image003[8]

 

You can also spec­ify a else clause which fol­lows all except clauses, it’s use­ful for code that must be exe­cuted if the try clause does not raise an exception:

clip_image004[4]

The use of the else clause is bet­ter than adding addi­tional code to the try clause because it avoids acci­den­tally catch­ing an excep­tion that wasn’t raised by the code being pro­tected by the try-except statement.

 

There’s also the optional finally clause which is intended to define clean-up actions that must be exe­cuted under all circumstances:

clip_image005[4]

 

You can get more infor­ma­tion about the instance of the exceptions:

clip_image006[4]

clip_image007[4]

 

To throw an exception:

clip_image008[9]

 

To re-throw an exception:

clip_image009[4]

 

To define a cus­tom excep­tion:

clip_image010[4]

The __str__() func­tion ensures that you can print the excep­tion instance directly:

clip_image011[4]

 

When cre­at­ing a mod­ule that can raise sev­eral dis­tinct errors, a com­mon prac­tice is to cre­ate a base class for excep­tions defined by that mod­ule, and sub­class that to cre­ate spe­cific excep­tion classes for dif­fer­ent error conditions:

clip_image012[4]

 

Some objects define stan­dard clean-up actions, for instance, the with state­ment always closes the file after the state­ment is executed:

clip_image013[4]

(the same as the using state­ment in C#)

Share

Fore­words

A while back I decided to try and learn Python for the hell of it as it seems like an inter­est­ing lan­guage and has some of the most con­cise and user-friendly syn­tax. Hav­ing spent some time going through a num­ber of dif­fer­ent learn­ing sources and mate­ri­als (like the offi­cial site python.org which has a very help­ful tuto­r­ial sec­tion) I have put together a set of notes I made as I was learn­ing and hope­fully they can be use­ful to you as a quick list of how-to code snippets.

All the code snap­shots I’m show­ing here are taken from the IDLE Python shell.

Classes

Some notable dif­fer­ences from the class mech­a­nism in C#:

  • The class inher­i­tance mech­a­nism allows mul­ti­ple base classes
  • Classes are cre­ated at run­time and can be mod­i­fied fur­ther after creation
  • All class mem­bers are pub­lic by default
  • All class mem­bers are virtual
  • Classes them­selves are objects

Like many other lan­guages, most built-in oper­a­tors with spe­cial syn­tax (arith­metic oper­a­tors, sub­script­ing etc.) can be rede­fined for class instances.

 

Sim­ple class definition:

clip_image001

 

To instan­ti­ate a new class:

clip_image002

 

To define a con­struc­tor which takes in some parameters:

clip_image003

to instan­ti­ate this class:

clip_image004

 

When a class defines an __init__() method, class instan­ti­a­tion auto­mat­i­cally invokes this method for the newly-created class instance.

 

To access its attributes:

clip_image005

 

To access its docstring:

clip_image006

 

There are only two kinds of valid attribute names, data attrib­utes and meth­ods.

 

Data attrib­utes don’t need to be declared, they sim­ply spring into exis­tence when they are first assigned to. For instance, for the Per­son class defined above, we can add a new data attribute to an exist­ing instance:

clip_image007

note that the new data attribute belongs to the instance ref­er­enced by x, and doesn’t exist on any other instance of the Per­son class (think Javascript)

 

You can delete the data attribute after you’re done with it:

clip_image008

 

Data attrib­utes can be over­ride by users of an object, so to avoid acci­den­tal name con­flicts it’s best prac­tice to use some kind of con­ven­tion that min­i­mize the chance of con­flicts. E.g. cap­i­tal­iz­ing method names, pre­fix­ing data attribute names with a small unique string, or using verbs for meth­ods and nouns for data attributes.

 

It’s impor­tant to note that noth­ing in Python makes it pos­si­ble to enforce data hid­ing, it’s all based on convention.

 

NOTES: Dif­fer­ence between a func­tion and a method is that a method is a func­tion bound to a class.

 

To define a method on the class:

clip_image009

 

To call the method:

clip_image010

You may have noticed that in the method def­i­n­i­tion say_greeting takes a sin­gle para­me­ter self but it was called with none. This is a spe­cial rule which applies to meth­ods where the object is passed as the first argu­ment. The con­ven­tion is to call the first argu­ment of a method self.

 

It’s not nec­es­sary that the func­tion def­i­n­i­tion is tex­tu­ally enclosed in the class definition:

clip_image011

and you can still use it like before:

clip_image012

 

To call other meth­ods inside the class:

clip_image013

 

To find the class of an instance:

clip_image014

 

To cre­ated a derived class:

clip_image015

because we haven’t defined any new attrib­utes, every­thing will be inher­ited from Per­son includ­ing the __init__ method:

clip_image016

 

If a requested attribute is not found in the class, the search pro­ceeds to look in the base class and if still not found it pro­ceeds to look in the base class of that class, and so on.

You can also extend a base method instead of replac­ing it, to call a base class method:

clip_image017

 

Use isin­stance() func­tion to check if an object is an instance of a class or some class derived from it:

clip_image018

 

Use issub­class() func­tion to check if a class derives from another:

clip_image019

 

Every class keeps these built-in attributes:

  • __dict__ : Dic­tio­nary con­tain­ing the class’s namespace.
  • __doc__ : Class doc­u­men­ta­tion string, or None if undefined.
  • __name__: Class name.
  • __module__: Mod­ule name in which the class is defined. This attribute is “__main__” in inter­ac­tive mode.
  • __bases__ : A pos­si­bly empty tuple con­tain­ing the base classes, in the order of their occur­rence in the base class list.

clip_image020

 

Python’s garbage col­lec­tor runs dur­ing pro­gram exe­cu­tion and is trig­gered when an object’s ref­er­ence count reaches zero.

You can imple­ment a destruc­tor, __del__() method, that is invoked when the instance is about to be destroyed.

 

Here’s a list of some of the meth­ods you can over­ride in your own class:

clip_image021

 

Python sup­ports mul­ti­ple inher­i­tance:

clip_image022

 

Whilst you can’t hide an object’s attrib­utes you can still make them not directly vis­i­ble to out­siders by adding a dou­ble under­score prefix:

clip_image023

What’s hap­pened is that Python changed the name of these attrib­utes to include the class name:

clip_image024

So you can still access them like this:

clip_image025

This is called name man­gling it is mostly designed to avoid acci­den­tal name con­flicts as opposed to pro­vide data hiding.

 

Most con­tainer objects can be looped over using a for state­ment, under­neath, the for state­ment calls iter() on the con­tainer object and gets back an object that defines the method next(). When there are no more ele­ments, next() raises a Sto­pIt­er­a­tion excep­tion which tells the for loop to terminate.

 

Here’s how you might cre­ate a cus­tom iter­a­tor which loops through a con­tainer object in reverse:

clip_image026

clip_image027

 

Mod­ules

A mod­ule allows you to log­i­cally orga­nize your Python code, a mod­ule is a file con­tain­ing Python def­i­n­i­tions and statements.

The file name is the mod­ule name with the .py extension.

Within a mod­ule, the mod­ule name is avail­able as the value of the global vari­able __name__.

You can import an entire mod­ule in your code, or just spe­cific sub­set of the func­tions defined in the mod­ule. For exam­ple, to import the def­i­n­i­tions in a file called ‘fib.py’ in the cur­rent directory:

clip_image001[4]

 

A mod­ule can con­tain exe­cutable state­ments as well as func­tions, these state­ments are intended to ini­tial­ize the mod­ule and are exe­cuted only the first time the mod­ule is imported somewhere.

 

Each mod­ule has its own pri­vate sym­bol table which is used as the global sym­bol table by all the func­tions defined in the mod­ule. This way, you won’t have to worry about acci­den­tal clashes with global vari­able names. How­ever, if required, you could still access a mod­ules global vari­ables with modulename.itemname.

 

You can import a sub­set of the items from a mod­ule using a vari­ant of the import state­ment. To import spe­cific func­tions or variables:

clip_image002[4]

 

You can import all names from a mod­ule except those begin­ning with an underscore:

clip_image003[4]

 

You can use the python exe­cutable to run a python script:

clip_image004[4]

When you do this, the __name__ global vari­able of the script is changed to __main__, and by adding the fol­low­ing lines to your script you can make the file usable as a script as well as an importable module:

clip_image005[4]

clip_image006[9]

 

When you ask to import a mod­ule called fib, Python first looks for a file named fib.py in the cur­rent direc­tory, if not found it then looks in the list of direc­to­ries spec­i­fied by the envi­ron­ment vari­able PYTHONPATH.

You mustn’t name your script the same as a stan­dard mod­ule, or Python will attempt to load the script as a mod­ule when that mod­ule is imported..

 

To improve the start-up time of short pro­grams that use a lot of stan­dard mod­ules, python gen­er­ates a com­piled ver­sion of the mod­ule. For instance, for the mod­ule fib, the fib.pyc con­tains a byte-compiled ver­sion of the fib.py file. The mod­i­fi­ca­tion time of the ver­sion of fib.py used to cre­ate fib.pyc is recorded in fib.pyc file and is used to deter­mine whether fib.pyc is up to date and there­fore if it could be used.

How­ever, it’s worth not­ing that a pro­gram doesn’t run faster when it’s read from a .pyc file instead of .py file, it’s only loaded faster.

When a script is run from the com­mand line the byte-code for the script is never writ­ten to a .pyc file. You can improve the load time for these scripts by mov­ing most of its code to a mod­ule and hav­ing a small boot­strap script that imports that module.

It’s pos­si­ble to have a .pyc file with­out the cor­re­spond­ing .py file, this can be used to dis­trib­ute a library of Python code in a form that is mod­er­ately hard to reverse engineer.

 

You can use the com­pileall mod­ule to cre­ate .pyc files for ALL .py files in a directory:

clip_image007[4]

sys.py is one of the stan­dard mod­ules, and use sys.path you can see all the paths which Python will look when it’s try­ing to find a mod­ule to import:

clip_image008[4]

Of course, you can add to that path:

clip_image009[8]

But remem­ber, this change is only valid in the cur­rent ses­sion, when you restart the inter­preter this new path will be lost.

 

To find out what names are defined in a mod­ule, use the built-in dir() function:

clip_image010[4]

 

With­out argu­ment, dir() lists all the names you have defined currently:

clip_image011[4]

Another handy thing you can do with the dir() func­tion is to use to list all the built-in functions:

clip_image012[4]

 

You can put a col­lec­tion of related mod­ules in a pack­age. Here’s a pos­si­ble struc­ture for a sound pack­age in terms of file hierarchies:

clip_image013[4]

The __init__.py files are required to make Python treat the direc­to­ries as con­tain­ing pack­ages. It can be an empty file, but it can also exe­cute ini­tial­iza­tion code for the package.

 

To import an indi­vid­ual mod­ule of the package:

clip_image014[4]

But it must be ref­er­enced with its full name:

clip_image015[8]

 

Alter­na­tively you could also:

clip_image016[8]

The ben­e­fit of this approach is so that you don’t need the full name to ref­er­ence it:

clip_image017[4]

 

Or, if all you want is the echofil­ter func­tion, you could also:

clip_image018[4]

And to ref­er­ence it:

clip_image019[4]

 

When the users write from sound.effects import * the import state­ment uses the fol­low­ing convention:

  • If a package’s __init__.py code defines a list named __all__, it is taken to be the list of mod­ules names that should be imported when from pack­age import * is encountered:

clip_image020[4]

  • If __all__ is not defined, from sound.effects import * only ensures that the pack­age sound.effects is imported and run any ini­tial­iza­tion code in __init__.py.

 

The rec­om­mended approach is to import spe­cific mod­ules using from Pack­age import module.

 

You can also rel­a­tive paths from the cur­rent mod­ule, so from the sur­round mod­ule you might:

clip_image021[4]

Where . refers to the cur­rent pack­age, .. refers to the par­ent pack­age, ..fil­ters refers to the fil­ters pack­age at the same level as the par­ent package

Because the name of the main mod­ule is always “__main__”, there­fore mod­ules intended for use as the main mod­ule of a Python appli­ca­tion should always use absolute imports.

Share

Fore­words

A while back I decided to try and learn Python for the hell of it as it seems like an inter­est­ing lan­guage and has some of the most con­cise and user-friendly syn­tax. Hav­ing spent some time going through a num­ber of dif­fer­ent learn­ing sources and mate­ri­als (like the offi­cial site python.org which has a very help­ful tuto­r­ial sec­tion) I have put together a set of notes I made as I was learn­ing and hope­fully they can be use­ful to you as a quick list of how-to code snippets.

All the code snap­shots I’m show­ing here are taken from the IDLE Python shell.

Sets

A set is an unordered col­lec­tion with no dupli­cate ele­ments. To cre­ate a set:

clip_image001

notice how the dupli­cate was removed?

 

You can also use a string as a sequence of char objects and turn it into a set:

clip_image002

 

Now, let’s use this two sim­ple sets to illus­trate some basic set operations:

clip_image003

Set dif­fer­ence:

clip_image004

Set union:

clip_image005

Set inter­sect:

clip_image006

Exclu­sive (in one or the other but not both):

clip_image007

 

Dic­tio­nar­ies

Dic­tio­nar­ies are key value pairs indexed by keys, which can be any immutable type. To cre­ate an empty dictionary:

clip_image008

 

You access the ele­ments in a dic­tio­nary using square brack­ets [], you can find out all the keys and val­ues in a dic­tio­nary using the keys and val­ues methods:

clip_image009

 

You can also use the del state­ment to delete a key value pair:

clip_image010

 

You can cre­ate a dic­tio­nary using the dict() method and pass in a sequence of tuples:

clip_image011

if the keys are all strings, you can even do this:

clip_image012

 

To loop through dic­tio­nar­ies, the key and cor­re­spond­ing value can be retrieved at the same time using the iteritems() method:

clip_image013

 

Loops

Use a while loops to gen­er­ate the Finobacci sequence:

clip_image001[10]

 

For loops:

clip_image002[10]

 

Muta­ble sequences like lists should not be mod­i­fied at the same time as loop­ing through it, and instead should be done on a copy:

clip_image003[6]

what would hap­pen if we don’t use a copy here? This code will get stuck in an infi­nite loop:

clip_image004[6]

 

The else and break statement:

Loops in Python can have an else clause which is exe­cuted when the loop ter­mi­nates through exhaus­tion of the list (for for loops) or when the loop con­di­tion becomes false (for while loops), but it won’t exe­cute if the loop is ter­mi­nated with the break statement:

clip_image005[10]

 

The con­tinue statement:

clip_image006[10]

 

The pass statement:

It does noth­ing, often used to cre­ate a min­i­mal class..

clip_image007[6]

 

Func­tions

Use the def key­word to define a function:

clip_image001[12]

Note that the first string is a doc­string that can be used by some tools to gen­er­ate documentations.

 

Argu­ments are passed using call by ref­er­ence, as the argu­ment is always an object ref­er­ence not the value of the object.

Every func­tion has a return value, if noth­ing is returned then a spe­cial None value is returned. Writ­ing of the None value is usu­ally sup­pressed by the inter­preter, but you can see it using print:

clip_image002[12]

 

Use the return key­word to return val­ues from a function:

clip_image003[8]

 

Default argu­ment values:

clip_image004[8]

 

The default val­ues are eval­u­ated once and once only, at the point of func­tion definition:

clip_image005[12]

how­ever, it works dif­fer­ently with muta­ble objects such as lists, dic­tio­nar­ies etc.

clip_image006[12]

notice how the sec­ond func­tion call didn’t use the default empty list? That’s because the object ref­er­enced by the vari­able l is muta­ble and its state was mutated by the first add_item call.

 

You can also call the func­tion using key­word argu­ments if they appear AFTER the reg­u­lar para­me­ters have been passed in:

clip_image007[8]

note that key­word argu­ments can­not be pro­vided twice.

 

You can define an arbi­trary argu­ment list which takes in a tuple:

clip_image008[10]

 

Sim­i­larly, you can take in a dictionary:

clip_image009[6]

 

Inter­est­ingly, the reserve of the above is true too! You can take a tuple and unpack­ing it with the * operator:

clip_image010[6]

 

Or take a dic­tio­nary and unpack­ing it with the ** operator:

clip_image011[6]

 

Lamb­das:

clip_image012[6]

Share