insndb/core: introduce Tuple type
authorDmitry Selyutin <ghostmansd@gmail.com>
Sat, 10 Jun 2023 18:08:49 +0000 (21:08 +0300)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 22 Dec 2023 19:26:19 +0000 (19:26 +0000)
src/openpower/insndb/core.py

index 68fdfe20e8a30eb780ae2761ae41eb99326838fa..2cd7d5fbaebfb2698954589242af70924f341a5a 100644 (file)
@@ -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):