From: Plom Heller Date: Mon, 4 May 2026 12:59:52 +0000 (+0200) Subject: Improve expressiveness of Piece.__str__(). X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%22https:/validator.w3.org/bar%20baz.html?a=commitdiff_plain;h=b59c1cb86f64e0f6be55e6fc917dfbd83ae7feea;p=bricksplom Improve expressiveness of Piece.__str__(). --- diff --git a/bricksplom.py b/bricksplom.py index c0ea7e1..0806ec4 100755 --- a/bricksplom.py +++ b/bricksplom.py @@ -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))