经理/容器类,怎么样?

时间:2018-04-12 16:02:29

标签: python python-3.x object-oriented-analysis

我目前正在设计一个需要管理某个硬件设置的软件。

硬件设置如下:

System design

系统 - 系统包含两个相同的设备,并具有相对于整个系统的某些功能。

设备 - 每个设备包含两个相同的子设备,并且具有相对于两个子设备的某些功能。

子设备 - 每个子设备有4个可配置实体(通过相同的硬件命令控制 - 因此我不将它们视为子子设备)。

我想要实现的目标:

我想通过系统管理器控制所有可配置实体(实体以串行方式计数),这意味着我可以执行以下操作:

system_instance = system_manager_class(some_params)
system_instance.some_func(0) # configure device_manager[0].sub_device_manager[0].entity[0]
system_instance.some_func(5) # configure device_manager[0].sub_device_manager[1].entity[1]
system_instance.some_func(8) # configure device_manager[1].sub_device_manager[1].entity[0]

我想到了什么:

我正在考虑创建一个抽象类,它包含所有子设备函数(调用转换函数)并让system_manager,device_manager和sub_device_manager继承它。因此,所有类都将具有相同的函数名称,我将能够通过系统管理器访问它们。 围绕这些方面的东西:

class abs_sub_device():
    @staticmethod
    def convert_entity(self):
        sub_manager = None
        sub_entity_num = None
        pass

    def set_entity_to_2(entity_num):
        sub_manager, sub_manager_entity_num = self.convert_entity(entity_num)
        sub_manager.some_func(sub_manager_entity_num)


class system_manager(abs_sub_device):
    def __init__(self):
        self.device_manager_list = [] # Initiliaze device list
        self.device_manager_list.append(device_manager())
        self.device_manager_list.append(device_manager())

    def convert_entity(self, entity_num):
        relevant_device_manager = self.device_manager_list[entity_num // 4]
        relevant_entity         = entity_num % 4
        return relevant_device_manage, relevant_entity

class device_manager(abs_sub_device):
    def __init__(self):
        self.sub_device_manager_list = [] # Initiliaze sub device list
        self.sub_device_manager_list.append(sub_device_manager())
        self.sub_device_manager_list.append(sub_device_manager())        

    def convert_entity(self, entity_num):
        relevant_sub_device_manager = self.sub_device_manager_list[entity_num // 4]
        relevant_entity         = entity_num % 4
        return relevant_sub_device_manager, relevant_entity

class sub_device_manager(abs_sub_device):
    def __init__(self): 
        self.entity_list = [0] * 4

    def set_entity_to_2(self, entity_num):
        self.entity_list[entity_num] = 2
  • 该代码用于对我的设计进行一般性理解,而不是用于实际功能。

问题:

在我看来,我试图设计的系统非常通用,并且必须有一个内置的python方式来实现这一点,或者我的整个面向对象看它是错误的。

我真的想知道是否有人有更好的方法来做到这一点。

5 个答案:

答案 0 :(得分:5)

经过多次思考,我认为我找到了一种非常通用的解决方法,使用装饰器,继承和动态函数创建的组合。

主要思想如下:

1)每个层为它自动动态创建所有子层相关函数(在init函数内部,使用init函数上的装饰器)

2)每个创建的函数根据转换函数(它是abs_container_class的静态函数)动态转换实体值,并调用具有相同名称的降低层函数(参见make_convert_function_method)。

3)这基本上导致所有子层功能在更高级别上实现,零代码重复。

def get_relevant_class_method_list(class_instance):
    method_list = [func for func in dir(class_instance) if callable(getattr(class_instance, func)) and not func.startswith("__") and not func.startswith("_")]
    return method_list

def make_convert_function_method(name):
    def _method(self, entity_num, *args):
        sub_manager, sub_manager_entity_num = self._convert_entity(entity_num)
        function_to_call = getattr(sub_manager, name)
        function_to_call(sub_manager_entity_num, *args)        
    return _method


