Python 中对某些操作进行分组的类

发布于 2024-12-29 03:26:46 字数 758 浏览 0 评论 0原文

我正在为我自己的 Linux 发行版编写配置程序。配置分为几个部分:常规、网络、会话等 - 将类似的选项分组。例如,在常规部分有计算机名称、描述、工作组、语言选项。

我认为每个部分都应该由类呈现,每个选项都应该有相应的属性(getter,也许还有 setter)。

此外,如果有功能来测试是否启用给定选项(即系统是否满足此选项的要求),那么对于泛化来说会很好,即:

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

更重要的是,我需要这些选项(以及部分)与相应的翻译名称和描述链接在一起(使用 gettext)。

我知道在类中硬编码表示层不是一个好主意...... 我需要诸如设计模式、总体思路/模板之类的东西来实现它,这样它就具有高质量,易于管理和扩展。

我将为这些类(文本、GUI、Web)创建几个前端,所以我不想每次都重复相同的代码。

我正在努力思考,但不幸的是我不知道该怎么做或者有一些疑问......

谢谢:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦 2025-01-05 03:26:46

我想说你的项目看起来非常雄心勃勃。

对每个部分使用一个类对我来说似乎不是一个好主意,因为它基本上意味着添加、删除或修改部分将需要更改代码,它不是动态的,并且意味着发布与现有版本不兼容的新版本。上一篇。

如果每个部分都以同样的方式处理那就更好了。

对于核心,我认为您需要:

  1. 一个受保护的文件,其中声明了可接受的部分和选项
  2. 一个可以访问此类文件的配置持有者
  3. 配置文件的加载器
    • 读取/解析您的配置文件。
    • 构建您的配置持有者。
  4. 可以访问您的配置持有者的命令
    • 实现此访问的抽象BaseCommand
    • 每个实际命令的子类:GetAddRemove

那么每个前端都只是这些命令的接口。

I'd say that your project looks quite ambitious.

And using a class for every section doesn't seems a good idea to me, because it basically means that adding, removing or modifying sections will require changes to the code, it's not dynamic, and implies the release of a new version incomptable with the previous one.

It would be better if every section will be treated in the same way.

For the core I think you'll need:

  1. A protected file with the declaration of the acceptable sections and options
  2. A configuration holder with access to such file.
  3. A loader for your config files:
    • Read/parse your config files.
    • Build your configuration holder.
  4. Commands with access to your configuration holder:
    • An abstract BaseCommand that implements this access,
    • Subclasses for every actual command: Get, Add, Remove.

Then every front-ends will just be an interface for those commands.

御守 2025-01-05 03:26:46

我假设您的配置构建了一棵树,其中节点是部分,叶子是配置选项。

鉴于该设置,您可以使用声明式 API 来表示 2 深度配置,例如具有以下类的网络:

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很多 ORMmore 实现了此类功能。

给定这组类,您将能够按如下方式操作它们:

>>> 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 构造(见下文),并且提供除 get 和 set 之外的其他方法的能力。

如果除了 get/set 之外不需要其他方法,您可以使用 python 描述符来实现不同类型的字段,我建议您阅读文章 Python 属性和方法 关于该主题,并且有更深入的了解以及上面关于 Python ORM 的链接,因为这是使用的 方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文