python学习小组分组程序_用于在Python中对某些动作进行分组的类

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.

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.

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...

Thank you :)

解决方案

I assume your configurations builds a tree with nodes beings sections and leaf being configuration options.

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()

This kind of declarative API is achieved with metaclasses have a look at dictshield, and the many ORMs and more that implements such feature.

Given this set of class you would be able to manipulate them as follow:

>>> 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

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值