home · contact · privacy
Improve expressiveness of Piece.__str__().
authorPlom Heller <plom@plomlompom.com>
Mon, 4 May 2026 12:59:52 +0000 (14:59 +0200)
committerPlom Heller <plom@plomlompom.com>
Mon, 4 May 2026 12:59:52 +0000 (14:59 +0200)
bricksplom.py

index c0ea7e133e4f46dd1791b1ab8978a061ecb5c608..0806ec440bd92da904f7f59a5dd83f8d59ee21e8 100755 (executable)
@@ -202,7 +202,7 @@ class Design(Textfiled, Lookupable):
         return None
 
 
-class Piece(Textfiled, Lookupable):
+class Piece(Textfiled, WithDb, Lookupable):
     'Individual configuration of design and color.'
 
     def __init__(
@@ -210,17 +210,20 @@ class Piece(Textfiled, Lookupable):
             id_: str,
             design_id: str,
             color_id: str,
-            comment: str
+            comment: str,
+            **kwargs
             ) -> None:
         self.id_ = id_
         self.design_id = design_id
         self.color_id = color_id
         self.comment = comment
+        super().__init__(**kwargs)
 
     @classmethod
     def from_textfile(
             cls,
             path: tuple[str, str],
+            db: Optional['BricksDb'] = None,
             **_
             ) -> dict[str, Self]:
         collected = {}
@@ -229,7 +232,8 @@ class Piece(Textfiled, Lookupable):
             assert piece_id not in collected
             color_id, comment = (toks[-1].split(CHAR_SEP_TOKEN, maxsplit=1)
                                  + [''])[:2]
-            collected[piece_id] = cls(piece_id, design_id, color_id, comment)
+            collected[piece_id] = cls(piece_id, design_id, color_id, comment,
+                                      db=db)
         return collected
 
     def raw(
@@ -238,6 +242,15 @@ class Piece(Textfiled, Lookupable):
         return (f'{self.id_:>7} {self.design_id:>6} '
                 f'{self.color_id:>3} {self.comment}').rstrip()
 
+    def __str__(
+            self
+            ) -> str:
+        assert self._db
+        design = Design.possibly_by_alt(self.design_id, self._db.designs)
+        color = self._db.colors[self.color_id]
+        comment = f' # {self.comment}' if self.comment else ''
+        return f'{self.id_:>7} {self.design_id:>6} {design.description} ({str(color).strip()}){comment}'
+
 
 class Collection(Textfiled, WithDb, Lookupable):
     'Named collection of pieces in order of pages of columns of counts.'
@@ -432,7 +445,7 @@ class BricksDb:
             path_tables: str
             ) -> None:
         self.colors = Color.from_textfile((path_tables, PATH_COLORS))
-        self.pieces = Piece.from_textfile((path_tables, PATH_PIECES))
+        self.pieces = Piece.from_textfile((path_tables, PATH_PIECES), db=self)
         self.collections = Collection.from_textfile(
                 (path_tables, PATH_COLLECTIONS), db=self)
         self.designs = Design.from_textfile((path_tables, PATH_DESIGNS))