filefinder.matches#

Matches management.

Functions

get_groups_indices(groups, key)

Get sorted list of groups indices corresponding to key.

Classes

Match(group, match, idx)

Match extract from a filename.

Matches(match, groups)

Scan an input file and store the results.

class Match(group, match, idx)#

Match extract from a filename.

Parameters:
  • group (Group) – Group used to get this match.

  • match (Match) – Match object for the complete filename.

  • idx (int) – Index of the group in the match object.

group#

Group used to get this match.

match_str#

String matched in the filename.

start#

Start index of match in the filename.

end#

End index of match in the filename.

match_parsed#

Parsed value. None if parsing was not successful.

get_match(parse=True)#

Get match string or value.

Parameters:

parse (bool) – If True (default), and the parsing was successful, return the parsed value instead of the matched string.

Raises:

ValueError – Could not parse the match.:

Return type:

str | Any

class Matches(match, groups)#

Scan an input file and store the results.

Parameters:
  • match (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.

classmethod from_filename(filename, pattern, groups)#

Find matches for a given filename.

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

  • pattern (Pattern | str) – Compiled match pattern to use. If left to None, we generate the current regex.

  • 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_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[filefinder.matches.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:

str | 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[str | Any]

groups#

Groups used.

matches: list[filefinder.matches.Match]#

Matches for a single filename.

get_groups_indices(groups, key)#

Get sorted list of groups indices corresponding to key.

Key can be an integer index, or a string of a group name. Since multiple groups can share the same name, multiple indices can be returned (sorted).

Raises:
Parameters:
Return type:

list[int]