From 1baf42a30ee56665127cc8bc272236360ccc8ac2 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Sat, 10 Jun 2023 21:08:49 +0300 Subject: [PATCH] insndb/core: introduce Tuple type --- src/openpower/insndb/core.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) 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): -- 2.30.2