From: Plom Heller Date: Tue, 4 Aug 2026 22:18:12 +0000 (+0200) Subject: Minor code improvements. X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/booking/%7B%7Btodo.comment%7D%7D?a=commitdiff_plain;ds=sidebyside;p=bricksplom Minor code improvements. --- diff --git a/src/bricksplom/ldraw.py b/src/bricksplom/ldraw.py index e899381..29432ce 100644 --- a/src/bricksplom/ldraw.py +++ b/src/bricksplom/ldraw.py @@ -23,6 +23,9 @@ LdrawRefsTree = tuple[Path, '_SubRefs'] _SubRefs = dict[str, LdrawRefsTree] +_N_TOKS_PER_REFLINE = 15 + + class LdrawDb: 'Connection to local LDRAW archive.' @@ -46,19 +49,20 @@ class LdrawDb: def _walk_tree( self, filename: str, - collect: Callable[[Callable[[str], Any], str, list[str]], Any], + collect: Callable[[Callable[[], Any], str, list[str]], Any], result: Callable[[Path, list[Any]], Any], ) -> Any: path = self.path(filename) collected = [] for line in path.read_text(encoding='utf8').split(CHAR_NEWLINE): - toks = line.lstrip().split(maxsplit=14) + toks = line.lstrip().split(maxsplit=_N_TOKS_PER_REFLINE - 1) if not (toks and toks[0] == '1'): continue - assert len(toks) == 15 + assert len(toks) == _N_TOKS_PER_REFLINE + referenced = toks.pop().replace('\\', '/') collected += [ - collect(lambda ref: self._walk_tree(ref, collect, result), - toks.pop().replace('\\', '/'), + collect(lambda: self._walk_tree(referenced, collect, result), + referenced, toks) ] return result(path, collected) @@ -70,7 +74,7 @@ class LdrawDb: 'Produce LdrawRefsTree for filename.' return self._walk_tree( filename, - collect=lambda walker, ref, _: (ref, walker(ref)), + collect=lambda walk, ref, _: (ref, walk()), result=lambda path, coll: (path, {t[0]: t[1] for t in coll})) def count_studs( @@ -79,7 +83,7 @@ class LdrawDb: ) -> int: 'On item at {._root}/p(art)/{filename} count studs.' def count_studs( - walker: Callable[[str], int], + walk: Callable[[], int], ref: str, toks: list[str] ) -> int: @@ -96,8 +100,7 @@ class LdrawDb: if jdx != idx and row[jdx] != 0]: do_ignore = True break - return (int((not do_ignore) and ref in LDRAW_STUDS) - or walker(ref)) + return int((not do_ignore) and ref in LDRAW_STUDS) or walk() return self._walk_tree(filename, collect=count_studs,