Converging Multiple Classes

W

Wanderer

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
 
J

Jean-Michel Pichavant

Wanderer said:
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
 
W

Wanderer

Wanderer said:
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?

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.
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top