def container_class_init_decorator(function_object):
    def new_init_function(self, *args):
        # Call the init function :
        function_object(self, *args)
        # Get all relevant methods (Of one sub class is enough)
        method_list = get_relevant_class_method_list(self.container_list[0])
        # Dynamically create all sub layer functions :
        for method_name in method_list:
            _method = make_convert_function_method(method_name)
            setattr(type(self), method_name, _method)

    return new_init_function


class abs_container_class():
    @staticmethod
    def _convert_entity(self):
        sub_manager = None
        sub_entity_num = None
        pass

class system_manager(abs_container_class):
    @container_class_init_decorator
    def __init__(self):
        self.device_manager_list = [] # Initiliaze device list
        self.device_manager_list.append(device_manager())
        self.device_manager_list.append(device_manager())
        self.container_list = self.device_manager_list

    def _convert_entity(self, entity_num):
        relevant_device_manager = self.device_manager_list[entity_num // 4]
        relevant_entity         = entity_num % 4
        return relevant_device_manager, relevant_entity

class device_manager(abs_container_class):
    @container_class_init_decorator
    def __init__(self):
        self.sub_device_manager_list = [] # Initiliaze sub device list
        self.sub_device_manager_list.append(sub_device_manager())
        self.sub_device_manager_list.append(sub_device_manager())    
        self.container_list = self.sub_device_manager_list

    def _convert_entity(self, entity_num):
        relevant_sub_device_manager = self.sub_device_manager_list[entity_num // 4]
        relevant_entity         = entity_num % 4
        return relevant_sub_device_manager, relevant_entity

class sub_device_manager():
    def __init__(self): 
        self.entity_list = [0] * 4

    def set_entity_to_value(self, entity_num, required_value):
        self.entity_list[entity_num] = required_value
        print("I set the entity to : {}".format(required_value))

# This is used for auto completion purposes (Using pep convention)
class auto_complete_class(system_manager, device_manager, sub_device_manager):
    pass


system_instance = system_manager() # type: auto_complete_class
system_instance.set_entity_to_value(0, 3)

此解决方案仍有一点问题,因为最高级别的类几乎没有静态实现的功能,所以自动完成不起作用。 为了解决这个问题,我作了一些欺骗,我创建了一个从所有层继承的空类,并使用pep约定向IDE声明它是正在创建的实例的类型(#type:auto_complete_class)。

答案 1 :(得分:4)

这会解决您的问题吗?

class EndDevice:
    def __init__(self, entities_num):
        self.entities = list(range(entities_num))

    @property
    def count_entities(self):
        return len(self.entities)

    def get_entity(self, i):
        return str(i)


class Device:
    def __init__(self, sub_devices):
        self.sub_devices = sub_devices

    @property
    def count_entities(self):
        return sum(sd.count_entities for sd in self.sub_devices)

    def get_entity(self, i):
        c = 0
        for index, sd in enumerate(self.sub_devices):
            if c <= i < sd.count_entities + c:
                return str(index) + " " + sd.get_entity(i - c)
            c += sd.count_entities
        raise IndexError(i)


SystemManager = Device # Are the exact same. This also means you can stack that infinite

sub_devices1 = [EndDevice(4) for _ in range(2)]
sub_devices2 = [EndDevice(4) for _ in range(2)]
system_manager = SystemManager([Device(sub_devices1), Device(sub_devices2)])

print(system_manager.get_entity(0))
print(system_manager.get_entity(5))
print(system_manager.get_entity(15))

答案 2 :(得分:2)

我无法想到比OOP更好的方法,但继承只会为系统管理器提供一组低级函数,所以它就像拥有一个设备管理器和一个子-装置经理。一个更好的事情是,有点像tkinter小部件,有一个系统管理器并初始化所有其他管理器,如树中的孩子,所以:

system = SystemManager()
device1 = DeviceManager(system)
subDevice1 = SubDeviceManager(device1)
device2 = DeviceManager(system)
subDevice2 = SubDeviceManager(device2)

#to execute some_func on subDevice1
system.some_func(0, 0, *someParams)

我们可以通过保留一个“孩子”列表来实现这一目标。高级管理人员和具有参考儿童的职能。

class SystemManager:
  def __init__(self):
    self.children = []
  def some_func(self, child, *params):
    self.children[child].some_func(*params)

class DeviceManager:
  def __init__(self, parent):
    parent.children.append(self)
    self.children = []
  def some_func(self, child, *params):
    self.children[child].some_func(*params)

class SubDeviceManager:
  def __init__(self, parent):
    parent.children.append(self)
    #this may or may not have sub-objects, if it does we need to make it its own children list.
  def some_func(self, *params):
    #do some important stuff

不幸的是,这确实意味着如果我们想要从系统管理器调用子设备管理器的功能而没有很多点,我们将不得不在系统管理器中再次定义它。你可以做的是使用内置的exec()函数,它将接受一个字符串输入并使用Python解释器运行它:

class SystemManager:
  ...
  def execute(self, child, function, *args):
    exec("self.children[child]."+function+"(*args)")

(并保持设备管理器相同)

然后你会在主程序中写一下:

system.execute(0, "some_func", 0, *someArgs)

哪个会打电话

device1.some_func(0, someArgs)

答案 3 :(得分:2)

这就是我的想法:

SystemManager().apply_to_entity(entity_num=7, lambda e: e.value = 2)

class EntitySuperManagerMixin():
    """Mixin to handle logic for managing entity managers."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)  # Supports any kind of __init__ call.
        self._entity_manager_list = []

    def apply_to_entity(self, entity_num, action):
        relevant_entity_manager = self._entity_manager_list[index // 4]
        relevant_entity_num = index % 4
        return relevant_entity_manager.apply_to_entity(
            relevant_entity_num, action)


class SystemManager(EntitySuperManagerMixin):

    def __init__(self):
        super().__init__()
        # An alias for _entity_manager_list to improve readability.
        self.device_manager_list = self._entity_manager_list
        self.device_manager_list.extend(DeviceManager() for _ in range(4))


class DeviceManager(EntitySuperManagerMixin):

    def __init__(self):
        super().__init__()
        # An alias for _entity_manager_list to improve readability.
        self.sub_device_manager_list = self._entity_manager_list
        self.sub_device_manager_list.extend(SubDeviceManager() for _ in range(4))


class SubDeviceManager():
    """Manages entities, not entity managers, thus doesn't inherit the mixin."""

    def __init__(self):
        # Entities need to be classes for this idea to work.
        self._entity_list = [Entity() for _ in range(4)]

    def apply_to_entity(self, entity_num, action):
        return action(self._entity_list[entity_num])


class Entity():
    def __init__(self, initial_value=0):
        self.value = initial_value

有了这个结构:

  • 特定于实体的函数可以保持绑定到Entity类(它所属的位置)。
  • 需要在两个位置更新特定于管理器的代码:EntitySuperManagerMixin和最低级别的管理器(无论如何都需要自定义行为,因为它处理的是实际实体,而不是其他管理器)。

答案 4 :(得分:1)

我看到它的方式,如果你想动态配置系统的不同部分,你需要某种寻址,所以如果你输入一个带有一些参数的ID或地址,系统将知道你正在谈论的哪个子系统的地址和然后使用参数配置该系统。

OOP非常好,然后您可以通过bitwise operators.

轻松操作此类数据

所以基本的寻址是通过二进制系统完成的,所以要在python中做到这一点,首先需要为你的类实现一个地址静态属性,如果系统增长,可能会有一些基本的进一步细节。

addres系统的基本实现如下:

bin(71)
1010 1011
and if we divide it into nibbles 
1010 - device manager 10
1011 - sub device manager 11

所以在这个例子中我们有15个设备管理器和15个子设备管理器的系统,每个设备和子设备管理器都有它的整数地址。所以让我们说你想用子设备管理器访问设备管理器no10 11号。你需要他们的地址是二进制71,你会得到:

system.config(address, parameter )

system.config funcion看起来像这样:

def config(self,address, parameter):  
    device_manager = (address&0xF0)>>4 #10  
    sub_device_manager = address&0xf  # 11  
    if device_manager not in range(self.devices): raise LookupError("device manager not found")
    if sub_device_manager not in range(self.devices[device_manager].device): raise LookupError("sub device manager not found")

    self.devices[device_manager].device[sub_device_manager].implement(parameter)  

在外行中,您会告诉系统设备10的sub_device 11需要使用此参数进行配置。

那么这个设置如何在一些系统基类的python继承类中查看,然后可以合成/继承到不同的类:

class systems(object):

    parent = None #global parent element, defaults to None well for simplicity

    def __init__(self):
        self.addrMASK = 0xf # address mask for that nibble
        self.addr = 0x1 # default address of that element
        self.devices = [] # list of instances of device 
        self.data = {  #some arbitrary data
            "param1":"param_val",
            "param2":"param_val",
            "param3":"param_val",
        }


    def addSubSystem(self,sub_system): # connects elements to eachother

        # checks for valiability
        if not isinstance(sub_system,systems):
            raise TypeError("defined input is not a system type") # to prevent passing an integer or something

        # appends a device to system data 
        self.devices.append(sub_system)

        # search parent variables from sub device manager to system
        obj = self
        while 1:
            if obj.parent is not None:
                obj.parent.addrMASK<<=4 #bitshifts 4 bits
                obj.parent.addr <<=4 #bitshifts 4 bits
                obj = obj.parent
            else:break

        #self management , i am lazy guy so i added this part so i wouldn't have to reset addresses manualy
        self.addrMASK <<=4 #bitshifts 4 bits
        self.addr <<=4 #bitshifts 4 bits

        # this element is added so the obj address is coresponding to place in list, this could be done more eloquently but i didn't know what are your limitations
        if not self.devices:
            self.devices[ len(self.devices)-1 ].addr +=1        
        self.devices[ len(self.devices)-1 ].parent = self

    # helpful for checking data ... gives the address of system
    def __repr__(self):

        return "system at {0:X}, {1:0X}".format(self.addr,self.addrMASK)

    # extra helpful lists data as well
    def __str__(self):
        data = [ '{} : {}\n'.format(k,v) for k,v in self.data.items() ]
        return " ".join([ repr(self),'\n',*data ])

    #checking for data, skips looping over sub systems
    def __contains__(self,system_index):

        return system_index-1 in range(len(self.data))

    # applying parameter change  -- just an example
    def apply(self,par_dict):
        if not isinstance(par_dict,dict): 
            raise TypeError("parameter must be a dict type")
        if any( key in self.data.keys() for key in par_dict.keys() ):

            for k,v in par_dict.items():
                if k in self.data.keys():
                    self.data[k]=v
                else:pass

        else:pass

    # implementing parameters trough addresses
    def implement(self,address,parameter_dictionary):

        if address&self.addrMASK==self.addr:

            if address-self.addr!=0:
                item = (address-self.addr)>>4
                self.devices[item-1].implement( address-self.addr,parameter_dictionary )
            else:
                self.apply(parameter_dictionary)






a = systems()
b = systems()
a.addSubSystem(b)
c = systems()
b.addSubSystem(c)

print('a')
print(a)
print('')

print('b')
print(b)
print('')

print('c')
print(c)
print('')

a.implement(0x100,{"param1":"a"})
a.implement(0x110,{"param1":"b"})
a.implement(0x111,{"param1":"c"})

print('a')
print(a)
print('')

print('b')
print(b)
print('')

print('c')
print(c)
print('')