
Using what's been described as the
scripting idiom, a "scripter" will write a series of code steps which perform a task from top to bottom. For a job that only involves simple data manipulation (even if there's a lot of data to have that manipulation applied), this approach can be very effective indeed - a short script, easy to follow, and with no dependencies on other bits of code.
Here's the start of a Python example from yesterday:
fh = open("railstats.xyz")
for station in fh.xreadlines():
parts = station.split("\t")
print parts[1],parts[3],parts[9],parts[6]
The code works well enough, and for a once-only job it's acceptabel and perhaps the most efficient way of coding - but how many jobs are truely "once only"? Notice in my example how specific separators and column numbers from the data file end up in the main program code, making it harder to debug and harder to follow than had it been written like this:
fh = open("railstats.xyz")
for place in fh.xreadlines():
current = station(place)
print current.getcode(), current.getosref(), \
current.getyear(2007), current.getname()
It's now ...
• much clearer what the fields are
• independent of the field separator
• able to hide minute details of the particular column numbers
Of course, that's not the full story. I've still had to provide the code that defines the splitting character, names each of the fields, etc - it's in a
class called station and I'm writing using the
Object Oriented idiom. And that does
where I just have a single simple use to make of a data set lead to code that totals more lines. But, look, I also gain ...
• A data handler that can be reused in other programs
• A data handler which is easily tailored if I want to use it on a somewhate different data set
• A data handler which can be independently maintained and tested
On yesterdays's
Python Course, I wrote the two pieces of code above as parts of complete examples showing the "scripting" and Object Oriented approaches - you can find the complete source of the OO example
[here] and the complete source of the scripting example
[here]. The full data file to accompany the demonstration may be downloaded from
[here]. The scripting approach may be considered to be a bit of a "bull at a gate" approach, hence the illustration ... which was derived from public domain resources at
www.public-domain-image.com.
Technically, a scripting language is one in which each line is translated from source as it's run - and with the languages we teach that's really just Tcl and Shell these days. However, the term has become (ab)used to mean a language that's run direct from source code, all be it through an intermediate compiled form interally and an informal (or perhaps formal) virtual machines. Note added for the purists ;-)
Reuse of classes!. Here comes some of the power - a day later, I reused the same class for a sorting demo ... source code
[here].
(written 2011-09-13, updated 2011-09-14)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y106 - Object Oriented Python [477] Class, static and unbound variables - (2005-10-25)
[834] Python makes University Challenge - (2006-08-15)
[900] Python - function v method - (2006-10-20)
[1306] Python class rattling around - (2007-08-16)
[1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2017] Python - a truly dynamic language - (2009-01-30)
[2169] When should I use OO techniques? - (2009-05-11)
[2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
[3878] From Structured to Object Oriented Programming. - (2012-10-02)
[3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
[4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
[4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
[4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
[4448] What is the difference between a function and a method? - (2015-03-04)
[4591] From single block to structure and object oriented programming - (2015-12-02)
[4650] Why populate object with values as you construct them? - (2016-02-18)
[4721] When to check an object type - Python isinstance example - (2016-11-03)
Q906 - Object Orientation and General technical topics - Object Orientation: Individual Objects [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[507] Introduction to Object Oriented Programming - (2005-11-27)
[1543] Learning Object Oriented Principles (and perhaps Java) - (2008-02-17)
[1864] Object Oriented Perl - First Steps - (2008-11-01)
[2171] Cleaning up redundant objects - (2009-05-11)
[2173] Basic OO principles - (2009-05-11)
[2393] A first demonstration of OO, including polymorphism - (2009-09-04)
[2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
Some other Articles
Research is exciting. But should routine be automated?Python for loops - applying a temporary second name to the same objectMelksham to become a part of Trowbridge?Light bulbsMoving from scripting to Object Orientation in PythonSorta sorting a hash, and what if an exception is NOT thrown - RubySundays - and over eatingExceptions - a fail-safe way of trapping things that may go wrong3 digit HTTP status codes - what are they, which are most common, which should be a concern?Ruby at both extremes of your website