From a63f0ee1c0c9982e9f30da14c22aaf4705756518 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 7 Jul 2022 19:36:39 -0400 Subject: [PATCH] Add a custom `msgspec.Struct` with some humanizing --- piker/data/types.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 piker/data/types.py diff --git a/piker/data/types.py b/piker/data/types.py new file mode 100644 index 00000000..b7d21680 --- /dev/null +++ b/piker/data/types.py @@ -0,0 +1,46 @@ +# piker: trading gear for hackers +# Copyright (C) Guillermo Rodriguez (in stewardship for piker0) + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +""" +Built-in (extension) types. + +""" +from pprint import pformat + +import msgspec + + +class Struct( + msgspec.Struct, + + # https://jcristharif.com/msgspec/structs.html#tagged-unions + # tag='pikerstruct', + # tag=True, +): + ''' + A "human friendlier" (aka repl buddy) struct subtype. + + ''' + def to_dict(self) -> dict: + return { + f: getattr(self, f) + for f in self.__struct_fields__ + } + + def __repr__(self): + return f'Struct({pformat(self.to_dict())})' + +