全局包变量,有可能吗? [英] Global package variable, is it possible?

查看:68
本文介绍了全局包变量,有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好同胞pythoneers。我很难过,我希望

也许这里有人会对我的问题有一个优雅的解决方案。

这是我第一次和包裹一起玩,所以我很可能在这里误解了一些东西...


这就是我想要做的事情我的包裹。我希望我的包使用一个

配置文件,我想要的是配置文件在每个模块中神奇地出现

所以我可以抓住值从它没有

必须加载和解析每个包模块中的配置文件。不是很好理解__init__.py文件是如何工作的,我希望它能够像设置ConfigParser对象一样简单,然后我就是

计算(因为一个软件包是一个模块),它现在是全球性的,我的软件包我可以在我的模块中访问它,但我错了...我

不想在初始化时将它传递给每个模块,或者任何事情,或者像这样的b $ b lame。这样做的一个主要原因是我希望能够动态地重新加载配置文件并让我的所有模块

自动接收新的配置。必须有办法做到这一点,

但是看到__init __。py'的命名空间在

包模块中是不可用的,我没看到简单而优雅的方式来做到这一点。

有人有什么建议吗?谢谢!

Hello fellow pythoneers. I''m stumped on something, and I was hoping
maybe someone in here would have an elegant solution to my problem.
This is the first time I''ve played around with packages, so I''m
probably misunderstanding something here...

Here''s what I''d like to do in my package. I want my package to use a
configuration file, and what I''d like is for the config file to appear
magically in each module so I can just grab values from it without
having to load and parse the config file in each package module. Not
quite understanding how the __init__.py file works, I expected it to
be as easy as just setting up the ConfigParser object and then I
figured (since a package is a module) that it would now be global to
my package and I could access it in my modules, but I was wrong... I
don''t want to have to pass it in to each module upon init, or anything
lame like that. A main reason for this is that I''d like to be able to
reload the config file dynamically and have all my modules
automatically receive the new config. There must be a way to do this,
but seeing how __init__.py''s namespace is not available within the
package modules, I don''t see a simple and elegant way to do this.
Does anybody have any suggestions? Thanks!

推荐答案



你试过做的事情:

#module configure.py

value1 = 10

value2 = 20

....

#其他模块

来自configure import *


#现在我可以使用value1 value2等

var = value1 * value2


bye

Heve you tried to do something like:

# module configure.py
value1 = 10
value2 = 20
....
# other module
from configure import *

# now I''m able to use value1 value2 etc.
var = value1 * value2

bye


周五,2007-08-03 17:51 +0000,Fabio Z Tessitore写道:
On Fri, 2007-08-03 at 17:51 +0000, Fabio Z Tessitore wrote:

你试图做类似的事情:


#module configure.py

value1 = 10

value2 = 20

...


#other module

来自configure import *


#现在我可以使用value1 value2等。

var = value1 * value2
Heve you tried to do something like:

# module configure.py
value1 = 10
value2 = 20
...
# other module
from configure import *

# now I''m able to use value1 value2 etc.
var = value1 * value2



正确的想法,错误的执行。请注意,OP说我希望能够动态地重新加载配置文件,并且让我的所有模块都支持
自动接收新的配置。


" from configure import *"将设置导入当前的

命名空间,原始命名空间中的后续更改将在

一般情况下对当前命名空间没有任何影响。


这应该可以解决问题:


#module configure.py

value1 = 10

value2 = 20

....


#其他模块

导入配置


var = configure.value1 * configure.value2


HTH,

-

Carsten Haese
http://informixdb.sourceforge.net


8月3日上午10:51,Fabio Z Tessitore< fabioztessit ... @ libero.it>

写道:
On Aug 3, 10:51 am, Fabio Z Tessitore <fabioztessit...@libero.it>
wrote:

你试过做的事情:


#module configure.py

value1 = 10

value2 = 20

...


#其他模块

来自m配置导入*


#现在我可以使用value1 value2等。

var = value1 * value2


bye
Heve you tried to do something like:

# module configure.py
value1 = 10
value2 = 20
...

# other module
from configure import *

# now I''m able to use value1 value2 etc.
var = value1 * value2

bye



感谢您回复Fabio。我想到了这一点,但我不认为这可以在我的情况下很好地运作。看看下面的




__init__py:

############# ##########

_default_cfgfile =''config.conf''

