用于在Python中对某些动作进行分组的类 [英] Classes to group some actions in Python
问题描述
我正在为自己的Linux发行版编写配置程序。配置分为以下部分:常规,联网,会话等。
-将相似的选项分组。例如。在一般部分中,有计算机名称,描述,工作组,语言
选项。
I am writing configuration program for my own Linux distribution. The configuration is divided into sections: general, networking, session, etc.
- which groups similar options. E.g. in section general there are computer name, description, workgroup, language
options.
我认为每个部分都应显示按类,每个选项应具有相应的属性(getter或setter)。
I think every section should be presented by the class, and each option should have corresponding property (getter and maybe setter).
此外,如果有功能可以测试给定选项是否启用(例如,系统是否满足此选项的要求),则对通用化也很好:
Moreover it would be nice for generalization if there was function to test if given option is enabled (i.e. if system meets requirements for this option) i.e.:
class General(object):
@property
def name(self):
return self.get_computer_name()
@name.setter
def name(self, name):
self.set_computer_name(name)
def is_option_enabled(self, option):
return True_or_False
我还需要这些选项(和部分)
What is more I need these options (and sections too) link together with corresponding translated name and description (using gettext).
我知道类中的硬编码表示层不是一个好主意...
我需要类似设计模式,总体思路/模板的实现方法,以使其质量高,易于管理和扩展。
I know that hard coding presentation layer in classes is not good idea... I need something like design pattern, general idea/template how to implement this so it was high quality, easy to manage and extend.
我将创建多个前端对于这些类(文本, gui,web),所以我不想每次都重复相同的代码。
I am going to create several front ends for these classes (text, gui, web) so I don't want every time to repeat same code.
我在努力思考,但不幸的是我不知道该怎么做或有任何疑问...
I am thinking very hard, but unfortunately I don't have idea how to do this or have some doubts...
谢谢:)
推荐答案
我假设您的配置构建了一个带有节点bes部分和叶子为配置选项的树。
I assume your configurations builds a tree with nodes beings sections and leaf being configuration options.
鉴于此设置,您可以使用以下类来表示深度为2的配置,例如网络使用声明性API:
Given that setup you can represent a 2 depth deep configuration like network with the following classes using a declarative API:
class InterfaceConfiguration(Configuration):
mask = IPField()
dns = IPField()
IP = IPField()
dhcp = BooleanField()
driver = ChoiceField(choices=('madwifi', 'atheros', 'whatever'))
class NetworkConfiguration(Configuration):
eth0 = InterfaceConfiguration(verbose_name='network interface eth0')
eth1 = InterfaceConfiguration(verbose_name='network interface eth1')
wlan0 = InterfaceConfiguration(verboxe_name='wireless network interface wlan0')
hostname = StringField()
domain = StringField()
这种声明性API是通过元类看看 dictshield 和很多 ORM 和更多实现了此功能。
This kind of declarative API is achieved with metaclasses have a look at dictshield, and the many ORMs and more that implements such feature.
给出了这组
>>> configuration = NetworkConfiguration('/path/to/config/file')
>>> configuration.eth0.verbose_name
'network interface eth0'
>>> configuration.eth0.mask.set('192.168.0.255')
True
>>> configuration.eth0.driver.choices
('madwifi', 'atheros', 'whatever')
>>> configuration.hostname.set('amokrane')
>>> configuration.domain.set('imazighen')
>>> configuration.wlan0.dhcp.get_value()
True
这种API易于实现实现并且不需要特定的python构造(请参阅下文),并提供获取和设置以外的其他方法的功能。
This kind of API is simpler to implement and doesn't require specific python construction (see below) and provide the ability to have other methods besides get and set.
如果您除了不需要其他方法获取/设置您可以使用python描述符实现不同类型的字段,我建议您阅读文章关于该主题的Python属性和方法,具有更深的外观,以及上面有关Python ORM的链接,因为这是使用的方法。
If you don't need other methods besides get/set you can use python descriptors to implement the different kind of fields, I recommand you read the article Python attributes and methods about the subject and have deeper looks and the above links about Python ORMs since that is the used method.
这篇关于用于在Python中对某些动作进行分组的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!