Converging Multiple Classes

Discussion in 'Python' started by Wanderer, Feb 4, 2011.

  1. Wanderer

    Wanderer Guest

    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
    Wanderer, Feb 4, 2011
    #1
    1. Advertising

  2. 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
    Jean-Michel Pichavant, Feb 4, 2011
    #2
    1. Advertising

  3. Wanderer

    Wanderer Guest

    On Feb 4, 12:07 pm, Jean-Michel Pichavant <>
    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.
    Wanderer, Feb 4, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,924
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,192
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,391
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Replies:
    11
    Views:
    893
    Andrew Thompson
    Jul 17, 2004
  5. JHenstay
    Replies:
    3
    Views:
    539
    osmium
    Jan 11, 2004
Loading...

Share This Page