Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Moving from scripting to Object Orientation in Python

Cow at a wire fench, bull at a gateUsing 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
Q906 - Object Orientation and General technical topics - Object Orientation: Individual Objects
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
  [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
  [2393] A first demonstration of OO, including polymorphism - (2009-09-04)
  [2173] Basic OO principles - (2009-05-11)
  [2171] Cleaning up redundant objects - (2009-05-11)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1864] Object Oriented Perl - First Steps - (2008-11-01)
  [1543] Learning Object Oriented Principles (and perhaps Java) - (2008-02-17)
  [507] Introduction to Object Oriented Programming - (2005-11-27)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)

Y106 - Object Oriented Python
  [4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
  [3878] From Structured to Object Oriented Programming. - (2012-10-02)
  [3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
  [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2017] Python - a truly dynamic language - (2009-01-30)
  [1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
  [1306] Python class rattling around - (2007-08-16)
  [900] Python - function v method - (2006-10-20)
  [834] Python makes University Challenge - (2006-08-15)
  [477] Class, static and unbound variables - (2005-10-25)


Back to
Sorta sorting a hash, and what if an exception is NOT thrown - Ruby
Previous and next
or
Horse's mouth home
Forward to
Light bulbs
Some other Articles
Research is exciting. But should routine be automated?
Python for loops - applying a temporary second name to the same object
Melksham to become a part of Trowbridge?
Light bulbs
Moving from scripting to Object Orientation in Python
Sorta sorting a hash, and what if an exception is NOT thrown - Ruby
Sundays - and over eating
Exceptions - a fail-safe way of trapping things that may go wrong
3 digit HTTP status codes - what are they, which are most common, which should be a concern?
Ruby at both extremes of your website
4255 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3436_Mov ... ython.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat