Selecting a different superclass

P

psaffrey

This might be a pure OO question, but I'm doing it in Python so I'll
ask here.

I'm writing a number crunching bioinformatics application. Read lots
of numbers from files; merge, median and munge; draw plots. I've found
that the most critical part of this work is validation and
traceability - "where does this final value come from? How has it been
combined with other values? Is that right?"

My current implementation stores all my values just as floats with a
class called PointSet for storing one set of such values, with various
mathematical and statistical methods. There are several subclasses of
PointSet (IDPointSet, MicroArrayPointSet) for obtaining values from
different file types and with different processing pipelines.

I'm planning to instead store each value in a TraceablePoint class
which has members that describe the processing stages this value has
undergone and a TraceablePointSet class to store groups of these -
this will contain all the same methods as PointSet, but will operate
on TraceablePoints instead of floats. Of course, this will be much
slower than just floats, so I'd like to be able to switch it on and
off.

The problem is that IDPointSet and MicroArrayPointSet will need to
inherit from PointSet or TraceablePointSet based on whether I'm
handling traceable points or not. Can I select a superclass
conditionally like this in Python? Am I trying to do something really
evil here?

Any other bright ideas on my application also welcome.

Peter
 
M

Marco Mariani

The problem is that IDPointSet and MicroArrayPointSet will need to
inherit from PointSet or TraceablePointSet based on whether I'm
handling traceable points or not. Can I select a superclass
conditionally like this in Python? Am I trying to do something really
evil here?

Any other bright ideas on my application also welcome.

I think you should investigate something different than subclassing,
like a "Strategy" domain pattern or something similar.
 
M

Marco Mariani

Marco said:
I think you should investigate something different than subclassing,
like a "Strategy" domain pattern or something similar.

s/domain/design/
 
C

Chris Rebert

This might be a pure OO question, but I'm doing it in Python so I'll
ask here.
The problem is that IDPointSet and MicroArrayPointSet will need to
inherit from PointSet or TraceablePointSet based on whether I'm
handling traceable points or not. Can I select a superclass
conditionally like this in Python? Am I trying to do something really
evil here?

You're doing something really dynamic; don't think I'd call it
inherently evil though.
All you have to do is use a variable in the inheritance syntax, it's
really quite simple:

superclass = TraceablePointSet if tracing else PointSet

class IDPointSet(superclass):
#body code here

#use same trick for MicroArrayPointSet

Cheers,
Chris
 
J

Jason

Perfect - many thanks. Good to know I'm absolved from evil, also ;)

Peter

Another way would be to have a factory function that builds the
appropriate instance:

class PointSet(object):
@staticmethod
def Create_Instance(*args, **keyargs):
if TRACE_DATA:
return TraceablePointSet(*args, **keyargs)
else:
return PointSet(*args, **keyargs)
# Normal release class body goes here.

class TraceablePointSet(object):
# Normal debug class body goes here

point_set = PointSet.Create_Instance()

This is the way you'd do things if you wanted a mix of your release
class instances and debug class instances. Perhaps there's only a
certain set of initial arguments that need to be checked, or maybe the
TRACE_DATA global can change. Inside the body you could also
explicitly check for a "trace" parameter, like so:

if keyargs.get( 'trace_data', False ):
# Create debug instance ....

That would allow you to create debug instances only when you want
them.

A variation on Chris Rebert's option is also possible:

class _PointSet(object):
# Normal body definition here

class _TraceablePointSet(object):
# Traceable body definition here

if TRACE_DATA:
PointSet = _TraceablePointSet
else:
PointSet = _PointSet

Hope this helps. Python's dynamic nature loves you!

--Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top