ningen.loop module¶
Execute code with captured values.
- ningen.loop.foreach(pattern: Optional[Union[str, Sequence[Optional[str]]]] = None, **kwargs: Union[str, Sequence[Optional[str]]]) Iterator[Optional[Capture]][source]¶
Execute code with captured values.
The
patternshould be a capture pattern (seeningen.capture.Capturefor details) or a list of such patterns (which presumably capture the same set of named parts of the file name). Theforeachcode will loop on the (sorted, unique, non-None) such patterns.Additional named parameters (
kwargs) are supported. If one or more of these has a list of values, then a capture will be generated for every (sorted, unique, non-None) combination of the values for each of the captured paths, or just once if nopatternwas specified.For example:
for c in foreach("foo/{*name}.cc"): assert c.path == f"foo/{name}.cc" print(f"foo/{c.name}.cc exists on disk") for c in foreach("foo/{*name}.cc", mode=["debug", "release"], compiler=["gcc", "clang"]): print(f"compile foo/{c.name}.cc using the {c.compiler} compiler in {c.mode} mode") for c in foreach(mode=["debug", "release"], compiler=["gcc", "clang"]): print(f"final link step using the {c.compiler} compiler in {c.mode} mode")
- ningen.loop.expand(template: Union[str, Sequence[Optional[str]]], **kwargs: Union[str, Sequence[Optional[str]]]) List[str][source]¶
Generate multiple formatted strings using all the combinations of the provided named values (
kwargs).The
templateshould be a normal Python format (using{name}). If this is a list, then the result will contain the strings generated from all the (sorted, unique, non-None) templates in the list.Additional named parameters (
kwargs) are expected. If one or more of these has a list of values, then a string will be generated for every (sorted, unique, non-None) combination of the valuesFor example:
assert expand('src/{name}.cc', name='foo') == ['src/foo.cc'] assert expand('src/{name}.cc', name=['foo', 'bar']) == ['src/foo.cc', 'src/bar.cc'] assert expand('obj/{mode}/{name}.o', name=['foo', 'bar'], mode=['debug', 'release']) == ['obj/debug/foo.o', 'obj/debug/bar.o', 'obj/release/foo.o', 'obj/release/bar.o']