PATH_BOXES = 'boxes.txt'
PATH_COLLECTIONS = 'collections.txt'
+PATH_COLORS = 'colors.txt'
PATH_DESIGNS = 'designs.txt'
PATH_PIECES = 'pieces.txt'
CHAR_NEWLINE = '\n'
CHAR_SPACE = ' '
+CHAR_PLUS = '+'
+CHAR_MINUS = '-'
CHAR_COMMA = ','
CHAR_EQ = '='
CHAR_UNDER = '_'
'Build from file at path.'
+class Color(Textfiled):
+ 'Color incl. solidness/transparency field.'
+
+ def __init__(
+ self,
+ id_: str,
+ solid: bool,
+ wavelength: str
+ ) -> None:
+ self.id_ = id_
+ self.solid = solid
+ self.wavelength = wavelength
+
+ @classmethod
+ def from_textfile(
+ cls,
+ path: str
+ ) -> dict[str, Self]:
+ collected = {}
+ for id_, desc in [cls.tokify(line, 2) for line in cls.lines_of(path)]:
+ assert len(desc) > 1
+ assert desc[0] in {CHAR_PLUS, CHAR_MINUS}
+ collected[id_] = cls(id_, desc[0] == CHAR_PLUS, desc[1:])
+ return collected
+
+ def __str__(
+ self
+ ) -> str:
+ return (f'{self.id_:>3} '
+ f'{CHAR_PLUS if self.solid else CHAR_MINUS}{self.wavelength}')
+
+
class Design(Textfiled):
'Shape and texture configurations with descriptions and equalities.'
self,
pieces: dict[str, 'Piece'],
designs: dict[str, 'Design'],
- boxes: dict[str, 'Box']
+ boxes: dict[str, 'Box'],
+ colors: dict[str, 'Color']
) -> None:
'Print helper for collecting from boxes all pieces of self.'
lines = []
for count, piece_id, _ in self.piece_listings_flat():
piece = pieces[piece_id]
+ color = str(colors[piece.color_id]).split(CHAR_SPACE,
+ maxsplit=1)[1]
design = Design.possibly_by_alt(piece.design_id, designs)
assert design is not None
description = design.description
box = [b for b in boxes.values() if design.id_ in b.designs][0]
idx_in_box = box.designs.index(design.id_)
lines += [f'{box.id_:>2}:{idx_in_box:>2} '
- f'{count}× {piece.color_id:>3} {description}']
+ f'{count}× {color} {description}']
for line in sorted(lines):
print(line)
def check_consistencies_between_tables(
+ colors: dict[str, Color],
pieces: dict[str, Piece],
collections: dict[str, Collection],
designs: dict[str, Design],
break
assert pieces_found, design_id
+ # check all pieces' colors are recorded
+ for color_id in [v.color_id for v in pieces.values()]:
+ assert color_id in colors, color_id
+
def main(
) -> None:
+ colors = Color.from_textfile(PATH_COLORS)
pieces = Piece.from_textfile(PATH_PIECES)
collections = Collection.from_textfile(PATH_COLLECTIONS)
designs = Design.from_textfile(PATH_DESIGNS)
boxes = Box.from_textfile(PATH_BOXES)
- check_consistencies_between_tables(pieces, collections, designs, boxes)
- for title, items in (('PIECES', pieces),
+ check_consistencies_between_tables(colors, pieces, collections, designs,
+ boxes)
+ for title, items in (('COLORS', colors),
+ ('PIECES', pieces),
('DESIGNS', designs),
('BOXES', boxes),
('COLLECTIONS', collections)):