From d54830ca0668d0fc097d3465d5c410508aafdf32 Mon Sep 17 00:00:00 2001 From: Plom Heller Date: Fri, 1 May 2026 20:11:11 +0200 Subject: [PATCH] Improve naming of syntax-relevant char variables. --- bricksplom.py | 68 ++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/bricksplom.py b/bricksplom.py index 58c846d..31396ff 100755 --- a/bricksplom.py +++ b/bricksplom.py @@ -12,20 +12,18 @@ PATH_DESIGNS = 'designs.txt' PATH_PIECES = 'pieces.txt' CHAR_NEWLINE = '\n' -CHAR_SPACE = ' ' -CHAR_PLUS = '+' -CHAR_MINUS = '-' -CHAR_COMMA = ',' -CHAR_EQ = '=' -CHAR_UNDER = '_' -CHAR_HASH = '#' -CHAR_IN = '+' -CHAR_OUT = '-' -CHAR_INACTIVE = CHAR_HASH -CHAR_COMMENT = CHAR_HASH -CHAR_SEPARATOR_COLUMN = CHAR_MINUS -CHAR_SEPARATOR_PAGE = CHAR_EQ - +CHAR_SEP_TOKEN = ' ' +CHAR_COMMENT = '#' +CHAR_DESIGN_ALT = '=' +CHAR_DESIGN_DESC = '_' +CHAR_COLL_INDENT = ' ' +CHAR_COLL_IN = '+' +CHAR_COLL_OUT = '-' +CHAR_COLL_INACTIVE = '#' +CHAR_COLL_SEP_COLUMN = '-' +CHAR_COLL_SEP_PAGE = '=' +CHAR_COL_SOLID = '+' +CHAR_COL_TRANSPARENT = '-' BOX_PREFIX = 'box:' PieceListing = tuple[int, str, str] @@ -59,8 +57,8 @@ class Textfiled(ABC): if len(collected) == len_expected - 1: tok = body else: - assert CHAR_SPACE in body, body - tok, body = body.split(CHAR_SPACE, maxsplit=1) + assert CHAR_SEP_TOKEN in body, body + tok, body = body.split(CHAR_SEP_TOKEN, maxsplit=1) collected += [tok] return tuple(collected) @@ -75,6 +73,7 @@ class Textfiled(ABC): class WithDb(ABC): + 'Add db:Optional[BricksDB] field to __init__, setting ._db.' def __init__( self, @@ -107,15 +106,16 @@ class Color(Textfiled): 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:]) + assert desc[0] in {CHAR_COL_SOLID, CHAR_COL_TRANSPARENT} + collected[id_] = cls(id_, desc[0] == CHAR_COL_SOLID, desc[1:]) return collected def __str__( self ) -> str: return (f'{self.id_:>3} ' - f'{CHAR_PLUS if self.solid else CHAR_MINUS}{self.wavelength}') + + (CHAR_COL_SOLID if self.solid else CHAR_COL_TRANSPARENT) + + self.wavelength) class Design(Textfiled): @@ -142,11 +142,11 @@ class Design(Textfiled): for line in cls.lines_of(path)]: assert len(body) > 1 char_type, body = body[0], body[1:] - assert char_type in {CHAR_EQ, CHAR_UNDER} - if char_type == CHAR_EQ: + assert char_type in {CHAR_DESIGN_ALT, CHAR_DESIGN_DESC} + if char_type == CHAR_DESIGN_ALT: alts[body] = alts.get(body, set()) alts[body].add(design_id) - else: # == CHAR_UNDER + else: # == CHAR_DESIGN_DESC collected[design_id] = cls(design_id, body) for id_, alternatives in alts.items(): collected[id_].alternates = alternatives @@ -197,7 +197,7 @@ class Piece(Textfiled): collected = {} for toks in [cls.tokify(line, 3) for line in cls.lines_of(path)]: piece_id, design_id = toks[:2] - color_id, comment = (toks[-1].split(CHAR_SPACE, maxsplit=1) + color_id, comment = (toks[-1].split(CHAR_SEP_TOKEN, maxsplit=1) + [''])[:2] collected[piece_id] = cls(piece_id, design_id, color_id, comment) return collected @@ -239,22 +239,24 @@ class Collection(Textfiled, WithDb): collected = {} i_listings: list[list[list[PieceListing]]] = [[[]]] for line in cls.lines_of(path): - if not line.startswith(CHAR_SPACE): + if not line.startswith(CHAR_COLL_INDENT): id_, metadata = cls.tokify(line, 2) assert metadata - assert metadata[0] in {CHAR_IN, CHAR_OUT, CHAR_INACTIVE} - is_in = (None if metadata[0] == CHAR_INACTIVE - else metadata[0] == CHAR_IN) + assert metadata[0] in {CHAR_COLL_IN, + CHAR_COLL_OUT, + CHAR_COLL_INACTIVE} + is_in = (None if metadata[0] == CHAR_COLL_INACTIVE + else metadata[0] == CHAR_COLL_IN) i_listings = [[[]]] collected[id_] = is_in, metadata[1:], i_listings - elif line[1:2] == CHAR_SEPARATOR_COLUMN: + elif line[1:2] == CHAR_COLL_SEP_COLUMN: i_listings[-1] += [[]] - elif line[1:2] == CHAR_SEPARATOR_PAGE: + elif line[1:2] == CHAR_COLL_SEP_PAGE: i_listings += [[[]]] else: count, remainder = cls.tokify(line, 2) assert count.isdigit() - id_, comment = (remainder.split(CHAR_SPACE, maxsplit=1) + id_, comment = (remainder.split(CHAR_SEP_TOKEN, maxsplit=1) + [''])[:2] assert len(id_) > 0 i_listings[-1][-1] += [(int(count), id_, comment)] @@ -270,8 +272,8 @@ class Collection(Textfiled, WithDb): def __str__( self ) -> str: - is_in_str = (CHAR_INACTIVE if self.is_in is None - else (CHAR_IN if self.is_in else CHAR_OUT)) + is_in_str = (CHAR_COLL_INACTIVE if self.is_in is None + else (CHAR_COLL_IN if self.is_in else CHAR_COLL_OUT)) return (f'\n{self.id_} {is_in_str}{self.description}\n' + self._format_paginated(lambda count, p_id, comment: f' {count:2} {p_id:>7} {comment}')) @@ -319,7 +321,7 @@ class Collection(Textfiled, WithDb): for count, piece_id, _ in self.piece_listings_flat(): box_listing, description = self._db.piece_to_box_listing(piece_id) color = str(self._db.colors[self._db.pieces[piece_id].color_id] - ).lstrip().split(CHAR_SPACE, maxsplit=1)[1] + ).lstrip().split(CHAR_SEP_TOKEN, maxsplit=1)[1] lines += [f'{box_listing} {count}× {color} {description}'] for line in sorted(lines): print(line) -- 2.30.2