home
·
contact
·
privacy
projects
/
bricksplom
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
5802d74
)
Improve naming of syntax-relevant char variables.
master
author
Plom Heller
<plom@plomlompom.com>
Fri, 1 May 2026 18:11:11 +0000
(20:11 +0200)
committer
Plom Heller
<plom@plomlompom.com>
Fri, 1 May 2026 18:11:11 +0000
(20:11 +0200)
bricksplom.py
patch
|
blob
|
history
diff --git
a/bricksplom.py
b/bricksplom.py
index 58c846dfde27dfc61e690f016e37dd139b428631..31396ffdc7dda0fc7bee0d2cac281a4a035ac8e5 100755
(executable)
--- a/
bricksplom.py
+++ b/
bricksplom.py
@@
-12,20
+12,18
@@
PATH_DESIGNS = 'designs.txt'
PATH_PIECES = 'pieces.txt'
CHAR_NEWLINE = '\n'
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]
BOX_PREFIX = 'box:'
PieceListing = tuple[int, str, str]
@@
-59,8
+57,8
@@
class Textfiled(ABC):
if len(collected) == len_expected - 1:
tok = body
else:
if len(collected) == len_expected - 1:
tok = body
else:
- assert CHAR_S
PACE
in body, body
- tok, body = body.split(CHAR_S
PACE
, maxsplit=1)
+ assert CHAR_S
EP_TOKEN
in body, body
+ tok, body = body.split(CHAR_S
EP_TOKEN
, maxsplit=1)
collected += [tok]
return tuple(collected)
collected += [tok]
return tuple(collected)
@@
-75,6
+73,7
@@
class Textfiled(ABC):
class WithDb(ABC):
class WithDb(ABC):
+ 'Add db:Optional[BricksDB] field to __init__, setting ._db.'
def __init__(
self,
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
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} '
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):
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:]
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)
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
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]
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_S
PACE
, maxsplit=1)
+ color_id, comment = (toks[-1].split(CHAR_S
EP_TOKEN
, maxsplit=1)
+ [''])[:2]
collected[piece_id] = cls(piece_id, design_id, color_id, comment)
return collected
+ [''])[: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):
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
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
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] += [[]]
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()
i_listings += [[[]]]
else:
count, remainder = cls.tokify(line, 2)
assert count.isdigit()
- id_, comment = (remainder.split(CHAR_S
PACE
, maxsplit=1)
+ id_, comment = (remainder.split(CHAR_S
EP_TOKEN
, maxsplit=1)
+ [''])[:2]
assert len(id_) > 0
i_listings[-1][-1] += [(int(count), id_, comment)]
+ [''])[: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:
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}'))
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]
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_S
PACE
, maxsplit=1)[1]
+ ).lstrip().split(CHAR_S
EP_TOKEN
, maxsplit=1)[1]
lines += [f'{box_listing} {count}× {color} {description}']
for line in sorted(lines):
print(line)
lines += [f'{box_listing} {count}× {color} {description}']
for line in sorted(lines):
print(line)