我正在尝试使用IPython配置系统为我自己的类提供配置参数。我做了个简单的测试。你知道吗
from IPython.config.configurable import Configurable
from IPython.utils.traitlets import Int, Unicode, Bool
class MyConfigTest(Configurable):
user_name = Unicode(u'Fred', config=True)
def __init__(self, config=None):
super(MyConfigTest, self).__init__(config=config)
def display(self):
print 'my name is ' + self.user_name
然后把这个放到我的配置文件中:
c.MyConfigTest.user_name = 'John'
当我运行IPython--debug时,我可以看到我的配置变量被正确读取:
[TerminalIPythonApp] Config changed:
[TerminalIPythonApp] {'MyConfigTest': {'user_name': 'John'}, 'TerminalIPythonApp
': {'log_level': 10}}
但是当我在代码中实例化MyConfigTest
时,它仍然有默认值:
In [1]: x = MyConfigTest()
In [2]: x
Out[2]: <__main__.MyConfigTest at 0x2e1dc50>
In [3]: x.display()
my name is Fred
我是不是读错了变量?我通读了一些IPython源代码,看不到任何明显的东西。你知道吗
config系统创建一个config对象,在实例化类时传入该对象来配置它们。如果您想在IPython中使用它,可以这样做:
不过,我们并不打算让这个系统在IPython中配置用户对象。这是可能的,但不是为配置系统设计的用例。它是为在IPython扩展中配置类而设计的,并且具有足够的通用性,您可以将它用于完全不同的应用程序(通过子类化
IPython.config.application.Application
)。你知道吗相关问题 更多 >
编程相关推荐