如何跨模块存储应用程序设置 [英] How to store application settings across modules

查看:18
本文介绍了如何跨模块存储应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从离开我们公司的开发人员那里收到了一个项目.不是太复杂,但看起来不是很好.那么问题来了:应用程序有一些模块,一个是存储一些应用程序的设置".选项(并非所有可能的选项,我们只说两个:foobar).当应用程序启动时,它从命令行读取选项(使用 argparse):

parser.add_argument('--foo', action='store_true')parser.add_argument('--bar', action='store_true')parser.add_argument('--baz', action='store_true')

然后它执行了这个令人讨厌的事情:

用于名称,在 parser.parse_args(sys.argv[1:])._get_kwargs() 中的 val:setattr(sys.modules['settings'], name, val)

首先:我认为这是肮脏的、非pythonic hack.其次,使用这样的代码很不方便,因为当我需要使用 settings.baz 时,IDE 会抱怨它不存在.此 hack 的目的是使从命令行解析的选项在应用程序中进一步使用的所有模块中可用.

我在考虑单例模式之类的东西,但我只在 PHP 中使用过一次,不知道在 python 中这个解决方案是否正确.即使是这样,有人可以举个例子吗?

我是 python 和 SO 的菜鸟,请善待我:)谢谢.

附言很抱歉我的英语可能有错误

解决方案

Python 中的模块 单例对象,使用一个来存储其他模块使用的设置将是非常 Pythonic 的>

讨厌的事情"的第二行只是设置名为 settings 的模块的属性,所以还不错.更糟糕的是第一行的 _get_kwargs() 部分,它访问了 parser.parse_args() 返回的 argparse.Namespace 对象的私有属性code> 以获取从命令行解析的设置的名称和值.一个稍微好一点的方法可能是这样的:

import settings # 可能是空的 .py 文件对于名称,var 中的 val(parser.parse_args(sys.argv[1:])).iteritems():setattr(设置,名称,val)

然而,这不会解决您的 IDE 问题,因为 IDE 不知道动态添加的设置的名称.解决此问题的一种简单方法是在 settings.py 模块中使用某种默认值定义所有可能的属性,而不是使用空的.

模块第一次被import添加到sys.modules字典中,以它的名字作为键和一个的实例types.ModuleType 作为一个值.随后的 import 将首先检查它的条目是否已经存在,如果存在,将跳过重新加载文件——这就是为什么我声称它们基本上是单例对象.对其属性所做的修改将立即对其他导入它的模块可见,或者在之后进行,因此它通常是应用程序内良好的数据共享机制.

I received a project from developer who left our company. Not too complex, but it doesn't look very nice. So here is the question: Application has some modules and one is "settings" which stores some app. options (not all possible options, lets say just two: foo and bar). When application is started it reads options from command line (using argparse):

parser.add_argument('--foo', action='store_true')
parser.add_argument('--bar', action='store_true')
parser.add_argument('--baz', action='store_true')

And then it performs this nasty thing:

for name, val in parser.parse_args(sys.argv[1:])._get_kwargs():
    setattr(sys.modules['settings'], name, val)

First: I think this is dirty, non-pythonic hack. And second, it is simply inconvenient to use such code, because when I need to use settings.baz, IDE complaints that it doesn't exist. The intention of this hack is to make options parsed from command line available in all modules that are used in application further.

I'm thinking about something like singleton pattern, but I only used it once in PHP, and don't know if this correct solution in python. And even if it is, can someone show example?

I'm noob in python and on SO, please be kind to me :) Thanks.

p.s. I'm sorry for possible mistakes in my English

解决方案

Modules in Python are singleton objects, and using one to store the settings used by the other modules would be a very Pythonic

The second line of the "nasty thing" is just setting the attributes of a module named settings and so isn't that bad. What's worse is the _get_kwargs() part of the first line which is accessing a private attribute of the argparse.Namespace object returned by parser.parse_args() to get the names and values of the settings parsed from the command-line. A slightly better way to do it might be something like this:

import settings  # possibly empty .py file

for name, val in vars(parser.parse_args(sys.argv[1:])).iteritems():
    setattr(settings, name, val)

However this won't fix your IDE problems because the IDE doesn't know the name of settings added dynamically. A simple way to fix that would be to define all the possible attributes with some kind of default values in a settings.py module instead of having an empty one.

The first time a module is imported an entry for it is added to the sys.modules dictionary with its name as the key and an instance of types.ModuleType as a value. Subsequent imports will first check to see if an entry for it already exists and will skip reloading the file if it does -- which is why I claim they're basically singleton objects. Modifications made to its attributes will immediately be visible to other modules that have imported it or do so afterwards, so it's generally a good data sharing mechanism within an application.

这篇关于如何跨模块存储应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