jacinle.utils.matching#

Functions to match names using glob patterns.

Classes

IENameMatcher

A name matcher based on two sets of glob patterns: one for inclusion and one for exclusion.

NameMatcher

A name matcher based on a set of glob patterns.

Class IENameMatcher

class IENameMatcher[source]#

Bases: object

A name matcher based on two sets of glob patterns: one for inclusion and one for exclusion.

  • When include is None, exclude is not None, the matcher will match all names that are not excluded.

  • When include is not None, exclude is None, the matcher will match all names that are included.

  • When include is not None, exclude is not None, the matcher will match all names that are included and not excluded.

    The exclude rule set has higher priority than the include rule set.

Example

matcher = IENameMatcher(include=['*.jpg', '*.png'], exclude=['*.bak.png'])
with matcher:
    matcher.match('a.jpg')  # True
    matcher.match('a.png')  # True
    matcher.match('a.bak.png')  # False
    matcher.match('a.txt')  # False
    matcher.match('a.bak.txt')  # False

stat_type, things = matcher.get_last_stat()
print(stat_type)  # 'exclude'
# Everything that has been rejected.
print(things)  # ['a.bak.png', 'a.txt', 'a.bak.txt']
__init__(include, exclude)[source]#

Initialize the name matcher.

Parameters:
  • include (Iterable[str] | None) – a list of glob patterns for inclusion.

  • exclude (Iterable[str] | None) – a list of glob patterns for exclusion.

__new__(**kwargs)#
begin()[source]#

Begin a matching session.

end()[source]#

End a matching session, which returns a tuple of (stat_type, things) See the docstring of IENameMatcher.

get_last_stat()[source]#

Get the last matching session’s result.

match(k)[source]#

Match a name against the rule set. Return True if matched, otherwise return False.

Parameters:

k (str)

Return type:

bool

Class NameMatcher

class NameMatcher[source]#

Bases: object

A name matcher based on a set of glob patterns.

The rule set is a list of (pattern, value) pairs. The pattern is a glob pattern, and the value is the value to be returned when the pattern matches.

Example

matcher = NameMatcher({'*.jpg': 'image', '*.png': 'image', '*.txt': 'text'})
with matcher:
    matcher.match('a.jpg')  # 'image'
    matcher.match('a.png')  # 'image'

matched, unused = matcher.get_last_stat()  # Return a tuple of (matched values, unmatched patterns).
print(matched)  # [('a.jpg', '*.jpg', 'image'), ('a.png', '*.png', 'image')]
print(unused)  # {'*.txt'}
__init__(rules=None)[source]#

Initialize the name matcher.

Parameters:

rules (Iterable[Tuple[str, Any]] | Dict[str, Any] | None) – A list of (pattern, value) pairs, or a dict of {pattern: value}.

__new__(**kwargs)#
append_rule(rule)[source]#

Append a rule to the rule set. The rule is a (pattern, value) pair.

Parameters:

rule (Tuple[str, Any]) – the rule to be appended.

begin(*, force_compile=False)[source]#

Begin a matching session.

compile()[source]#

Compile the rule set.

end()[source]#

End a matching session, which returns a tuple of (matched values, unmatched patterns). See the docstring of NameMatcher for more details.

get_last_stat()[source]#

Get the last matching session’s result.

insert_rule(index, rule)[source]#

Insert a rule to the rule set at a given position (priority). The rule is a (pattern, value) pair.

Parameters:
map()[source]#

Get the map of {pattern: value}.

Return type:

Dict[str, Any]

match(k)[source]#

Match a name against the rule set. Return the value if matched, otherwise return None.

Parameters:

k (str) – the name to be matched.

Returns:

The value if matched, otherwise None.

Return type:

Any | None

pop_rule(index=None)[source]#

Pop a rule from the rule set.

Parameters:

index – the index of the rule to be popped. If None, the last rule will be popped.

property rules: List[Tuple[str, Any]]#

Get the rules.