jacinle.utils.matching#
Functions to match names using glob patterns.
Classes
A name matcher based on two sets of glob patterns: one for inclusion and one for exclusion. |
|
A name matcher based on a set of glob patterns. |
Class IENameMatcher
- class IENameMatcher[source]#
Bases:
objectA name matcher based on two sets of glob patterns: one for inclusion and one for exclusion.
When
includeis None,excludeis not None, the matcher will match all names that are not excluded.When
includeis not None,excludeis None, the matcher will match all names that are included.- When
includeis not None,excludeis not None, the matcher will match all names that are included and not excluded. The
excluderule set has higher priority than theincluderule set.
- When
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']
- __new__(**kwargs)#
- end()[source]#
End a matching session, which returns a tuple of
(stat_type, things)See the docstring ofIENameMatcher.
Class NameMatcher
- class NameMatcher[source]#
Bases:
objectA 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'}
- __new__(**kwargs)#
- end()[source]#
End a matching session, which returns a tuple of (matched values, unmatched patterns). See the docstring of
NameMatcherfor more details.
- insert_rule(index, rule)[source]#
Insert a rule to the rule set at a given position (priority). The rule is a (pattern, value) pair.
- match(k)[source]#
Match a name against the rule set. Return the value if matched, otherwise return None.