Files
flucto-heisskleber/heisskleber/core/packer.py
Felix Weiler 774e0d8496 Breaking: Change signatures of Publisher.send and Subscriber.__init__.
This commit is a lot and adds breaking changes.
Also adds a UDP subscriber and publisher.
2023-11-07 12:41:07 +01:00

45 lines
1.2 KiB
Python

"""Packer and unpacker for network data."""
import json
import pickle
from typing import Any, Callable
from .types import Serializable
def get_packer(style: str) -> Callable[[dict[str, Serializable]], str]:
"""Return a packer function for the given style.
Packer func serializes a given dict."""
if style in _packstyles:
return _packstyles[style]
else:
return _packstyles["default"]
def get_unpacker(style: str) -> Callable[[str], dict[str, Serializable]]:
"""Return an unpacker function for the given style.
Unpacker func deserializes a string."""
if style in _unpackstyles:
return _unpackstyles[style]
else:
return _unpackstyles["default"]
def serialpacker(data: dict[str, Any]) -> str:
return ",".join([str(v) for v in data.values()])
_packstyles: dict[str, Callable[[dict[str, Serializable]], str]] = {
"default": json.dumps,
"json": json.dumps,
"pickle": pickle.dumps, # type: ignore
"serial": serialpacker,
}
_unpackstyles: dict[str, Callable[[str], dict[str, Serializable]]] = {
"default": json.loads,
"json": json.loads,
"pickle": pickle.loads, # type: ignore
}