From: Dmitry Selyutin Date: Sat, 10 Jun 2023 18:08:49 +0000 (+0300) Subject: insndb/core: introduce Tuple type X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1baf42a30ee56665127cc8bc272236360ccc8ac2;p=openpower-isa.git insndb/core: introduce Tuple type --- diff --git a/src/openpower/insndb/core.py b/src/openpower/insndb/core.py index 68fdfe20..2cd7d5fb 100644 --- a/src/openpower/insndb/core.py +++ b/src/openpower/insndb/core.py @@ -98,6 +98,24 @@ class Dataclass(Node, metaclass=DataclassMeta): yield from filter(match, map(field, _dataclasses.fields(clsself))) +class Tuple(Node, tuple): + def __init_subclass__(cls, datatype): + cls.__datatype = datatype + return super().__init_subclass__() + + @walkmethod + def walk(clsself, match=None): + if match is None: + match = lambda subnode: True + + if isinstance(clsself, type): + yield clsself.__datatype + else: + for item in clsself: + if match(item): + yield item + + class VisitorMethod: def __init__(self, nodecls, method): self.__nodecls = nodecls @@ -3779,22 +3797,10 @@ class SVP64Database: return None -class Records(tuple): +class Records(Tuple, datatype=Record): def __new__(cls, records): return super().__new__(cls, sorted(records)) - @walkmethod - def walk(clsself, match=None): - if match is None: - match = lambda subnode: True - - if isinstance(clsself, type): - yield Record - else: - for record in clsself: - if match(record): - yield record - class Database(Node): def __init__(self, root):