Coverage for tarjinja/expand.py: 92%
18 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-05 14:24 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-05 14:24 +0000
1import ast
2import itertools
3import re
4from collections.abc import Generator
7def list_expand(s: str) -> Generator[str, None, None]:
8 """
9 >>> list(list_expand("abc [1,2,3] def ['abc', 'def'] xyz")) # doctest:+ELLIPSIS
10 ['abc 1 def abc xyz', 'abc 1 def def xyz', ..., 'abc 3 def def xyz']
11 """
12 args = []
13 cur = 0
14 for i in re.finditer("\\[[^\\[\\]]*\\]", s):
15 st, en = i.span()
16 val = ast.literal_eval(s[st:en])
17 if cur < st: 17 ↛ 19line 17 didn't jump to line 19 because the condition on line 17 was always true
18 args.append([s[cur:st]])
19 args.append(val)
20 cur = en
21 if cur < len(s): 21 ↛ 24line 21 didn't jump to line 24 because the condition on line 21 was always true
22 args.append([s[cur:]])
24 for v in itertools.product(*args):
25 yield "".join(map(str, v))