Python中的类层次结构

时间:2017-11-01 18:08:35

标签: python class-hierarchy

我有一个使用Python绑定通过firmata协议连接到arduino的relaisboard。使用pyfirmata(https://github.com/tino/pyFirmata)时,通信工作没有问题。

继电器板有16个继电器。每组3个继电器都是一个通道。每个通道都连接到被测设备输入或输出。这只是对重新发布目的的粗略描述。

您可以在下面找到代码的骨架。

#!/usr/bin/env python

__version__ = '0.1'

# Fault Injection Unit

# Power is connected to Fault Bus 1
# Ground is connected to Fault Bus 2

from pyfirmata import Arduino

class FaultInsertionBoard(object):

    def __init__ (self, comPort = 'COM3'):
        """Initalize the Fault insertion Board

        Open communication with host via serial port

        Arguments:
        comPort -- The serial port used to connect the board to the host.
        """
        self.board = Arduino(comPort)

    class Channel(object):

        def __init__ (self, aChannel):
            """ Create a Channel"""
            pass

        def NoFault():
            """ Set the channel to the "No fault" condition

            No Fault condition is:
            -- DUT channel connected to the testing sistem
            -- DUT channel disconnected from the Fault bus 1  
            -- DUT channel disconnected from the Fault bus 2
            """
            pass


        def OpenCircuit():
            """ Set the channel to the "Open Circuit fault" condition

            Open Circuit fault condition is:
            -- DUT channel disconnected from the testing sistem
            -- DUT channel disconnected from the Fault bus 1  
            -- DUT channel disconnected from the Fault bus 2
            """
            pass

        def ShortToGround():
            """ Set the channel to the "Short to Ground fault" condition

            Open Circuit fault condition is:
            -- DUT channel disconnected from the testing sistem
            -- DUT channel disconnected from the Fault bus 1  
            -- DUT channel connected to the Fault bus 2
            """
            pass

        def ShortToPower():
            """ Set the channel to the "Short to Ground fault" condition

            Open Circuit fault condition is:
            -- DUT channel disconnected from the testing sistem: channel relay is open
            -- DUT channel connected to the Fault bus 1: Fault Bus 1 relay is closed 
            -- DUT channel disconnected from the Fault bus 2: Fault Bus 1 relay is open
            """
            pass

def main():

    FaultBoard = FaultInsertionBoard('COM3')
    VoutSensor = FaultBoard.Channel(0)  
    IOutSensor = FaultBoard.Channel(1)    
    VoutSensor.NoFault()
    IOutSensor.NoFault()
    VoutSensor.ShortToGround()
    IOutSensor.ShortToPower()

if __name__ == "__main__":
    main()

其中:

  • FaultInsertionBoardArduino类的简单包装器 Firmata
  • Channel(n)标识n - 三个群组
  • NoFaultShortToPowerShortToGround是各种配置 每个通道的三个继电器(实际上并不重要) 配置)。

现在的问题是:我对使用C编写的嵌入式固件有很好的经验,远不如Python。显然上面的代码不正确。

有人可以建议我使用类框架来获得上述功能吗?换句话说,如何编写Python代码以便如上所述驱动继电器?

PS:或者我可以这样写:

FaultBoard = FaultInsertionBoard('COM3')
FaultBoard.Channel(0).NoFault()

但我认为它不那么优雅和清晰。

1 个答案:

答案 0 :(得分:0)

一方面,您的实际问题非常普遍,您应该尝试在将来更具体。另一方面,初学者通常很难知道从哪里开始,因此我将为您提供一些设计技巧,以帮助您应对这一挑战。

没有嵌套类

嵌套类在Python中几乎没用。完全合法,但毫无意义。它们不会为您提供对包含类的神奇访问权限,并且不会出现在任何实例中(因为它们可能位于Java中)。所有嵌套都是为了使命名空间更复杂。

我要做的第一件事就是将Channel移出FaultInsertionBoard。一个简单的unindent就足够了。我将向您展示如何进一步使用它。

命名惯例

要记住的另一件事是Python命名约定。虽然不是必需条件,但通常只使用类名称,而其他所有内容都是小写字母,而不是单词之间的下划线(而不是camelCase)。

在函数参数的默认值定义中,=周围放置空格也是常规

我会在这个答案中遵循这些惯例。

继承与遏制

你应该使用继承而不是FaultInsertionBoard的包含:

class FaultInsertionBoard(Arduino):
    pass

这将使FaultInsertionBoard拥有Arduino的所有方法和属性。您现在可以fault_board.method()代替fault_board.board.method(),其中methodArduino类的某种方法。

您可能需要定义一些额外的初始化步骤,例如设置com_port的默认值,以及稍后设置通道。您可以定义自己的__init__版本,并在需要时调用父类的实现:

class FaultInsertionBoard(Arduino):
    def __init__(self, com_port='COM3'):
        super().__init__(com_port)

如果您使用Python 2.x,请使用super(FaultInsertionBoard, self).__init__

添加频道

为了能够实际访问通道实例,您需要定义一些数据结构来保存它们,并预先初始化一些通道。数据结构可以作为属性直接访问,也可以通过对参数进行额外检查的方法来访问。

正如我之前提到的,嵌套Channel类不会让你朝着这个方向前进。实际上,由于您的Channel类可能需要访问其父板,我们将在其构造函数中添加一个新的初始化参数:

class Channel:
    def __init__(self, channel_id, parent):
        self.id = channel_id
        self.parent = parent

您有几种选择。最简单的方法是初始化ChannelFaultInsertionBoard的序列,您可以通过[]而不是()访问该序列:

class FaultInsertionBoard(Arduino):
    def __init__(self, com_port='COM3'):
        super().__init__(com_port)
        self.channels = []
        self.channels.append(Channel(0, self))
        self.channels.append(Channel(1, self))
        ...

现在main将如下所示:

def main():
    fault_board = FaultInsertionBoard('COM3')
    v_out_sensor = fault_board.channels[0]  
    i_out_sensor = fault_board.channel[1]
    v_out_sensor.no_fault()
    v_out_sensor.short_to_ground()
    i_out_sensor.no_fault()
    i_out_sensor.short_to_ground()

如果您绝对想要使用括号以channel(0)等方式访问频道,则可以在FaultInsertionBoard中定义方法。保持__init__方法相同,您可以添加另一种方法:

def channel(self, index):
    # Check index if you want to, possibly raise an error if invalid
    return self.channels[index]

在这种情况下,main将如下所示:

def main():
    fault_board = FaultInsertionBoard('COM3')
    v_out_sensor = fault_board.channel(0)  
    i_out_sensor = fault_board.channel(1)
    v_out_sensor.no_fault()
    v_out_sensor.short_to_ground()
    i_out_sensor.no_fault()
    i_out_sensor.short_to_ground()

第一种方法的优点是允许您直接访问Channel个对象的序列。由于您正在对通道应用相同的操作,因此您可以迭代所有这些操作以获得更简单的界面:

def main():
    fault_board = FaultInsertionBoard('COM3')
    for channel in fault_board.channels:
        channel.no_fault()
        channel.short_to_ground()

便利方法

您的代码中似乎多次使用x.no_fault(); x.short_to_ground()操作。在这种情况下,创建所谓的便捷方法通常很有帮助。您可以将以下内容添加到Channel

def reset(self):
    self.no_fault()
    self.short_to_ground()

然后main看起来像这样:

def main():
    fault_board = FaultInsertionBoard('COM3')
    for channel in fault_board.channels:
        channel.reset()