instance for the current thread or task. Want to use a fresh counter for |
instance for the current thread or task. Want to use a fresh counter for |
a test? Just do this:: |
a test? Just do this:: |
|
|
with Counter(): |
with Counter.new(): |
# code that uses the standard count.* API |
# code that uses the standard count.* API |
|
|
Within the ``with`` block, any code that refers to ``count`` will be using the |
Within the ``with`` block, any code that refers to ``count`` will be using the |
>>> Counter.value # before using a different counter |
>>> Counter.value # before using a different counter |
1 |
1 |
|
|
>>> @context.call_with(Counter()) |
>>> @context.call_with(Counter.new()) |
... def do_it(c): |
... def do_it(c): |
... print Counter.value |
... print Counter.value |
0 |
0 |
|
|
To use it, just do:: |
To use it, just do:: |
|
|
with DoubleCounter(): |
with DoubleCounter.new(): |
# code in this block that calls ``Counter.inc()`` will be incrementing |
# code in this block that calls ``Counter.inc()`` will be incrementing |
# a ``DoubleCounter`` instance by 2 |
# a ``DoubleCounter`` instance by 2 |
|
|
Or, in Python 2.4, you can do something like:: |
Or, in Python 2.4, you can do something like:: |
|
|
>>> @context.call_with(DoubleCounter()) |
>>> @context.call_with(DoubleCounter.new()) |
... def do_it(c): |
... def do_it(c): |
... print Counter.value |
... print Counter.value |
... Counter.inc() |
... Counter.inc() |
want. You can even take a snapshot of the entire current context and restore |
want. You can even take a snapshot of the entire current context and restore |
all the previous values:: |
all the previous values:: |
|
|
with context.swap(): |
with context.only(): |
# code to read config file and set up services |
# code to read config file and set up services |
# code that uses the configured services |
# code that uses the configured services |
|
|
* ``namespace`` would be better off not being a proxy, and should maybe accept |
* ``namespace`` would be better off not being a proxy, and should maybe accept |
type info. |
type info. |
|
|
* Configuration files and "how to specify values" protocol -- e.g. parameter |
* Configuration files and "how to specify values" protocol |
should probably accept param(value) vs. param(lambda: value) |
|
|
|
* Scoped services |
|
|
|
* Components w/state binding and **kw attrs update on init |
* Components w/state binding and **kw attrs update on init |
|
|
* Force Action __exit__ operations to occur in a purely static context (i.e. |
* State __enter__ should lock the state to the current thread, w/o __exit__ or |
no dynamic state available) to avoid re-registration, indirect access to |
swap() or on_exit being possible from other threads, so that they will be |
finalized resources, etc. |
thread-safe. |
|
|
* Detect value calculation cycles |
* Detect value calculation cycles |
|
|