Skip to main content

Interfacing with message-broker functionality

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

posted-0.1.5.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

posted-0.1.5-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file posted-0.1.5.tar.gz.

File metadata

  • Download URL: posted-0.1.5.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for posted-0.1.5.tar.gz
Algorithm Hash digest
SHA256 1f66a8bd24f16dab990b004ce6cfd3ab79c802e77ace7550dca4e1dfa3eae65d
MD5 fe8dcdd1ff82c8ca23ea4af38b128512
BLAKE2b-256 f06a3793c866ad91dbb0b71272493fcd484694f0b14f78bafbc9b792e7eaf8ce

See more details on using hashes here.

File details

Details for the file posted-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: posted-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for posted-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4ec6b1bab3f2edcccd8a154167b26dd453552e6a3b1e87e435b085a0c0aeacf1
MD5 88d5b3344547f237e5f206810e1becde
BLAKE2b-256 a9c30f956d42b739e3b4262612815e0bda42d122ad42c280130258fd3728c18f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page