我有一个对象,它封装了一些具有某些特征的网络配置文件。例如,我的个人资料有一个连接,根据它,它可能或不能有 IP 选项(静态或< EM> DHCP )。
所以,我的第一次尝试是使用从 dict 扩展的普通类并添加一些辅助函数:
class Profile(dict):
IP_CONNECTIONS = ('ethernet', 'wireless', 'pppoe')
def is_ethernet(self): return self['Connection'] == 'ethernet'
def is_wireless(self): return self['Connection'] == 'wireless'
def is_wireless_adhoc(self): return self.is_wireless() and 'AdHoc' in self
def has_ip(self)
return self['Connection'] in self.IP_CONNECTIONS
def has_ip_static(self)
if not self.has_ip():
return False
if self.is_ipv4():
return self['IP'] == 'static'
if self.is_ipv6():
return self['IP6'] == 'static'
return False
def has_ip_dhcp(self):
if not self.has_ip():
return False
if self.is_ipv4():
return self['IP'] == 'dhcp'
if self.is_ipv6():
return self['IP6'] == 'dhcp' or self['IP6'] == 'dhcp-noaddr'
return False
def is_ipv4(self): return self.has_ip() and 'IP' in self
def is_ipv6(self): return self.has_ip() and 'IP6' in self
def get_client(self):
if self.has_ip_dhcp() and 'DHCPClient' in self:
return self['DHCPClient']
return None
这很有效,但我有一个庞大的课程,有很多is_*
和has_*
特征功能。其中大多数只会用于非常具体的个人资料,并且大部分时间都会返回False
。
然后我突然想到我可以使用继承来描述特征。
尝试并且未能实现元类后,因为调用__new__
方法时数据尚未可用。我想出了类似的东西:
def load_profile(filename):
data = _read_profile(filename)
bases = _classify_profile(data)
baseclass = type('Profile', bases, {})
return baseclass(data)
class IP:
CONNECTIONS = ('ethernet', 'wireless')
class IPv4(IP):
def is_static(self):
return self['IP'] == 'static'
class IPv6(IP):
def is_static(self):
return self['IP6'] == 'static'
class DHCP:
def get_client(self):
return self['DHCPClient'] if 'DHCPClient' in self else None
class Wireless:
def is_adhoc(self):
return 'AdHoc' in self
def _classify_profile(data):
classes = [dict]
if data['Connection'] == 'wireless':
classes.append(Wireless)
if data['Connection'] in IP.CONNECTIONS:
if 'IP' in data:
classes.append(IPv4)
if data['IP'] == 'dhcp':
classes.append(DHCP)
if 'IP6' in data:
classes.append(IPv6)
if data['IP6'] == 'dhcp' or data['IP6'] == 'dhcp-noaddr':
classes.append(DHCP)
return tuple(classes)
在我做profile.has_ip()
之前,现在我只用isinstance(profile, IP)
进行测试。在我看来,责任分离更加清晰。
问题:这是实现动态继承的好方法吗?这样做的pythonic方法是什么?
提前致谢!
答案 0 :(得分:0)
我真的不知道动态继承是什么意思,但我会这样写:
base_classes = []
class IP(dict):
CONNECTIONS = ('ethernet', 'wireless')
def is_static(self):
raise NotImplementedError('To be implemented in subclasses.')
@classmethod
def wants_the_meaningful_named_data(cls, data):
return False
base_classes.append(IP)
class IPv4(IP):
def is_static(self):
return self['IP'] == 'static'
@classmethod
def wants_the_meaningful_named_data(cls, data):
return data['Connection'] in cls.CONNECTIONS and 'IP' in data
base_classes.append(IPv4)
class IPv6(IP):
def is_static(self):
return self['IP6'] == 'static'
@classmethod
def wants_the_meaningful_named_data(cls, data):
return data['Connection'] in cls.CONNECTIONS and 'IP6' in data
base_classes.append(IPv6)
def load_profile(filename):
data = _read_profile(filename)
for base_class in base_classes:
if base_class.wants_the_meaningful_named_data(data):
return base_class(data)
return dict(data)
这样的事情就是我喜欢的。我认为不需要进入元类。