本文整理匯總了Python中mtm.ioc.Container類的典型用法代碼示例。如果您正苦於以下問題:Python Container類的具體用法?Python Container怎麽用?Python Container使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Container類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test1
def test1(self):
config = {
'PathVars': {
'foo': 'yep [bar]',
'bar': 'result2',
'nest1': 'asdf [foo]',
}
}
Container.bind('Config').toSingle(Config, [config])
Container.bind('VarManager').toSingle(VarManager)
pathMgr = Container.resolve('VarManager')
assertThat(pathMgr.hasKey('foo'))
assertThat(not pathMgr.hasKey('asdf'))
assertThat(pathMgr.tryGet('bobsdf') == None)
assertThat(pathMgr.expand('before [bar] after') == 'before result2 after')
assertThat(pathMgr.expand('before [foo] after') == 'before yep result2 after')
assertThat(not pathMgr.hasKey('qux'))
pathMgr.add('qux', 'sadf')
assertThat(pathMgr.hasKey('qux'))
assertThat(pathMgr.expand('[qux]') == 'sadf')
assertThat(pathMgr.expand('[nest1]') == 'asdf yep result2')
print('Done')
示例2: _request
def _request(self):
if not Container.hasBinding(self._identifier):
return self._default
obj = Container.resolve(self._identifier)
self._assertion(obj)
return obj
示例3: testSingletonType
def testSingletonType(self):
Container.bind('Qux').toSingle(Qux)
test1 = Test1()
test2 = Test2()
self.assertIs(test1.qux, test2.qux)
示例4: installBindings
def installBindings():
Container.bind('LogStream').toSingle(LogStreamFile)
Container.bind('LogStream').toSingle(LogStreamConsole, True, False)
demoConfig = os.path.realpath(os.path.join(ProjenyDir, 'Demo/Projeny.yaml'))
Prj.installBindings(demoConfig)
示例5: testMultiple
def testMultiple(self):
Container.bind('Config').toSingle(Config, loadYamlFilesThatExist(ScriptDir + '/ExampleConfig.yaml', ScriptDir + '/ExampleConfig2.yaml'))
config = Container.resolve('Config')
# From 1
assertIsEqual(config.getString('receipt'), 'Oz-Ware Purchase Invoice')
# From 2
assertIsEqual(config.getString('thing1'), 'Foo')
# Second one should override
assertIsEqual(config.getString('foo2'), 'ipsum')
assertIsEqual(config.getString('nest1', 'firstName'), 'Dorothy')
# Test concatenating lists together
assertIsEqual(config.getList('list1'), ['lorem', 'ipsum', 'asdf', 'joe', 'frank'])
# Test concatenating dictionaries together
assertIsEqual(config.getDictionary('dict1'), {'joe': 5, 'mary': 15, 'kate': 5, 'jim': 10})
assertIsEqual(config.tryGetDictionary(None, 'asdfasdfasdf'), None)
assertIsEqual(config.tryGetDictionary({ 5: 1 }, 'asdfasdfasdf'), { 5: 1 })
assertIsEqual(config.tryGetList(None, 'asdfasdfasdf'), None)
assertIsEqual(config.tryGetList([1, 2], 'asdfasdfasdf'), [1, 2])
assertIsEqual(config.tryGetBool(False, 'zxvzasdfasdfasdf'), False)
assertIsEqual(config.tryGetBool(True, 'zxvzasdfasdfasdf'), True)
assertIsEqual(config.tryGetString('asdf', 'zxvzasdfasdfasdf'), 'asdf')
assertIsEqual(config.tryGetInt(5, 'zxvzasdfasdfasdf'), 5)
示例6: testTransientInstance
def testTransientInstance(self):
Container.bind('Qux').to(Qux)
test1 = Test1()
test2 = Test2()
self.assertIsNot(test1.qux, test2.qux)
示例7: testUseDefault
def testUseDefault(self):
Container.bind('Foo').to(1)
Container.bind('Test1').toSingle(Test1)
test1 = Container.resolve('Test1')
self.assertEqual(test1.foo, 1)
self.assertEqual(test1.bar, 5)
示例8: testSingletonMethodWithParam
def testSingletonMethodWithParam(self):
Container.bind('Qux').toSingle(GetQux, 4)
test1 = Test1()
test2 = Test2()
self.assertIs(test1.qux, test2.qux)
self.assertEqual(test1.qux.GetValue(), 4)
示例9: testTransientInstanceWithParam
def testTransientInstanceWithParam(self):
Container.bind('Qux').to(Qux, 5)
test1 = Test1()
test2 = Test2()
self.assertIsNot(test1.qux, test2.qux)
self.assertEqual(test1.qux.GetValue(), 5)
示例10: testTransientMethod
def testTransientMethod(self):
Container.bind('Qux').to(GetQux, 6)
test1 = Test1()
test2 = Test2()
self.assertIsNot(test1.qux, test2.qux)
self.assertEqual(test1.qux.GetValue(), 6)
示例11: testSpecialChars
def testSpecialChars(self):
Container.bind('Config').toSingle(Config, loadYamlFilesThatExist(ScriptDir + '/ExampleConfig.yaml', ScriptDir + '/ExampleConfig2.yaml'))
config = Container.resolve('Config')
assertIsEqual(config.tryGetString(None, 'foo4'), 'asdf')
assertIsEqual(config.tryGetString(None, 'foo5'), 'zxcv')
assertIsEqual(config.tryGetString(None, 'foo6'), 'asdf')
assertIsEqual(config.tryGetString(None, 'foo7'), 'zxcv')
示例12: test1
def test1(self):
assertThat(False, "TODO")
#Container.bind('Config').toSingle(ConfigXml)
Container.bind('VarManager').toSingle(VarManager)
Container.bind('SystemHelper').toSingle(SystemHelper)
Container.bind('Logger').toSingle(Logger)
self._test1(Container.resolve('SystemHelper'))
示例13: testSimple
def testSimple(self):
yamlPath = ScriptDir + '/ExampleConfig.yaml'
Container.bind('Config').toSingle(Config, loadYamlFilesThatExist(yamlPath))
config = Container.resolve('Config')
assertIsEqual(config.getString('date'), '2012-08-06')
assertIsEqual(config.getString('receipt'), 'Oz-Ware Purchase Invoice')
assertIsEqual(config.getList('places'), ['New Jersey', 'New York'])
assertRaisesAny(lambda: config.getString('places'))
assertRaisesAny(lambda: config.getDictionary('places'))
assertIsEqual(config.getDictionary('customer'),
{'first_name': 'Dorothy', 'family_name': 'Gale'})
# Tests YAML references
assertIsEqual(config.getString('foo1'), config.getString('receipt'))
示例14: testOverrideDefault
def testOverrideDefault(self):
Container.bind('Foo').to(2)
Container.bind('Test1').to(Test1)
Container.bind('Bar').to(3)
test1 = Container.resolve('Test1')
self.assertEqual(test1.foo, 2)
self.assertEqual(test1.bar, 3)
示例15: testOutputToFile
def testOutputToFile(self):
assertThat(False)
#Container.bind('Config').toSingle(ConfigXml)
Container.bind('VarManager').toSingle(VarManager, {
'LogPath': ScriptDir + '/logtest.txt',
'LogPathPrevious': ScriptDir + '/logtest.prev.txt',
})
log = Logger(False, True)
log.heading("heading 1")
log.info("info 1")
log.error("error 1")
log.good("good 1")
log.info("info 2")
log.heading("heading 2")
log.info("info 3")
log.finished("Done")
log.dispose()