【问题标题】:Python - Device Model with different operating modesPython - 具有不同操作模式的设备模型
【发布时间】:2020-02-21 16:06:43
【问题描述】:

我对用 Python 建模设备很感兴趣。让我们以具有两种不同操作模式的相机为例,即。 e.照片模式和视频模式。通常,当视频模式处于活动状态时,不应调用来自照片模式的方法,反之亦然。 IE。不能在照片模式中从视频模式更改帧率 (fps)。

在限制访问其他模式的情况下实现此设备的一个想法是创建一个控制器类来管理相机内组件之间的通信(打开/关闭串行端口、执行硬件测试等)。在控制器类中,实现的模式(照片模式和视频模式)可以分配给控制器,而这些模式可以作为自己的类/库来实现。

一个有效的命令集可能是:

# Python 3
CameraController = Controller()  # Creates an instance of the controller class 'Controller'
CameraController.open()  # Opens all communication ports and other things
CameraController.PhotoMode.open()  # Opens/Initializes the photo mode
CameraController.PhotoMode.take_picture()  # Command of photo mode to take a picture

# It would be necessary to close the actual mode and to initialize the alternative mode

CameraController.VideoMode.open()  # Should give a warning/error/closes automatically the PhotoMode
CameraController.VideoMode.take_video()  #  Command of video mode to take a video

可以通过在 Python 中操作内置对象类的 __getattribute__(self, *args, **kwargs) 方法并检查(在评估方法或属性之前)请求的模式是否处于活动状态来实现此行为。

有人知道对所述行为建模的不同方法吗?我在互联网上进行了一些研究,但没有找到更好的解决此类问题的方法。我想学习如何以最常见的方式实现这种行为。由于代码可移植性有时很重要,因此也欢迎来自其他编程语言的其他常用方法。

【问题讨论】:

    标签: python oop model state


    【解决方案1】:

    您可以使用__getattr__ 来实现此目的。

    class Controller(object):
    
        def __init__(self):
            self.mode = None
    
        def __getattr__(self, item):
            if item in ['PhotoMode', 'VideoMode']:
                self.mode = item
            return self
    
        def open(self):
            if self.mode is None:
                # code to open all ports
                print("opened all ports")
            elif self.mode == 'PhotoMode':
                # open photo ports / close other ports based on self.mode
                print("photo mode opened")
            elif self.mode == 'VideoMode':
                # open video modes / close other ports based on self.mode
                print("video mode opened")
            else:
                #error
                print("unknown mode")
    
        def take_picture(self):
            if self.mode != 'PhotoMode':
                #error
                print("wrong mode for photo")
            # take picture
            pass
    
        def take_video(self):
            if self.mode != 'VideoMode':
                #error
                print("wrong mode for video")
            # take picture
            pass
    
    
    
    # Python 3
    CameraController = Controller()  # Creates an instance of the controller class 'Controller'
    CameraController.open()  # Opens all communication ports and other things
    CameraController.PhotoMode.open()  # Opens/Initializes the photo mode
    CameraController.PhotoMode.take_picture()  # Command of photo mode to take a picture
    
    # It would be necessary to close the actual mode and to initialize the alternative mode
    
    CameraController.VideoMode.open()  # Should give a warning/error/closes automatically the PhotoMode
    CameraController.VideoMode.take_video()  #  Command of video mode to take a video
    

    【讨论】:

    • 建议的解决方案很有趣。有些事情我想避免: - 不同的模式不是作为类实现的(字符串用于区分它们),因此代码维护更加困难 - 每个命令都必须明确使用 if-else 子句来检查可以执行相应的方法 有没有更复杂的策略来解决这个问题?
    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 2019-03-26
    • 1970-01-01
    相关资源
    最近更新 更多