
Data files with each line being a record, and a number of 'columns' of information relating to each record on each line are a very common way of information being passed around - in essence a spreadsheet of data. And a sensible way for programs to handle that data is for each row / record to be converted into an object, so that data specific code can be hidden within (encapsulated in) a class, and with each column / field / attribute / property then being accessed in its own specific way.
Sometimes there is significant work involved in converting the incoming data into objects (data to object mapping), but at other times it can be straightforward. Take an example from last week's course, where I mapped a data file of UK bus stops into objects. As the data source was in comma separated value (CSV) format, and the langauge in use was Python, I was able to use the
csv module that's a part of the Pyton distribution. Reading the data file becomes as single as:
fh = open("Stops.csv","r")
getstop = csv.DictReader(fh)
for place in getstop:
current = stop(place)
With the obeject defination and constructor as follows:
class stop(object):
def __init__(self,table):
self.info = table
The
DictReader class emthod even takes the first line of the data files as headers, and used the field name supplied as the keys for the
dict so that my application can then pick up the unique bus stop code as follows:
def getId(self):
return self.info["NaptanCode"]
And my 'object to string' mapping for printing is as easy as
def __str__(self):
say = (self.info["NaptanCode"],self.info["Easting"],self.info["Northing"],
self.info["Street"],self.info["LocalityName"])
return (", ".join(say))
Compplete application to look up any UK bus stop by its code
[here] and the data file is
[here]. Careful - the data file is as huge as the demo program is tiny!
(written 2016-03-24, updated 2016-03-27)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q908 - Object Orientation and General technical topics - Object Orientation: Design Patterns [485] North, Norther and Northest - PHP 5 Objects - (2005-11-04)
[1224] Object Relation Mapping (ORM) - (2007-06-09)
[2322] Looking for a practical standards course - (2009-08-05)
[2741] What is a factory? - (2010-04-26)
[2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
[3608] Design Patterns - what are they? Why use them? - (2012-02-12)
[3716] Learning C++ - a design pattern for your first class - (2012-05-02)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[3843] Caching Design Patterns - (2012-08-20)
[4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
[4096] Perl design patterns example - (2013-05-20)
[4098] Using object orientation for non-physical objects - (2013-05-22)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4330] Java - factory method, encapsulation, hashmap example - (2014-11-27)
[4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
[4359] How to avoid too many recalculations within an object - (2014-12-21)
[4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
[4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
[4421] How healthy are the stars of stage and screen? - (2015-02-09)
[4581] Thin application, thick objects - keep you main code simple. Example in Ruby - (2015-11-21)
[4626] Singleton design pattern - examples and uses - (2016-01-20)
[4673] Separating detailed data code from the main application - Ruby example - (2016-05-16)
Y110 - Python - File Handling [114] Relative or absolute milkman - (2004-11-10)
[183] The elegance of Python - (2005-01-19)
[1442] Reading a file multiple times - file pointers - (2007-11-23)
[2011] Conversion of OSI grid references to Eastings and Northings - (2009-01-28)
[2282] Checking robots.txt from Python - (2009-07-12)
[2870] Old prices - what would the equivalent price have been in 1966? - (2010-07-14)
[3083] Python - fresh examples from recent courses - (2010-12-11)
[3442] A demonstration of how many Python facilities work together - (2011-09-16)
[3465] How can I do an FTP transfer in Python? - (2011-10-05)
[3558] Python or Lua - which should I use / learn? - (2011-12-21)
[3764] Shell, Awk, Perl of Python? - (2012-06-14)
[4438] Loving programming in Python - and ready to teach YOU how - (2015-02-22)
[4451] Running an operating system command from your Python program - the new way with the subprocess module - (2015-03-06)
[4593] Command line parameter handling in Python via the argparse module - (2015-12-08)
[4708] Scons - a build system in Python - building hello world - (2016-10-29)
[4717] with in Python - examples of use, and of defining your own context - (2016-11-02)
Some other Articles
Wiltshire Supported Bus consultation - final few daysSpring at Well House ManorChippenham to Salisbury by public transport - what we have and what we could haveMallory Place bus stop - services to BathEasy data to object mapping (csv and Python)Recursion in Python - the classic exampleUnique word locator - Python dict exampleWhat is happening on the 231 bus? What are you going to do about it?Prining a pound sign from Python AND running from the command line at the same timeThe end of competition on a bus route - the effects from then end of the 234