在python中配置文件 [英] config files in python

查看:97
本文介绍了在python中配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的应用程序中,我有一些可配置的信息,用于不同进程的
。目前我已将配置存储在

conf.py文件中作为名称=值对,并且我将conf.py文件导入

使用此变量。它运作良好


导入配置

打印conf.SomeVariable


但是如果我需要更改一些配置参数,它需要

我重新启动进程。


我想将这些数据存储在一些conf文件(txt)中并希望

使用它的方式与我使用py

文件中定义的变量相同。


我能想到的一个解决方案是将数据写为一个字典进入conf

文件。然后通过读取数据,对该数据应用eval。并更新

本地字典?但这不是一个好的解决方案....


任何指针?


Sandip

解决方案

sandipm写道:


在我的应用程序中,我有一些可配置的信息用于

by不同的过程。目前我已将配置存储在

conf.py文件中作为名称=值对,并且我将conf.py文件导入

使用此变量。它运作良好


导入配置

打印conf.SomeVariable


但是如果我需要更改一些配置参数,它需要

我才能重启进程。



reload(conf)

http://docs.python.org/lib/built-in-funcs.html#l2h-61


但你应该仔细考虑在你的应用程序中更新配置意味着什么是



Ciao,Michael。


2008年5月5日星期一00:35:51 -0700,sandipm写道:




在我的应用程序中,我有一些可配置的信息,用于不同进程的
。目前我已将配置存储在

conf.py文件中作为名称=值对,并且我将conf.py文件导入

使用此变量。它运作良好


导入配置

打印conf.SomeVariable


但是如果我需要更改一些配置参数,它需要

我重新启动进程。


我想将这些数据存储在一些conf文件(txt)中并希望

使用它的方式与我使用py

文件中定义的变量相同。


我能想到的一个解决方案是将数据写为一个字典进入conf

文件。然后通过读取数据,对该数据应用eval。并更新

本地字典?但这不是一个好的解决方案....


任何指针?


Sandip



已经建议的简单但相对危险的方式是

reload()模块。


更安全的方式 - 但需要更多的工作 - 可能是在标准库中的Configparser模块周围构建一些东西

...


Ciao

-----

FB


5月5日上午10点22分,Francesco Bochicchio< bock ... @ virgilio。 itwrote:


On Mon,05 May 2008 00:35:51 -0700,sandipm写道:




在我的应用程序中,我有一些可配置的信息,用于不同进程的
。目前我已将配置存储在

conf.py文件中作为名称=值对,并且我将conf.py文件导入

使用此变量。它运作良好


import conf

print conf.SomeVariable


但是如果我需要更改一些配置参数,那么重新启动进程需要



我想将这些数据存储在某个conf文件(txt)中,并希望

使用它同样的方式我正在使用py

文件中定义的这些变量。


我能想到的一个解决方案是将数据作为字典写入conf

文件。然后通过读取数据,对该数据应用eval。并更新

本地字典?但这不是一个好的解决方案....


任何指针?


Sandip



简单但相对危险的方式已经建议,是为了
重装()模块。


一种更安全的方式 - 但需要更多的工作 - 可能是建立一些东西

标准库中的Configparser模块...


Ciao

-----

FB



它是如何相对危险的?我的意思是,除了与导入用户定义模块相关的任何危险和/ b $ b之外。


Matt


Hi,
In my application, I have some configurable information which is used
by different processes. currently I have stored configration in a
conf.py file as name=value pairs, and I am importing conf.py file to
use this variable. it works well

import conf
print conf.SomeVariable

but if I need to change some configuration parameteres, it would need
me to restart processes.

I want to store this data in some conf file (txt) and would like to
use it same way as I am using these variables as defined in py
files.

one solution I can think of is writing data as a dictionary into conf
file. and then by reading data, apply eval on that data. and update
local dict? but this is not a good solution....

any pointers?

Sandip

解决方案

sandipm wrote:

In my application, I have some configurable information which is used
by different processes. currently I have stored configration in a
conf.py file as name=value pairs, and I am importing conf.py file to
use this variable. it works well

import conf
print conf.SomeVariable

but if I need to change some configuration parameteres, it would need
me to restart processes.

reload(conf)

http://docs.python.org/lib/built-in-funcs.html#l2h-61

But you should carefully consider what updating the configuration means
in your application.

Ciao, Michael.


On Mon, 05 May 2008 00:35:51 -0700, sandipm wrote:

Hi,
In my application, I have some configurable information which is used
by different processes. currently I have stored configration in a
conf.py file as name=value pairs, and I am importing conf.py file to
use this variable. it works well

import conf
print conf.SomeVariable

but if I need to change some configuration parameteres, it would need
me to restart processes.

I want to store this data in some conf file (txt) and would like to
use it same way as I am using these variables as defined in py
files.

one solution I can think of is writing data as a dictionary into conf
file. and then by reading data, apply eval on that data. and update
local dict? but this is not a good solution....

any pointers?

Sandip

The ''simple but relatively dangerous way'', already suggested, is to
reload() the module.

A safer way - but requiring more work - could be to build something around
the Configparser module in the standard library ...

Ciao
-----
FB


On May 5, 10:22 am, Francesco Bochicchio <bock...@virgilio.itwrote:

On Mon, 05 May 2008 00:35:51 -0700, sandipm wrote:

Hi,
In my application, I have some configurable information which is used
by different processes. currently I have stored configration in a
conf.py file as name=value pairs, and I am importing conf.py file to
use this variable. it works well

import conf
print conf.SomeVariable

but if I need to change some configuration parameteres, it would need
me to restart processes.

I want to store this data in some conf file (txt) and would like to
use it same way as I am using these variables as defined in py
files.

one solution I can think of is writing data as a dictionary into conf
file. and then by reading data, apply eval on that data. and update
local dict? but this is not a good solution....

any pointers?

Sandip


The ''simple but relatively dangerous way'', already suggested, is to
reload() the module.

A safer way - but requiring more work - could be to build something around
the Configparser module in the standard library ...

Ciao
-----
FB

How is it relatively dangerous? I mean, aside from any `dangers''
associated with importing a user defined module in the first place.

Matt


这篇关于在python中配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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