Skip to content

SyncCollection

SyncCollection

SyncCollection(*args: Any, **kwargs: Any)

Bases: Reactive

Virtual base class of SyncDict, SyncList, and SyncSet.

Meant for isinstance and issubclass checks. It can't be instantiated or subclassed.

Example:

from syncwave import SyncCollection, SyncList, Syncwave

syncwave = Syncwave()
sync_list = syncwave.create_store(SyncList[int], name="sync_list")

print(isinstance(sync_list, SyncCollection))  # True
print(issubclass(SyncList, SyncCollection))  # True


Usage Documentation

The SyncCollection Types

Source code in src/syncwave/reactive.py
109
110
111
112
def __init__(self, *args: Any, **kwargs: Any) -> None:
    """Raise `TypeError`: `Reactive` itself cannot be instantiated."""
    if type(self) is Reactive:
        raise TypeError("`Reactive` is a base class and cannot be instantiated.")

SyncDict

SyncDict(
    data: Mapping[KT, VT]
    | Iterable[tuple[KT, VT]] = (),
    /,
    **kwargs: VT,
)

Bases: MutableMapping[KT, VT], Reactive[SyncDictCtx[KT, VT]]

The reactive counterpart of dict.

When live, every change is validated and written to the JSON file, and changes to the file are applied in place. Created directly, it's inert and acts like a dict.

Example:

from syncwave import SyncDict, Syncwave

syncwave = Syncwave()
sync_dict = syncwave.create_store(SyncDict[str, int], name="sync_dict")

sync_dict["a"] = 1
sync_dict["b"] = 2
sync_dict["c"] = 3
print(sync_dict)
print(list(sync_dict.items()))


Usage Documentation

SyncDict

Parameters:

Name Type Description Default
data Mapping[KT, VT] | Iterable[tuple[KT, VT]]

A mapping, or an iterable of key/value pairs.

()
**kwargs VT

Additional entries.

{}
Source code in src/syncwave/sync_collection.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def __init__(
    self,
    data: Mapping[KT, VT] | Iterable[tuple[KT, VT]] = (),
    /,
    **kwargs: VT,
) -> None:
    """Create an inert `SyncDict`, with the same arguments as `dict`.

    Args:
        data: A mapping, or an iterable of key/value pairs.
        **kwargs: Additional entries.

    """
    self.__data = dict(data, **kwargs)

SyncList

SyncList(iterable: Iterable[VT] = ())

Bases: MutableSequence[VT], Reactive[SyncListCtx[VT]]

The reactive counterpart of list.

When live, every change is validated and written to the JSON file, and changes to the file are applied in place. Created directly, it's inert and acts like a list.

References to reactive items follow positions, not content. Slices aren't supported yet.

Example:

from syncwave import SyncList, Syncwave

syncwave = Syncwave()
sync_list = syncwave.create_store(SyncList[int], name="sync_list")

sync_list.append(1)
sync_list.extend([2, 3])
print(sync_list)


Usage Documentation

SyncList

Parameters:

Name Type Description Default
iterable Iterable[VT]

Initial items.

()
Source code in src/syncwave/sync_collection.py
389
390
391
392
393
394
395
396
def __init__(self, iterable: Iterable[VT] = ()) -> None:
    """Create an inert `SyncList`, with the same arguments as `list`.

    Args:
        iterable: Initial items.

    """
    self.__data = list(iterable)

SyncSet

SyncSet(iterable: Iterable[VT] = ())

Bases: MutableSet[VT], Reactive[SyncSetCtx[VT]]

The reactive counterpart of set.

When live, every change is validated and written to the JSON file (as an array), and changes to the file are applied in place. Created directly, it's inert and acts like a set.

Items must be hashable, so they can't be reactive.

Example:

from syncwave import SyncSet, Syncwave

syncwave = Syncwave()
sync_set: SyncSet[int] = syncwave.create_store(SyncSet[int], name="sync_set")

sync_set.add(1)
sync_set.add(2)
sync_set.discard(2)
print(sync_set)


Usage Documentation

SyncSet

Parameters:

Name Type Description Default
iterable Iterable[VT]

Initial items.

()
Source code in src/syncwave/sync_collection.py
650
651
652
653
654
655
656
657
def __init__(self, iterable: Iterable[VT] = ()) -> None:
    """Create an inert `SyncSet`, with the same arguments as `set`.

    Args:
        iterable: Initial items.

    """
    self.__data = set(iterable)