参数化模块导入 [英] Parametrized module import

查看:43
本文介绍了参数化模块导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模块,其行为需要是可配置的。模块

需要在第一次导入时决定它们提供的替代

接口。


目前,我设置一些环境变量,选择所需的

行为,模块检查这些变量以确定它应该自行设置的

模式。一些纯粹的Python

解决方案,而不是依赖于外部状态的解决方案。


你能推荐任何方法,还是警告一些陷阱

方法

解决方案
亚切克Generowicz< JA ********** ****@cern.ch>写道:

我有一个模块,其行为需要是可配置的。模块
需要在第一次导入时决定它所呈现的替代接口。

目前,我设置了一些环境变量来选择所需的
行为,模块检查这些变量以确定它应该自行设置的模式。我更喜欢纯粹的Python解决方案,而不是依赖于外部状态的解决方案。

你能推荐任何方法,还是警告一些方法的陷阱?




您可以创建另一个名为config的模块或cfg

,其中包含一些全局变量。您可以将它导入

您的可配置模块以及主程序。

然后您可以通过这些来配置模块的行为

导入模块之前的全局变量。


这样的东西:


#---文件config.py:---

foo_interface_mode = 0#default


#---文件your_module.py:---

导入配置

if config.foo_interface_mode == 0:

...此界面

else:

...该界面


#---文件main_program.py: ---

导入配置

config.foo_interface_mode = 1

import your_module


祝你好运

奥利弗


-

奥利弗Fromme,Konrad-Celtis-Str。 72,81369慕尼黑,德国


`所有我们看到或看起来只是梦中的梦想。'''

(EA Poe)


Oliver Fromme< ol ** @ haluter.fromme.com>写道:

您可以创建另一个名为config的模块或cfg
,其中包含一些全局变量。您可以将它导入到可配置模块以及主程序中。
然后您可以在导入模块之前通过这些全局变量配置模块的行为。




是的,我最初的糟糕原型想法是在sys模块中添加配置

信息,但这种变化更加整洁

....事实上,在考虑它的前2分钟之后,它看起来很完美:-)


然而,有一件事是保持不变的困扰我整个业务,

是导入模块(使用您选择的

配置)导入之后的可能性,在您不知情的情况下

它,具有不同的配置。理想情况下,应该有一些

警告,因为模块已经被导入,你指定的配置被忽略了...而且我

没看到如何实现这个目标。


Heather Coppersmith< me@privacy.net>写道:

Jacek Generowicz< ja ************** @ cern.ch>写道:

Oliver Fromme< ol ** @ haluter.fromme.com>写道:

您可以创建另一个名为config的模块或cfg其中包含一些全局变量。您可以将其导入到可配置模块以及主程序中。然后
你可以在导入模块之前通过那些全局变量来配置模块的行为。



是的,我最初的糟糕原型想法是添加配置
给sys模块的信息,但是这种变化更加整洁了......事实上,在考虑它的前2分钟之后,它看起来很完美:-)

然而,有一件事让我一直困扰着整个
业务,可能是在导入模块后(使用您选择的配置)导入后,
没有你知道它,具有不同的配置。理想情况下应该有一些警告,因为
模块已经被导入,你指定的
配置被忽略了...而且我不知道如何实现
这个。



导入时,模块的顶层是代码被执行,所以在你的模块的顶层尝试这个主题的变化:




它只在导入模块时执行对于

_first_时间,这样就行不通了。


但是,问题可以通过不修改
$ b来解决config中的$ b全局变量模块直接,但是通过

使用只允许一次调用的函数。


#---文件config.py:---

foo_interface_mode =无

DEF set_foo_interface_mode(模式):

如果foo_interface_mode是无:

foo_interface_mode =模式

,否则:

加薪" foo_interface_mode只可以设置一次"


#---文件your_module.py:---

导入配置

如果config.foo_interface_mode是无:

加薪" foo_interface_mode尚未设置"

ELIF配置。 foo_interface_mode == 0:

...这个界面

否则:

...那个界面


#---文件main_program.py:---

导入配置

config.set_foo_interface_mode(1)

进口your_module


当然,您可以使用assert而不是raise,或者只是

打印警告并继续。无论你喜欢什么。


最好的问候

奥利弗


-

奥利弗Fromme,Konrad-Celtis-Str。 72,81369慕尼黑,德国


`所有我们看到或看起来只是梦中的梦想。'''

(EA Poe)


I have a module whose behaviour needs to be configurable. The module
needs to decide, the first time it is imported, beteween alternative
interfaces it presents.

Currently, I set some environment variables which select the desired
behaviour, and the module inspects those variables to determine the
mode in which it should set itself up. I would prefer a pure Python
solution, rather than one which depends on external state.

Can you recommend any approaches, or warn against the pitfalls of some
approaches?

解决方案

Jacek Generowicz <ja**************@cern.ch> wrote:

I have a module whose behaviour needs to be configurable. The module
needs to decide, the first time it is imported, beteween alternative
interfaces it presents.

Currently, I set some environment variables which select the desired
behaviour, and the module inspects those variables to determine the
mode in which it should set itself up. I would prefer a pure Python
solution, rather than one which depends on external state.

Can you recommend any approaches, or warn against the pitfalls of some
approaches?



You could create another module called "config" or "cfg"
which contains some global variables. You import it into
your configurable module as well as into your main program.
Then you can configure the module''s behaviour via those
global variables before importing the module.

Something like this:

#--- File config.py: ---
foo_interface_mode = 0 # default

#--- File your_module.py: ---
import config
if config.foo_interface_mode == 0:
... this interface
else:
... that interface

#--- File main_program.py: ---
import config
config.foo_interface_mode = 1
import your_module

Best regards
Oliver

--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''''
(E. A. Poe)


