ningen.capture module¶
Capture parts from strings using a convenient glob-like syntax.
- ningen.capture.captures(pattern: str, values: Union[str, Sequence[Optional[str]]], *, must_match: bool = False, name: str = 'path') List[Capture][source]¶
Given a capture
pattern, return all theCaptureresults of applying it to each of the (sorted, unique, non-None)values. Ifmust_match, all the values must match the pattern. Otherwise, only captures of matching values are returned.By default, the complete matched string is made available in a data member
path(as this is typically used to parse disk file paths). You can override this by specifying a differentname.See
Capturefor the description of the capture pattern.
- ningen.capture.globs(pattern: str) List[Capture][source]¶
Given a capture
pattern, return all theCaptureresults of applying it to the results of aglobof the equivalent pattern.See
Capturefor the description of the capture pattern.
- class ningen.capture.Capture(**kwargs: str)[source]¶
Bases:
objectCapture the results of successfully matching a string (typically, a path name) with a capture pattern.
A capture pattern is similar to a
globpattern. However, all wildcards must be specified inside{...}as follows:You need to escape the
{character as{{and the}character as}}.{*name}has the same effect as*. The matching substring will be captured using thekey
name. For example,foo.{*suffix}will capture the file suffix.
If
namestarts with_then the matching substring will be discarded instead of being captured. For example, if you don’t want to capture the suffix, writefoo.{*_}instead offoo.{*suffix}.If
nameis followed by:, it must be followed by a glob pattern. That is,{*name}is a shorthand for{*name:*}. For examplefoo.{*suffix:[0-9]}will capture a single decimal digit suffix.{**name}is shorthand for{*name:**}. In this case you may not use:to specify a glob pattern. For example,foo/{**dir}/barwill capture all the (possibly empty) paths fromfooto nestedbarfiles.
Note
Use
/{**name}/instead of/{*name:**}/as the shorthand is given special treatment allow for capturing an empty sub-directory (that is, match a single/).The captured named values are available as members of the object, that is, write
capture.footo access the value of a captured{*foo}.