为什么使用特殊配置格式? [英] why use special config formats?

查看:72
本文介绍了为什么使用特殊配置格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我已经看到很多用于python的配置文件阅读器。是它

ConfigObj( http:// www.voidspace.org.uk/python/configobj.html )或

之类的。对我来说似乎是一种趋势。

我很久以前得出这个结论:你不需要配置文件

for PYTHON。为什么要自己重新发明内容并解析文本,为什么

解释器能为你做到这一点?无论如何,我觉得这个格式非常难看:
http://www.voidspace.org.uk/python/c...ig-file-format


配置有两个用例:静态与动态

配置。


最常见的情况,静态配置,你只需要一个

人工编辑的配置文件,包含键 - 值对。所以只需在你的包中添加一个名为config.py的文件,然后导入它。


例如,如果那是我们的包结构:< br $>
PyApache /

__init__.py

config.py

server.py


然后server.py会这样做:

....

import config

listener_sock.bind((config.host, config.port))

....


和config.py看起来像:

#要绑定的端口to

port = 80

host =" localhost"

timeout = 300

enable_keep_alives = False

options = [1,2,3]

....


不适合保存配置的python ?


第二种情况,动态配置,是指您需要在运行时或以编程方式更改

配置,因此配置

不需要是人类可读的。对于那种情况 - 使用泡菜。和

束(如aspn python cookbook上所示)


class Bunch(对象):

def __init __(self, ** kw):

self .__ dict __。update(kw)


创建初始配置文件:

config = Bunch (port = 80,host =" localhost",timeout = 300,...)

pickle.dump(open(" config.pkl"," wb"),config)<当然,你可以将Bunch''es嵌套在另一个里面,即

config = Bunch(

#global config

port = 80,

host =" localhost",


users = {

" malcom_x":Bunch(

http_path =" / home / joe / httpdocs",

cgi_path =" / home / joe / cgi-bin",

options = [" i love lucy",bush is gay]

),

...

},

...




现在你用它:

#g lobal配置

config = pickle.load(open(" config.pkl"))

listener_sock.bind((config.host,config.port))

#和每用户配置

来自getpass import getuser

print config.users [getuser()]。http_path

....


这样,如果你需要以编程方式改变你的配置,

只需更改和pickle.dump()它。


希望它有所帮助,

-tomer

解决方案

正如我写的那样,这被添加到lang.python.announce:

http://groups.google.com/group/comp....a7b35599f65794


-tomer


** *******@gmail.com 启发我们:我很久以前得出这个结论:你不需要配置文件为PYTHON。为什么重新发明东西并自己解析文本,
为什么口译员能为你做到这一点?


因为你一般不想给配置文件编写者

完全控制Python虚拟机。

对于最常见的情况,静态配置,您只需要一个人工编辑的配置文件,其中包含键 - 值对。所以只需在你的包中添加一个名为config.py的文件,然后将其导入。




只有每个只有一个配置文件才有效/>
您的软件包的安装,并且可由需要

的用户进行配置。例如,UNIX系统上的每用户数据库连接参数

应该在


HOME / .programrc中。程序的首选项

设置应该存储在用户可写的文件中,最好是用户的homedir



Sybren

-

世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa


hey

i''ve been seeing lots of config-file-readers for python. be it
ConfigObj (http://www.voidspace.org.uk/python/configobj.html) or the
like. seems like a trend to me.
i came to this conclusion a long time ago: YOU DON''T NEED CONFIG FILES
FOR PYTHON. why re-invent stuff and parse text by yourself, why the
interpreter can do it for you? and anyway, i find this a very ugly
format:
http://www.voidspace.org.uk/python/c...ig-file-format

there are two use cases for configuration: static vs. dynamic
configuration.

for the most common case, static configuration, you just have a
human-edited config file holding key-and-value pairs. so just add to
your package a file called config.py, and import it.

for example, if that''s our package structure:
PyApache/
__init__.py
config.py
server.py

then server.py would do:
....
import config
listener_sock.bind((config.host, config.port))
....

and config.py would look like:
# the port to bind to
port = 80
host = "localhost"
timeout = 300
enable_keep_alives = False
options = [1, 2, 3]
....

isn''t python suitable enough to hold your configuration?

the second case, dynamic configuration, is when you need to alter your
configuration at runtime or programatically, so the configuration
doesnt need to be human-readable. for that case -- use pickle. and
Bunch (as shown on the aspn python cookbook)

class Bunch(object):
def __init__(self, **kw):
self.__dict__.update(kw)

create the initial config file:
config = Bunch(port = 80, host = "localhost", timeout = 300, ...)
pickle.dump(open("config.pkl", "wb"), config)

of course you can nest Bunch''es inside one another, i.e.,
config = Bunch(
# global config
port = 80,
host = "localhost",

# this is per-user configuration
users = {
"malcom_x" : Bunch(
http_path = "/home/joe/httpdocs",
cgi_path = "/home/joe/cgi-bin",
options = ["i love lucy", "bush is gay"]
),
...
},
...
)

and now you use it:
# global configuration
config = pickle.load(open("config.pkl"))
listener_sock.bind((config.host, config.port))
# and per-user configuration
from getpass import getuser
print config.users[getuser()].http_path
....

that way, if you need to programatically change your configuration,
just change and pickle.dump() it.

hope it helps,
-tomer

解决方案

and just as i was writing, this was added to lang.python.announce:

http://groups.google.com/group/comp....a7b35599f65794

-tomer


to*********@gmail.com enlightened us with:

i came to this conclusion a long time ago: YOU DON''T NEED CONFIG
FILES FOR PYTHON. why re-invent stuff and parse text by yourself,
why the interpreter can do it for you?
Because you generally don''t want to give the configuration file writer
full control over the Python virtual machine.
for the most common case, static configuration, you just have a
human-edited config file holding key-and-value pairs. so just add to
your package a file called config.py, and import it.



Which only works if there is only one configuration file per
installation of your package, and is writable by the users that need
to configure it. For example, per-user database connection parameters
should be in


HOME/.programrc on UNIX systems. A program''s preference
settings should be stored in a user-writable file to, preferably in
the user''s homedir.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


这篇关于为什么使用特殊配置格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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