Oliver Fromme <ol**@haluter.fromme.com> writes:

You could create another module called "config" or "cfg"
which contains some global variables. You import it into
your configurable module as well as into your main program.
Then you can configure the module''s behaviour via those
global variables before importing the module.



Yes, my initial crappy prototype idea was to add configuration
information to the sys module, but this variation is much neater
.... in fact, after the first 2 minutes of thinking about it, it looks
perfect :-)

However, one thing which keeps bothering me about the whole business,
is the possibilty of importing the module (with your chosen
configuration) after it has already been imported, without you knowing
it, with a different configuration. Ideally there should be some
warning about the fact that the configuration you specified is being
ignored as a result of the module already being imported ... and I
don''t see how to achieve this.


Heather Coppersmith <me@privacy.net> wrote:

Jacek Generowicz <ja**************@cern.ch> wrote:

Oliver Fromme <ol**@haluter.fromme.com> writes:

You could create another module called "config" or "cfg" which
contains some global variables. You import it into your
configurable module as well as into your main program. Then
you can configure the module''s behaviour via those global
variables before importing the module.



Yes, my initial crappy prototype idea was to add configuration
information to the sys module, but this variation is much neater
... in fact, after the first 2 minutes of thinking about it, it
looks perfect :-)

However, one thing which keeps bothering me about the whole
business, is the possibilty of importing the module (with your
chosen configuration) after it has already been imported,
without you knowing it, with a different configuration. Ideally
there should be some warning about the fact that the
configuration you specified is being ignored as a result of the
module already being imported ... and I don''t see how to achieve
this.



Upon import, a module''s "top level" code is executed, so try a
variation on this theme at the top level of your module:



It''s only executed when the module is imported for the
_first_ time, so that wouldn''t work.

However, the problem can be solved by not modifying a
global variable in the "config" module directly, but by
using a function which allows only one call.

#--- File config.py: ---
foo_interface_mode = None
def set_foo_interface_mode (mode):
if foo_interface_mode is None:
foo_interface_mode = mode
else:
raise "foo_interface_mode may only be set once"

#--- File your_module.py: ---
import config
if config.foo_interface_mode is None:
raise "foo_interface_mode has not been set"
elif config.foo_interface_mode == 0:
... this interface
else:
... that interface

#--- File main_program.py: ---
import config
config.set_foo_interface_mode (1)
import your_module

Of course, you could use assert instead of raise, or just
print a warning and go on. Whatever you prefer.

Best regards
Oliver

--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''''
(E. A. Poe)


这篇关于参数化模块导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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