I am trying to abstract this machinery in a single class called
Controller which I want to inherit from either SimController or
RealController based on whether a module level flag SIMULATION is set
to True or False.
so I have something like this:
SIMULATION = False
class SimController(object):
"do sim stuff here"
class RealController(object):
" do real stuff here"
class Controller(SuperKlass):
pass
so if SIMULATION == False I want to be able to instance a Controller
object that inherits from RealController and vice-versa.
I thought this might be possible with metaclasses, but I didnt find
anything useful in the docs or on google.
Thanks for any help!
Cheers,
Chris
SIMULATION = False
class SimController(object):
"do sim stuff here"
class RealController(object):
" do real stuff here"
if SIMULATION:
SuperKlass = SimController
else:
SuperKlass = RealController
class Controller(SuperKlass):
pass
> I am trying to abstract this machinery in a single class called
> Controller which I want to inherit from either SimController or
> RealController based on whether a module level flag SIMULATION is set
> to True or False.
At first sight, that seems kind of odd. Wouldn't it be simpler to have
SimController and RealController inherit from Controller?
i.e.
SIMULATION = False
class SimController(object):
def foo(self):
print 'bar'
class RealController(object):
def foo(self):
print 'baz'
if SIMULATION:
SuperKlass = SimController
else:
SuperKlass = RealController
class Controller(SuperKlass):
pass
In [2]: import testcontroller
In [3]: testcontroller.SIMULATION
Out[3]: False
In [4]: c = testcontroller.Controller()
In [5]: c.foo()
baz
In [6]: testcontroller.SIMULATION = True
In [7]: c = testcontroller.Controller()
In [8]: c.foo()
baz
On Mon, Oct 5, 2009 at 3:32 PM, MRAB <pyt...@mrabarnett.plus.com> wrote:
> Chris Colbert wrote:
>>
>> I have an application that needs to run different depending on whether
>> the input data is being simulated, or provided from instrumentation.
>>
>> I am trying to abstract this machinery in a single class called
>> Controller which I want to inherit from either SimController or
>> RealController based on whether a module level flag SIMULATION is set
>> to True or False.
>>
My whole purpose for Controller is to encapsulate this logic.
So, if the data should be simulated, then i just need to pass
-simulate True as a command line argument, and the controller takes
care of it...
So, the arg parser will have to set the flag before any other
processing begins...
On Mon, Oct 5, 2009 at 3:49 PM, Chris Colbert <scco...@gmail.com> wrote:
> I dont think so, because that would require logic outside of the
> controller class to determine which controller to instantiate.
>
> My whole purpose for Controller is to encapsulate this logic.
>
> So, if the data should be simulated, then i just need to pass
> -simulate True as a command line argument, and the controller takes
> care of it...
>
> On Mon, Oct 5, 2009 at 3:44 PM, Richard Brodie <R.Br...@rl.ac.uk> wrote:
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
def Controller():
if SIMULATION:
SuperKlass = SimController
else:
SuperKlass = RealController
class Controller(SuperKlass):
pass
return Controller()
Yet Richard's design is the way to go.
controller.py:
class Controller:
def getInstance(simulation):
if simulation:
return SimController()
else:
return RealController()
getInstance = staticmethod(getInstance)
class RealController(Controller):
pass
class SimController(Controller):
pass
myController = Controller.getInstance(simulation=True)
I personnally would define getInstance as a module function, but because
you prefer to put all the logic in Controller... It doesn't really
matter in the end.
Cheers,
Jean-Michel
How about:
SIMULATION = True
class Controller(object):
def __new__(cls):
if SIMULATION:
return SimController()
else:
return RealController()
class SimController(object):
pass
class RealController(object):
pass
print Controller()
But my preferred solution (in case SIMULATION never changes during
runtime) would be:
class SimController(object):
pass
class RealController(object):
pass
if SIMULATION:
Controller = SimController
else:
Controller = RealController
print Controller()
Regards,
Mick.
> I dont think so, because that would require logic outside of the
> controller class to determine which controller to instantiate.
>
> My whole purpose for Controller is to encapsulate this logic.
>
> So, if the data should be simulated, then i just need to pass
> -simulate True as a command line argument, and the controller takes
> care of it...
I almost hate to point it out, but that command line argument is
"logic outside of the controller class to determine which controller
to instantiate." It's also a darn sight easier to code.
--
Rhodri James *-* Wildebeest Herder to the Masses