Main Content

Python - fresh examples of all the fundamentals

Archive - Originally posted on "The Horse's Mouth" - 2009-08-20 22:10:36 - Graham Ellis

Some more new examples in Python - from this week's course.

From my Introduction to Python / simple example to show the power of the language, I present my example that parsed a big data (log) file and counter and sorted by number of accesses the hits from various remote hosts. A long report, ending as follows:
216.129.119.10 549
216.129.119.44 580
66.249.71.164 587
66.249.68.89 2975
217.160.182.81 3215
72.30.142.107 3250
77.88.28.246 4815
66.249.71.55 5457
Dorothy-2:p2 grahamellis$

With the program source available [here].

The some shorter / easier stuff from the first day:

An program to provide a chart of exchange rates - the sort you would give your children when you take them overseas and want to teach them about the local money, and have them work out what is good value. [here].

A short example of how to save a multiline string in a variable in Python. [here].

A prompt ... read ... calculate ... report program in Python. [here].

In Python, you can save yourself writing loops to go through each element of a list by using list methods and operators. We wrote a 'control' example showing how it would work in any language [here], and went on to reduce the looping dramatically [here]. In both cases, the programs produce two lists of 365 elements - representing days of the year. One list has 31 1s, 28 2s and so on - to convert day of year to month of year, and the other list has the numbers 1 to 31, then 1 to 28, and so on, to convert day of year to day of month.

Newcomers often get confused between assigning to the whole of a list (replacing the whole thing) and assigning to an element (replacing just that element and leaving the rest in tact. Our final first day example - [here] illustrates both in source code, with
listname = replacing the whole list and
listname[6] = replacing just the 7th element.

Examples from the second day

Functions, Modules, Packages, and Object Orientation in Python!

In most languages, functions run to completion before they return anything, but in Python you can write a generator which passes back a series of values at yield statements - thus providing an iterator. This is especially useful where you have a big data set - you don't have to read the whole dataset into memory in one go and then process it bit by bit, nor have the read and process in one function. We wrote a 'control' case [here] showing how this would be in a conventional language, and then did it the python way with a generator [here]. You can do this in Lua - using coroutines - too.

The map function converts every member of a list through a function which you pass in as a parameter, and the filter function return you a list of (only) matching itemes, as identified by a callback function in the call. Consider these to be the equivalents of map and grep in Perl if you like ... and see the example source code [here].

As programs grow, you need to separate out your variables into different namespaces - you need to be able to tell "John Smith" from "John Jones" so in Python, you write smith.john of jones.john. And you load namespaces from different files. Example - the file that does the loading [here], and the file it loaded [here] - that latter complete with its own test code to allow you to check it works on its own before integrating it with your main code.

In Java, defining and using a class would be 2 separate files with lots of "public"s and "private"s. And in C++ you would have half a dozen files. Which is why Python's OO model is so sweet - the whole thing can be done in one file - and we have an example [here]. We also have an example with a static (class) variable [here] and we even have examples with multiple classes - look forward to day 3, but they're still potentially all in the one file if they're going to be coded, used and maintained in tandem.

Day 3 - a bit beyond the basics

Some string handling and formatting - [here] is the use of the % operator both for modulo (integer) and formatting - the operator is overloaded - and a regular expression example which shows you how you can split a string at either an exact match, or at a pattern - [here].

Polymorphism and Inheritance - the way data types can all be defined from the same starting point, with only the differences being coded, and then how those subtley different objects can automatically behave in the way designated for them - comes naturally in Python. We have examples [here] which show multiple related classes, and comparators, and [here] showing how they can be wrapped into a module.

Regular expressions ... we cover quite a lot there ... included a new example [here] where I looked for all the postcodes in a string.