Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.6.3 - Improve some ergonomics #171

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:

- run: |
mk python-release owner=vkottler \
repo=runtimepy version=3.6.2
repo=runtimepy version=3.6.3
if: |
matrix.python-version == '3.11'
&& matrix.system == 'ubuntu-latest'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.4
hash=b8d1c553e0054dc44ef17ff31b209064
hash=b0329c7974cadf6f6f543902be7a0ba2
=====================================
-->

# runtimepy ([3.6.2](https://pypi.org/project/runtimepy/))
# runtimepy ([3.6.3](https://pypi.org/project/runtimepy/))

[![python](https://img.shields.io/pypi/pyversions/runtimepy.svg)](https://pypi.org/project/runtimepy/)
![Build Status](https://github.com/vkottler/runtimepy/workflows/Python%20Package/badge.svg)
Expand Down
2 changes: 1 addition & 1 deletion local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 3
minor: 6
patch: 2
patch: 3
entry: runtimepy
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "runtimepy"
version = "3.6.2"
version = "3.6.3"
description = "A framework for implementing Python services."
readme = "README.md"
requires-python = ">=3.11"
Expand Down
4 changes: 2 additions & 2 deletions runtimepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.4
# hash=6b2c4795532f43ad26d48ad4b61ce46a
# hash=fb2f4be3594e1ee7d4d9c065b129dfaa
# =====================================

"""
Expand All @@ -10,7 +10,7 @@

DESCRIPTION = "A framework for implementing Python services."
PKG_NAME = "runtimepy"
VERSION = "3.6.2"
VERSION = "3.6.3"

# runtimepy-specific content.
METRICS_NAME = "metrics"
4 changes: 3 additions & 1 deletion runtimepy/codec/protocol/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def __getitem__(self, name: str) -> ProtocolPrimitive:
"""Get the value of a protocol field."""

if name in self.serializables:
return str(self.serializables[name].__class__.__name__)
return str(self.serializables[name])

return self.value(name)

Expand All @@ -261,5 +261,7 @@ def __setitem__(self, name: str, val: ProtocolPrimitive) -> None:
if isinstance(val, str):
val = self._enum_fields[name].get_int(val)
self._regular_fields[name].value = val
elif name in self.serializables and isinstance(val, str):
self.serializables[name].update_str(val)
else:
self._fields.set(name, val) # type: ignore
7 changes: 7 additions & 0 deletions runtimepy/primitives/serializable/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from typing import BinaryIO as _BinaryIO
from typing import TypeVar

# third-party
from vcorelib import DEFAULT_ENCODING

# internal
from runtimepy.primitives.byte_order import (
DEFAULT_BYTE_ORDER as _DEFAULT_BYTE_ORDER,
Expand Down Expand Up @@ -98,6 +101,10 @@ def update(self, data: bytes) -> int:
"""Update this serializable from a bytes instance."""
raise NotImplementedError

def update_str(self, data: str) -> int:
"""Update this serializable from string data."""
return self.update(data.encode(encoding=DEFAULT_ENCODING))

def _from_stream(self, stream: _BinaryIO) -> int:
"""Update just this instance from a stream."""

Expand Down
4 changes: 0 additions & 4 deletions runtimepy/primitives/serializable/fixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,3 @@ def update(self, data: bytes) -> int:
self.data = data
self.size = len(self.data)
return self.size

def update_str(self, data: str) -> int:
"""Update this chunk from a string."""
return self.update(data.encode())
47 changes: 47 additions & 0 deletions tests/codec/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from runtimepy.codec.protocol import Protocol, ProtocolFactory
from runtimepy.codec.protocol.base import FieldSpec
from runtimepy.enum.registry import EnumRegistry
from runtimepy.primitives import Uint32
from runtimepy.primitives.serializable import PrefixedChunk

# internal
Expand All @@ -20,13 +21,59 @@
class SampleProtocol(ProtocolFactory):
"""A sample protocol implementation."""

protocol = Protocol(EnumRegistry())

@classmethod
def initialize(cls, protocol: Protocol) -> None:
"""Initialize this protocol."""

protocol.add_field("test1", "uint8")


class StartsWithChunk(ProtocolFactory):
"""A sample protocol implementation."""

protocol = Protocol(EnumRegistry())

@classmethod
def initialize(cls, protocol: Protocol) -> None:
"""Initialize this protocol."""

protocol.add_serializable("node_name", PrefixedChunk(Uint32()))
protocol.add_field("a", "uint32")
protocol.add_field("b", "uint32")
protocol.add_field("c", "uint32")


def test_protocol_starts_with_serializable():
"""Test that protocols that start with serializables work."""

proto = StartsWithChunk.instance()

proto["a"] = 1000
proto["b"] = 2000
proto["c"] = 3000

proto["node_name"] = "Hello, world!"
assert proto["node_name"] == "Hello, world!"

new_inst = StartsWithChunk.instance()

# Serialize and then de-serialize.
with BytesIO() as ostream:
size = proto.array.to_stream(ostream)
data = ostream.getvalue()
with BytesIO(data) as istream:
assert new_inst.array.from_stream(istream) == size

assert new_inst["node_name"] == "Hello, world!"
assert new_inst["a"] == 1000
assert new_inst["b"] == 2000
assert new_inst["c"] == 3000

assert proto.size == new_inst.size


def test_protocol_basic():
"""Test basic interactions with protocol objects."""

Expand Down