来自configure import *

cfg = loadcfg (_default_cfgfile)


导入pkg_module1

导入pkg_module2

#EOF

configure.py:

########################
来自ConfigParser的
导入SafeConfigParser

def loadcfg(文件名):

file = open(文件名)

cfg = SafeConfigParser()

cfg.readfp(文件)

返回cfg

#EOF


pkg_module1:

###### ##################

_default_cfgfile =''config.conf''

来自configure import *

cfg = loadcfg(_default_cfgfile)

#EOF

我用这种方法看到的一个问题是我们必须定义

每个模块中的配置文件。或者更好的方法

只能在configure.py中定义配置文件,

然而这似乎也不太理想。我不喜欢它,因为我相信一些与包的位置同等重要的东西

配置文件,由所有模块使用,应该在__init __中定义

没有隐藏在其中一个模块中。


另一个问题是包加载后我们想要

加载一个新的配置文件,会发生什么?通过这种方法,我们将需要重新加载包使用的每个模块来读取新的

配置。即便如此,我们如何告诉每个模块新的配置

文件是什么?如果我确实在configure.py中定义了配置文件,那么

我想我能做的是在configure.py中设置一个cfgfile变量来

新的文件位置并重新加载所有模块。如果没有

全局包变量,那么也许这并不是那么糟糕......


嗯。所以也许这样的事情是有道理的:


__init__py:

#################### ###

_default_cfg_file =''config.conf''


import configure

def loadcfg(filename):

configure.cfgfile = filename

试试:

reload(pkg_module1)

reload(pkg_module2)

除了NameError:

pass

cfg = loadcfg(_default_cfg_file)


import pkg_module1

import pkg_module2

#EOF


confgure.py:

############# ##########

cfgfile =无

def loadcfg()

全球cfgfile

如果不是cfgfile:

返回无

file = open(cfgfile)

cfg = SafeConfigParser()

cfg.readfp(file)

返回cfg

#EOF


pkg_module1:

## #####################

import configure

cfg = configure.loadcfg()

#EOF


这有点令人费解,但我认为它解决了我的大部分

抱怨。谁有更好的想法如何做到这一点?再次感谢

Fabio。


Thanks for the response Fabio. I thought about this, but I don''t
think this will work well in my situation either. Take a look at the
following:

__init__py:
#######################
_default_cfgfile = ''config.conf''
from configure import *
cfg = loadcfg(_default_cfgfile)

import pkg_module1
import pkg_module2
# EOF
configure.py:
########################
from ConfigParser import SafeConfigParser

def loadcfg(filename):
file = open(filename)
cfg = SafeConfigParser()
cfg.readfp(file)
return cfg
# EOF

pkg_module1:
########################
_default_cfgfile = ''config.conf''
from configure import *
cfg = loadcfg(_default_cfgfile)
# EOF
One problem I see with this approach is that we must define the
configuration file in every module. Alternatively a better approach
would be to only define the configuration file within configure.py,
however this also seems less than ideal. I don''t like it because I
believe something as important as the location of the package
configuration file, used by all modules, should defined in __init__
not tucked away in one of it''s modules.

Another problem is that after the package is loaded and we want to
load in a new config file, what will happen? With this approach we''ll
have to reload every module the package uses for them to read the new
config. And even then, how do we tell each module what the new config
file is? If I did define the configuration file in configure.py then
I suppose what I could do is set a cfgfile variable in configure.py to
the new file location and reload all the modules. If there is no
global package variables, then maybe this isn''t so bad...

Hmm. So maybe something like this makes sense:

__init__py:
#######################
_default_cfg_file = ''config.conf''

import configure
def loadcfg(filename):
configure.cfgfile = filename
try:
reload(pkg_module1)
reload(pkg_module2)
except NameError:
pass
cfg = loadcfg(_default_cfg_file)

import pkg_module1
import pkg_module2
# EOF

confgure.py:
#######################
cfgfile = None
def loadcfg()
global cfgfile
if not cfgfile:
return None
file = open(cfgfile)
cfg = SafeConfigParser()
cfg.readfp(file)
return cfg
# EOF

pkg_module1:
#######################
import configure
cfg = configure.loadcfg()
# EOF

It''s a little bit convoluted, but I think it solves most of my
gripes. Anybody have a better idea of how to do this? Thanks again
Fabio.


这篇关于全局包变量,有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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