ValueError: replaces() must be used only once per class; there is already a |
ValueError: replaces() must be used only once per class; there is already a |
value for ``get``: <bound method ...get of <class 'S1'>> |
value for ``get``: <bound method ...get of <class 'S1'>> |
|
|
|
Services should be subclassable and super() should be usable in spite of all |
|
the singleton-izing magic, for both instance and class methods:: |
|
|
|
>>> class Base(context.Service): |
|
... def test(self, other=None): |
|
... print "hello from Base" |
|
... if other is not None and other is not self: |
|
... print "insanity!" |
|
... t2 = classmethod(test) |
|
|
|
>>> class Sub(Base): |
|
... def test(self): |
|
... print "hello from sub" |
|
... super(Sub, self).test(self) |
|
... def t2(cls): |
|
... print "hello from sub cm" |
|
... super(Sub, cls).t2() |
|
... t2 = classmethod(t2) |
|
|
|
>>> Base.get() is Sub.get() |
|
False |
|
|
|
>>> Sub.test() |
|
hello from sub |
|
hello from Base |
|
|
|
>>> Sub.t2() |
|
hello from sub cm |
|
hello from Base |
|
|
|
Service attributes should be settable and deletable, mapped to the instance:: |
|
|
|
>>> class Dummy(context.Service): |
|
... foo = 42 |
|
|
|
>>> Dummy.foo |
|
42 |
|
|
|
>>> Dummy.foo = 99 |
|
>>> Dummy().foo |
|
42 |
|
|
|
>>> Dummy.get().foo |
|
99 |
|
|
|
>>> del Dummy.foo |
|
>>> Dummy.foo |
|
42 |
|
>>> Dummy.get().foo |
|
42 |
|
|
|
|
Configuration Objects |
Configuration Objects |
Applications and Services |
Applications and Services |
========================= |
========================= |
|
|
|
Simple service replacement in a new application context:: |
|
|
|
>>> S1.get() |
|
<S1 object ...> |
|
|
|
>>> app = context.App() |
|
>>> app[S1] = S2 |
|
|
|
>>> old = app.swap() |
|
|
|
>>> S1.get() |
|
<S2 object ...> |
|
|
|
>>> app is old.swap() |
|
True |
|
|
|
|
By default, service factories are always executed in the context of the |
By default, service factories are always executed in the context of the |
application's main configuration, even if a different configuration is active |
application's main configuration, even if a different configuration is active |
at the time the service is requested:: |
at the time the service is requested:: |
|
|
>>> a = context.App(context.Config()) |
>>> a1 = context.App() |
|
|
>>> def factory(): |
>>> def factory(): |
... print aSetting() |
... print aSetting() |
... return S2() |
... return S2() |
>>> a.config[S1] = factory |
>>> a1[S1] = factory |
>>> context.with_(a.swap(), lambda a: S1.get()) |
>>> a1[S1] is factory |
|
True |
|
|
|
>>> old = a1.swap() |
|
>>> context.App[S1] is factory |
|
True |
|
|
|
>>> S1.get() |
42 |
42 |
<S2 object ...> |
<S2 object ...> |
|
|
>>> a = context.App(context.Config()) |
>>> a2 = context.App() |
>>> a.config[S1] = factory |
>>> a2[S1] = factory |
>>> c = context.Config(a.config) |
>>> c = context.Config(a2.config) |
>>> c[aSetting] = 999 |
>>> c[aSetting] = 999 |
>>> context.with_(a.swap(), lambda a: context.with_(c, lambda c: S1.get())) |
>>> context.with_(context.switch_to(a2), lambda a: context.with_(c, lambda c: S1.get())) |
42 |
42 |
<S2 object ...> |
<S2 object ...> |
|
|
|
>>> context.App.get() is a1 # verify switch_to() manager restored a1 |
|
True |
|
|
|
>>> a1 is old.swap() # verify swap returns previous value |
|
True |
|
|
|
>>> context.App.get() is old # verify swap sets target |
|
True |
|
|
|
|
|
|
Namespaces |
Namespaces |