OO design

Terry Hancock hancock at anansispaceworks.com
Tue Jul 19 22:07:08 EDT 2005


On Tuesday 19 July 2005 05:09 am, chris wrote:
>  So its feels like I need to get into class programming
> with all its attendant benefits. However my biggest problem is a conceptual
> one. I just can't get my head around defining suitable classes, how they
> aquire data and communicate with each other. I'm hoping some of you python
> lamas out there might be able to share some of your wisdom on the subject.

Doubtful, Llamas speak Perl.

It's some kind of rodent that does Python. "Womp rats", probably.  And Pythons,
of course.

> What I basically do is a lot of the following::
> 
> 1. get arbitrary numerical data (typically large data sets in columnar
> format or even via COM from other packages. I generally have to deal with
> one or more sets of X,Y data)
> 2. manipulate the data (scaling, least squares fitting, means, peaks,
> add/subtract one XY set from another etc)
> 3. plot data (original set, results of manipulation, scatterplot, histograms
> etc  - I use matplotlib)
> 4. export data (print, csv, shelve)

Well, obviously you have some options, but let's think about everything
in that description that *could* be an object:

* Data set (1)
* Individual row of a data set (1)
* Transformation of a data set (2)
* Plot data (3)
* Plot (3) (probably same as "Plot data"

Chances are that "Individual row of a data set" is a dumb enough object
to be represented by a built-in type (maybe a tuple).  "Data set" is obviously
a collection, possibly ordered, so it might subclass "list" (or just be a list).

So far, though, you could just use a "list of tuples".  Serialization/deserialization
of the data is one reason for subclassing list, though.  Or you could have
an object representing the file or serialized version, which has a method for
returning the data set as a list.

"Transformation" is almost certainly a class you want to define.  This would
obviously hold the data required to define the transformation and at least
one method for applying the transformation to a data set. You might even
define math operators on it so you can use operator notation like:

my_transform = Transformation(... initialization values defining the transform ...)
xformed_data_set = my_transform * original_data_set

But of course, you could just use methods:

xformed_data_set = my_transform.apply(original_data_set)

"Plot" is another good defined-class.  Initialize it with things like the plotting
window, scaling, etc and what data set it applies to. It's nice because it can
be 1:1 with the actual display widget in your GUI (if you have one).  Candidates
for attributes would include the lower and upper X and Y limits, log or linear
scale, etc.  Candidates for methods would be displaying the plot, printing
the plot, converting to a string description, etc.

> Clearly I'm having some conceptualisation problems here. Hope someone can
> shed some light on the subject

I think you are trying to use an object as a module.  Better to use a module
for that. ;-)

Meanwhile, meditate on what "object" or "thing" means to you, and how
it might map to programming concepts.  What is each "thing" that you can
imagine swapping out in a more sophisticated implementation with lots
of different variations (e.g. data formats, guis, etc).  Clearly modularizing
along the lines of interchangeable elements is also a good approach.

HTH,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list