Matches#

class Matches(matches, groups)#

Bases: object

Scan an input file and store the results.

Parameters:
  • match – Match object obtained from a filename. It should have as much capturing groups as the pattern.

  • groups (Sequence[Group]) – Sequence of Groups objects present in the pattern.

  • matches (Sequence[Match])

classmethod from_filename(filename, pattern, groups)#

Find matches for a given filename.

Parameters:
  • filename (str) – Filename to retrieve matches from.

  • pattern (Pattern | str) – Match pattern to use, compiled or not.

  • groups (Sequence[Group])

Returns:

matches – A Matches object, or None if the filename did not match.

Raises:

IndexError – Not as many matches as groups. Maybe one of the group regex contains an additional (unwanted) capturing group ?

Return type:

Matches | None

get_date(default_date=None)#

Retrieve date from matched elements.

Matches that can be used are : YBmdjHMSFxX. If a matcher is not found in the filename, it will be replaced by the element of the default date argument. All values deduced from these matches will be compared. If different matchers give different values (for instance the group Y and F give a different year), an exception will be raised.

Parameters:

default_date (datetime | Mapping[str, int] | None) – Default date. Datetime, or a mapping with keys in: year, month, day, hour, minute, and second. Defaults to 1970-01-01 00:00:00

Return type:

datetime

get_matches(key, keep_discard=False)#

Get Match objects corresponding to key.

Parameters:
  • key (int | str) – Group(s) to select, either by index or name.

  • keep_discard (bool) – If true groups with the ‘discard’ option are kept. Defauult is false.

Returns:

List of Match corresponding to the key.

Return type:

list[Match]

get_value(key, parse=True, keep_discard=False)#

Get matched value corresponding to key.

Return a single value. If multiple groups correspond to key, the value of the first one to appear in the pattern is returned.

Parameters:
  • key (int | str) – Group(s) to select, either by index or name.

  • parse (bool) – If True (default), return the parsed value. If False return the matched string.

  • keep_discard (bool) – If true groups with the ‘discard’ option are kept. Defauult is false.

Raises:

KeyError – No group with no ‘discard’ option was found.

Return type:

Any

get_values(key, parse=True, keep_discard=False)#

Get matched values corresponding to key.

Return a list of values, even if only one group is selected.

Parameters:
  • key (int | str) – Group(s) to select, either by index or name.

  • parse (bool) – If True (default), return the parsed value. If False return the matched string.

  • keep_discard (bool) – If true groups with the ‘discard’ option are kept. Defauult is false.

Return type:

list[Any]

groups: list[Group]#

Groups used.

matches: list[Match]#

Matches for a single filename.