posted
Interfacing with message-broker functionality
To install: pip install posted
Examples
ReactiveScope
A scope that reacts to writes by computing associated functions, themselves writing in the scope, creating a chain reaction that propagates information through the scope.
Parameters
func_nodes : Iterable[ReactiveFuncNode]The functions that will be called when the scope is written to.scope_factory : Callable[[], MutableMapping]A factory that returns a new scope. The scope will be cleared by calling this factory at each call to.clear().
Examples
First, we need some func nodes to define the reaction relationships. We'll stuff these func nodes in a DAG, for ease of use, but it's not necessary.
>>> from meshed import FuncNode, DAG
>>>
>>> def f(a, b):
... return a + b
>>> def g(a_plus_b, d):
... return a_plus_b * d
>>> f_node = FuncNode(func=f, out='a_plus_b')
>>> g_node = FuncNode(func=g, bind={'d': 'b'})
>>> d = DAG((f_node, g_node))
>>>
>>> print(d.dot_digraph_ascii())
<BLANKLINE>
a
<BLANKLINE>
│
│
▼
┌────────┐
b ──▶ │ f │
└────────┘
│ │
│ │
│ ▼
│
│ a_plus_b
│
│ │
│ │
│ ▼
│ ┌────────┐
└─────▶ │ g_ │
└────────┘
│
│
▼
<BLANKLINE>
g
<BLANKLINE>
Now we make a scope with these func nodes.
>>> s = ReactiveScope(d)
The scope starts empty (by default).
>>> s
<ReactiveScope with .scope: {}>
So if we try to access any key, we'll get a KeyError.
>>> s['g'] # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
KeyError: 'g'
That's because we didn't put write anything in the scope yet.
But, if you give g_ enough data to be able to compute g (namely, if you
write values of b and a_plus_b), then g will automatically be computed.
>>> s['b'] = 3
>>> s['a_plus_b'] = 5
>>> s
<ReactiveScope with .scope: {'b': 3, 'a_plus_b': 5, 'g': 15}>
So now we can access g.
>>> s['g']
15
Note though, that we first showed that g appeared in the scope before we
explicitly asked for it. This was to show that g was computed as a
side-effect of writing to the scope, not because we asked for it, triggering the
computation
Let's clear the scope and show that by specifying a and b, we get all the
other values of the network.
>>> s.clear()
>>> s
<ReactiveScope with .scope: {}>
>>> s['a'] = 3
>>> s['b'] = 4
>>> s
<ReactiveScope with .scope: {'a': 3, 'b': 4, 'a_plus_b': 7, 'g': 28}>
>>> s['g'] # (3 + 4) * 4 == 7 * 4 == 28
28
Metadata
Release files for posted 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| posted-0.1.6.tar.gz | 7.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| posted-0.1.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.5 kB
Release files / posted-0.1.6.tar.gz
| Download URL | posted-0.1.6.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
393b94d360bc61fb32cf954d1f5f146f249bb65bac64b8a0b49624b0d4437954
|
|
BLAKE2b-256 checksum How to use checksums |
d8b2381201e79b320747e710d82a5e5db09a96d65f1c466c1b9718067dd28e8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.20
|
Release files / posted-0.1.6-py3-none-any.whl
| Download URL | posted-0.1.6-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6b4ced7d824efde8508befb58f4fdc39480f14f539f9e7eda4bebce0d271897c
|
|
BLAKE2b-256 checksum How to use checksums |
acd702950feec81a6adfa391bdf28154c395d65848d91d41c44630193cc9b06c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.20
|