phyelds.data¶
phyelds.data
¶
Internal classes used to manage the state and the neighborhood of the system
(namely rep and nbr of field calculus).
NeighborhoodField(data, node_id, default=_MISSING, *, _sorted=False)
¶
Bases: Generic[T], Iterator[T]
NeighborhoodField class used to manage the interactions between nodes
(namely nbr of neighborhood calculus).
It provides methods to perform operations on the neighborhood, such as addition,
subtraction, multiplication, and division.
You should never use it directly, but rather use the neighbors function
exclude_self()
¶
Return the values of the neighbors, without the current node.
Returns:
| Type | Description |
|---|---|
Dict[int, T]
|
A new dictionary mapping each neighbor ID to its value; the field is unchanged. |
without_self()
¶
Return a new field containing only peer entries (excluding the local node).
local()
¶
Return the value of the current node.
Returns:
| Type | Description |
|---|---|
Optional[T]
|
The value of the current node, or |
with_local(value)
¶
Return a copy of the field where the current node has value.
The values of the neighbors and the default stay the same, and the field itself is not changed. If the field has no value for the current node, the copy adds it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
The new value of the current node. |
required |
Returns:
| Type | Description |
|---|---|
NeighborhoodField[T]
|
A new |
for_node(target_id)
¶
Return the value addressed to target_id, falling back to default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_id
|
int
|
The identifier of the recipient device. |
required |
Returns:
| Type | Description |
|---|---|
Optional[T]
|
The value mapped to |
select(neighborhood)
¶
Return the values of the nodes for which neighborhood holds a truthy value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neighborhood
|
NeighborhoodField[Any]
|
The field used as a filter, typically a field of booleans. |
required |
Returns:
| Type | Description |
|---|---|
List[T]
|
The values of this field, ordered by node ID, whose node is truthy in
|
where(mask)
¶
Return the field restricted to the entries for which mask holds.
Unlike select, the result is a field: reduce it as any other, as in
values.where(children).fold(local, operator.add).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
NeighborhoodField[Any]
|
The field used as a filter, typically a field of booleans. |
required |
Returns:
| Type | Description |
|---|---|
NeighborhoodField[T]
|
A new |
any(predicate=None, *, include_self=True)
¶
Check if any value in the neighborhood satisfies predicate (or is truthy).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Optional[Callable[[T], bool]]
|
Optional condition to test each element against. |
None
|
include_self
|
bool
|
Whether to include the local node's value. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if at least one value matches, False otherwise. |
all(predicate=None, *, include_self=True)
¶
Check if all values in the neighborhood satisfy predicate (or are truthy).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Optional[Callable[[T], bool]]
|
Optional condition to test each element against. |
None
|
include_self
|
bool
|
Whether to include the local node's value. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if every value matches, False otherwise. |
none(predicate=None, *, include_self=True)
¶
Check that no value in the neighborhood satisfies predicate (or is truthy).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Optional[Callable[[T], bool]]
|
Optional condition to test each element against. |
None
|
include_self
|
bool
|
Whether to include the local node's value. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if no value matches, False otherwise. |
count_matching(predicate, *, include_self=True)
¶
Count how many values in the neighborhood satisfy predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
Condition to test each element against. |
required |
include_self
|
bool
|
Whether to include the local node's value. |
True
|
Returns:
| Type | Description |
|---|---|
int
|
The number of matching elements. |
fold(initial, accumulator, *, include_self=True)
¶
Fold the neighborhood values into a single result starting from initial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial
|
U
|
Starting value for the accumulation. |
required |
accumulator
|
Callable[[U, T], U]
|
Binary function |
required |
include_self
|
bool
|
Whether to include the local node's value. |
True
|
Returns:
| Type | Description |
|---|---|
U
|
The accumulated result. |
reduce(reducer, *, include_self=True, default=None)
¶
Reduce the neighborhood values with reducer, returning default if empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reducer
|
Callable[[T, T], T]
|
Binary function combining two elements of type |
required |
include_self
|
bool
|
Whether to include the local node's value. |
True
|
default
|
Any
|
Fallback value returned when there are no elements to reduce. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The reduced value, or |
min(*, include_self=True, default=None, key=None)
¶
Return the minimum value in the neighborhood, or default if empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_self
|
bool
|
Whether to include the local node's value. |
True
|
default
|
Any
|
Fallback returned when no elements are present. |
None
|
key
|
Optional[Callable[[T], Any]]
|
Optional ordering key function. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The minimal value, or |
max(*, include_self=True, default=None, key=None)
¶
Return the maximum value in the neighborhood, or default if empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_self
|
bool
|
Whether to include the local node's value. |
True
|
default
|
Any
|
Fallback returned when no elements are present. |
None
|
key
|
Optional[Callable[[T], Any]]
|
Optional ordering key function. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The maximal value, or |
sum(*, include_self=True, start=0)
¶
Return the sum of the values in the neighborhood.
product(*, include_self=True, start=1)
¶
Return the product of the values in the neighborhood.
id_of_min(*, include_self=True, key=None)
¶
Return the node ID with the minimal value in the neighborhood, or None if empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_self
|
bool
|
Whether to include the local node. |
True
|
key
|
Optional[Callable[[int, T], Any]]
|
Optional function |
None
|
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The ID of the winning entry, or |
id_of_max(*, include_self=True, key=None)
¶
Return the node ID with the maximal value in the neighborhood, or None if empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_self
|
bool
|
Whether to include the local node. |
True
|
key
|
Optional[Callable[[int, T], Any]]
|
Optional function |
None
|
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The ID of the winning entry, or |
coerce_in(lower, upper)
¶
Clamp every value in the field to the closed interval [lower, upper].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lower
|
Any
|
Minimum allowed value. |
required |
upper
|
Any
|
Maximum allowed value. |
required |
Returns:
| Type | Description |
|---|---|
NeighborhoodField[T]
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
replace_matching(replacement, predicate)
¶
Return a new field where values satisfying predicate are replaced with replacement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replacement
|
T
|
Value assigned to matching entries. |
required |
predicate
|
Callable[[T], bool]
|
Condition used to select values to replace. |
required |
Returns:
| Type | Description |
|---|---|
NeighborhoodField[T]
|
A new |
map(func, *values)
¶
Apply func to every value of the field.
Values of the device go to func through values, after each entry, rather than
through a closure: parents.map(operator.eq, local_id()) rather than
parents.map(lambda parent: parent == my_id). An engine running many devices at
once then gives each entry the values of its own device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., U]
|
The function to apply to each value (and to the default, if any). |
required |
*values
|
Any
|
Values of the device, passed to |
()
|
Returns:
| Type | Description |
|---|---|
NeighborhoodField[U]
|
A new |
phyelds.data.codec
¶
Wire format of the messages of a device: the outbox of Engine.cooldown to bytes and back.
Two formats, which work alike on CPython and MicroPython, so that devices on either runtime
understand each other. JSON, the default, is readable: what JSON cannot say (tuples, sets,
keys that are not strings, infinities, fields, the dataclasses of the libraries) is written
as an object with a single tag key; plain JSON objects never appear, so the tags cannot
clash. The binary format is smaller, above all with compact paths: a tag byte before each
value instead of the syntax of JSON. decode tells them apart by the first byte, so
devices sending either can share a network.
register(kind, name=None)
¶
Let a dataclass, or a constant such as a sentinel, travel in messages.
A dataclass travels as its fields; a constant is restored as the very same object,
so that it can still be compared with is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
Any
|
A dataclass, or any other object to treat as a constant. |
required |
name
|
str | None
|
Name on the wire, the same on every device. Defaults to the class name. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
encode(outbox, binary=False)
¶
Encode the messages a device sends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outbox
|
Mapping[Any, Any]
|
The messages, as returned by |
required |
binary
|
bool
|
Whether to write the binary format, smaller, instead of JSON. |
False
|
Returns:
| Type | Description |
|---|---|
bytes
|
The bytes to send. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a value is of a type the codec does not know; see |
decode(payload)
¶
Decode the messages received from a device, in either format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
bytes
|
Bytes produced by |
required |
Returns:
| Type | Description |
|---|---|
dict[Any, Any]
|
The messages, ready to be given to |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the first byte of |