home · contact · privacy
Use former .print_packing_instructions for Collection.show. master
authorPlom Heller <plom@plomlompom.com>
Thu, 7 May 2026 12:26:34 +0000 (14:26 +0200)
committerPlom Heller <plom@plomlompom.com>
Thu, 7 May 2026 12:26:34 +0000 (14:26 +0200)
bricksplom.py

index 22399927e55e8c4c8fdc25bc3b2dbb92edc4dc1c..aa714490ea2e56208f95a9dffb64cbd74260cda8 100755 (executable)
@@ -185,7 +185,7 @@ class Design(Textfiled, Lookupable):
         alts: dict[str, set[str]] = {}
         for design_id, body in [cls.tokify(line, 2)
                                 for line in cls.lines_of(path)]:
-            assert design_id not in collected
+            assert design_id not in collected, design_id
             assert len(body) > 1
             char_type, body = body[0], body[1:]
             assert char_type in {CHAR_DESIGN_ALT, CHAR_DESIGN_DESC}
@@ -334,11 +334,6 @@ class Collection(Textfiled, WithDb, Lookupable):
                 + self._format_paginated(lambda count, p_id, comment:
                                          f' {count:2} {p_id:>7} {comment}'))
 
-    def show(
-            self
-            ) -> str:
-        return self.raw()
-
     def __str__(
             self
             ) -> str:
@@ -347,7 +342,7 @@ class Collection(Textfiled, WithDb, Lookupable):
     def searchable(
             self
             ) -> str:
-        return self.raw()
+        return self.show()
 
     def _format_paginated(
             self,
@@ -362,7 +357,7 @@ class Collection(Textfiled, WithDb, Lookupable):
                     lines += [' -']
                 for count, piece_id, comment in column:
                     lines += [format_line(count, piece_id, comment)]
-        return '\n'.join(lines) + '\n'
+        return '\n'.join(lines)
 
     def piece_listings_flat(self) -> tuple[PieceListing, ...]:
         'Flattened variant of .piece_listings, no division into pages/cols.'
@@ -372,30 +367,42 @@ class Collection(Textfiled, WithDb, Lookupable):
                 collected += list(column)
         return tuple(collected)
 
-    def print_packing_instructions(
+    def show(
             self
-            ) -> None:
-        'Print helper for putting into boxes all pieces of self.'
-        def format_line(_, piece_id, __) -> str:
+            ) -> str:
+        def format_line(count, piece_id, comment) -> str:
             assert self._db
-            box_listing, description = self._db.piece_to_box_listing(piece_id)
-            return f'{piece_id:>7} {box_listing} {description}'
-
-        print(self._format_paginated(format_line))
-
-    def print_unpacking_instructions(
-            self
-            ) -> None:
-        'Print helper for collecting from boxes all pieces of self.'
-        assert self._db
-        lines = []
-        for count, piece_id, _ in self.piece_listings_flat():
-            box_listing, description = self._db.piece_to_box_listing(piece_id)
+            piece = self._db.pieces[piece_id]
+            design_id = piece.design_id
+            tail_comment = f' # {comment}' if comment else ''
             color = str(self._db.colors[self._db.pieces[piece_id].color_id]
                         ).lstrip().split(CHAR_SEP_TOKEN, maxsplit=1)[1]
-            lines += [f'{box_listing} {count}× {color} {description}']
-        for line in sorted(lines):
-            print(line)
+            box: Optional[Box] = None
+            for i_box in self._db.boxes().values():
+                for idx_in_box in [
+                        idx for idx, d_to_ls
+                        in enumerate(i_box.designs_to_listings)
+                        if piece_id in [listing[1] for listing in d_to_ls[1]]]:
+                    box = i_box
+                    break
+                if box:
+                    break
+            if not box:
+                for i_box in self._db.boxes().values():
+                    for idx_in_box in [
+                            idx for idx, t
+                            in enumerate(i_box.designs_to_listings)
+                            if design_id in t[0].all_ids]:
+                        box = i_box
+                        break
+                    if box:
+                        break
+            box_listing = f'{box.id_:>2}:{idx_in_box:>2}' if box else '__:__'
+            return (f'{count:>2}× {piece_id:>7}:{design_id:>6} {box_listing} '
+                    f'{color} {self._db.designs[design_id].description}'
+                    f'{tail_comment}')
+
+        return f'{self}\n\n{self._format_paginated(format_line)}'
 
 
 class Box(WithDb, Lookupable):
@@ -417,7 +424,8 @@ class Box(WithDb, Lookupable):
             design = design.alternate_to or design
             if not (designed_listings and designed_listings[-1][0] == design):
                 designed_listings += [(design, []), ]
-            designed_listings[-1][1] += [listing]
+            target = designed_listings[-1][1]
+            target += [listing]
         self.designs_to_listings = tuple((d, tuple(ls))
                                          for d, ls in designed_listings)
 
@@ -536,33 +544,6 @@ class BricksDb:
         item = table[inquiry]
         return item.raw() if show_raw else item.show()
 
-    def piece_to_box_listing(
-            self,
-            piece_id: str,
-            ) -> tuple[str, str]:
-        'For Piece of piece_id, print position in .boxes() and description.'
-        design = self.designs[self.pieces[piece_id].design_id]
-        owning_box: Optional[Box] = None
-        for box in self.boxes().values():
-            for idx_in_box in [
-                    idx for idx, d_to_ls in enumerate(box.designs_to_listings)
-                    if piece_id in [listing[1] for listing in d_to_ls[1]]]:
-                owning_box = box
-                break
-            if owning_box:
-                break
-        if not owning_box:
-            for box in self.boxes().values():
-                for idx_in_box in [
-                        idx for idx, t in enumerate(box.designs_to_listings)
-                        if design.id_ in t[0].all_ids]:
-                    owning_box = box
-                    break
-                if owning_box:
-                    break
-        assert owning_box
-        return f'{box.id_:>2}:{idx_in_box:>2}', design.description
-
     def print(
             self
             ) -> None: