home · contact · privacy
Minor refactoring.
authorPlom Heller <plom@plomlompom.com>
Tue, 21 Apr 2026 23:08:46 +0000 (01:08 +0200)
committerPlom Heller <plom@plomlompom.com>
Tue, 21 Apr 2026 23:08:46 +0000 (01:08 +0200)
bricksplom.py

index 9d87abbd99ccbf267be223ad7cac5d2f562b90e3..ddaaf930dd02933f35f5d0314d51f8994a4830b4 100755 (executable)
@@ -2,7 +2,7 @@
 'Data structures for managing/sorting bricks of a certain kind.'
 from abc import ABC, abstractmethod
 from pathlib import Path
-from typing import Self
+from typing import Optional, Self
 
 PATH_BOXES = 'boxes.txt'
 PATH_COLLECTIONS = 'collections.txt'
@@ -95,10 +95,26 @@ class Design(Textfiled):
             collected[id_].alternates = alternatives
         return collected
 
-    def __str__(self) -> str:
+    def __str__(
+            self
+            ) -> str:
         return '\n'.join([f'{self.id_:>6} _{self.description}']
                          + [f'{a:>6} ={self.id_}' for a in self.alternates])
 
+    @classmethod
+    def possibly_by_alt(
+            cls,
+            id_queried: str,
+            designs: dict[str, Self]
+            ) -> Optional[Self]:
+        'In designs find one of id_queried, by way of alternates if necessary.'
+        if id_queried in designs:
+            return designs[id_queried]
+        for design in designs.values():
+            if id_queried in design.alternates:
+                return design
+        return None
+
 
 class Piece(Textfiled):
     'Individual configuration of design and color.'
@@ -128,7 +144,9 @@ class Piece(Textfiled):
             collected[piece_id] = cls(piece_id, design_id, color_id, comment)
         return collected
 
-    def __str__(self) -> str:
+    def __str__(
+            self
+            ) -> str:
         return (f'{self.id_:>7} {self.design_id:>6} '
                 f'{self.color_id:>3} {self.comment}').rstrip()
 
@@ -183,7 +201,9 @@ class Collection(Textfiled):
                 collected += [p_id for _, p_id, _ in column]
         return tuple(sorted(collected))
 
-    def __str__(self) -> str:
+    def __str__(
+            self
+            ) -> str:
         pages = []
         for page in self.piece_listings:
             columns = []
@@ -216,7 +236,9 @@ class Box(Textfiled):
                 for id_, order in [cls.tokify(line, 2)
                                    for line in cls.lines_of(path)]}
 
-    def __str__(self) -> str:
+    def __str__(
+            self
+            ) -> str:
         return f'{self.id_:>2} {",".join(self.designs)}'
 
 
@@ -248,12 +270,7 @@ def check_consistencies_between_tables(
 
     # check all pieces' designs recorded in designs
     for design_id in [piece.design_id for piece in pieces.values()]:
-        if design_id not in designs:
-            for replacement in [k for k, v in designs.items()
-                                if design_id in v.alternates]:
-                design_id = replacement
-                break
-        assert design_id in designs, design_id
+        assert Design.possibly_by_alt(design_id, designs) is not None
 
     # check all recorded designs have matching pieces (at least via alts)
     for design_id, alts in [(k, v.alternates) for k, v in designs.items()]: