Converging Multiple Classes

Wanderer wanderer at dialup4less.com
Fri Feb 4 18:12:35 CET 2011


On Feb 4, 12:07 pm, Jean-Michel Pichavant <jeanmic... at sequans.com>
wrote:
> Wanderer wrote:
> > I have a bunch of cameras I want to run tests on. They each have
> > different drivers and interfaces. What I want to do is create python
> > wrappers so that they all have a common interface and can be called by
> > the same python test bench program. I'm not sure what to call it. I
> > don't think it's inheritance. Maybe there is no official thing here
> > and I just need to brute force my way through it. Is there some
> > programming methodology I should be using?
>
> > Thanks
>
> I guess Interface/Abstract classes are what you are searching for.
>
> # Camera is the interface/abstract base class of all cameras.
> # it defines all the function that a Camera needs to implement.
> class Camera(object):
>     def printCameraType(self):
>        # This code is common to all Cameras
>        print self.__class__.__name__
>
>     def shutdown(self):
>        # an abstract method raises NotImplementedError and does not
> implement anything
>        # however it indicates to all child classes what they need to
> implement.
>        raise NotImplementedError()
>
> # One implementation of a Camera
> class ATypeOfCamera(Camera):
>     def shutdown():
>        print 'I am implementing the shutdown for that very specific
> Camera type'
>        return 0
>
> class AnotherTypeOfCamera(Camera):
>     def shutdown():
>        print 'Shutting down with the proper implementation'
>        return 0
>
> Now here is what you test bench whould look like:
>
> from camera import ATypeOfCamera, AnotherTypeOfCamera
>
> for cameraType in [ATypeOfCamera, AnotherTypeOfCamera]:
>     myCam = cameraType()
>     myCam.printCameraType()
>     myCam.shutdown()
>
> JM

Thanks JM. That help a lot.



More information about the Python-list mailing list