younotyou.younotyou
1import fnmatch 2from typing import Iterable 3 4 5def younotyou( 6 candidates: Iterable[str], 7 include_patterns: list[str] = ["*"], 8 exclude_patterns: list[str] = [], 9 case_sensitive: bool = True, 10) -> list[str]: 11 """Returns a list of strings that match any pattern in `include_patterns`, but don't match any pattern in `exclude_patterns`. 12 13 Patterns can be literals or glob style wildcard strings. 14 15 Exclusion patterns override include patterns, 16 i.e. if an item matches an include pattern but also matches an exclude pattern, it will be excluded. 17 >>> strings = ["thispattern", "aPaTtErN", "mypatterns"] 18 >>> younotyou(strings, ["*pattern"]) 19 >>> ['thispattern'] 20 >>> younotyou(strings, ["*pattern*"]) 21 >>> ['thispattern', 'mypatterns'] 22 >>> younotyou(strings, ["*pattern*"], case_sensitive=False) 23 >>> ['thispattern', 'aPaTtErN', 'mypatterns'] 24 >>> younotyou(strings, ["*pattern*"], ["my*", "*is*"], case_sensitive=False) 25 >>> ['aPaTtErN'] 26 >>> younotyou(strings, exclude_patterns=["*PaT*"]) 27 >>> ['thispattern', 'mypatterns'] 28 >>> younotyou(strings, exclude_patterns=["*PaT*"], case_sensitive=False) 29 >>> [] 30 >>> younotyou(strings, include_patterns=["*PaT*"], exclude_patterns=["*PaT*"], case_sensitive=False) 31 >>> [] 32 """ 33 matcher = fnmatch.fnmatchcase if case_sensitive else fnmatch.fnmatch 34 return [ 35 candidate 36 for candidate in candidates 37 if any(matcher(candidate, pattern) for pattern in include_patterns) 38 and all(not matcher(candidate, pattern) for pattern in exclude_patterns) 39 ]
def
younotyou( candidates: Iterable[str], include_patterns: list[str] = ['*'], exclude_patterns: list[str] = [], case_sensitive: bool = True) -> list[str]:
6def younotyou( 7 candidates: Iterable[str], 8 include_patterns: list[str] = ["*"], 9 exclude_patterns: list[str] = [], 10 case_sensitive: bool = True, 11) -> list[str]: 12 """Returns a list of strings that match any pattern in `include_patterns`, but don't match any pattern in `exclude_patterns`. 13 14 Patterns can be literals or glob style wildcard strings. 15 16 Exclusion patterns override include patterns, 17 i.e. if an item matches an include pattern but also matches an exclude pattern, it will be excluded. 18 >>> strings = ["thispattern", "aPaTtErN", "mypatterns"] 19 >>> younotyou(strings, ["*pattern"]) 20 >>> ['thispattern'] 21 >>> younotyou(strings, ["*pattern*"]) 22 >>> ['thispattern', 'mypatterns'] 23 >>> younotyou(strings, ["*pattern*"], case_sensitive=False) 24 >>> ['thispattern', 'aPaTtErN', 'mypatterns'] 25 >>> younotyou(strings, ["*pattern*"], ["my*", "*is*"], case_sensitive=False) 26 >>> ['aPaTtErN'] 27 >>> younotyou(strings, exclude_patterns=["*PaT*"]) 28 >>> ['thispattern', 'mypatterns'] 29 >>> younotyou(strings, exclude_patterns=["*PaT*"], case_sensitive=False) 30 >>> [] 31 >>> younotyou(strings, include_patterns=["*PaT*"], exclude_patterns=["*PaT*"], case_sensitive=False) 32 >>> [] 33 """ 34 matcher = fnmatch.fnmatchcase if case_sensitive else fnmatch.fnmatch 35 return [ 36 candidate 37 for candidate in candidates 38 if any(matcher(candidate, pattern) for pattern in include_patterns) 39 and all(not matcher(candidate, pattern) for pattern in exclude_patterns) 40 ]
Returns a list of strings that match any pattern in include_patterns
, but don't match any pattern in exclude_patterns
.
Patterns can be literals or glob style wildcard strings.
Exclusion patterns override include patterns, i.e. if an item matches an include pattern but also matches an exclude pattern, it will be excluded.
>>> strings = ["thispattern", "aPaTtErN", "mypatterns"]
>>> younotyou(strings, ["*pattern"])
>>> ['thispattern']
>>> younotyou(strings, ["*pattern*"])
>>> ['thispattern', 'mypatterns']
>>> younotyou(strings, ["*pattern*"], case_sensitive=False)
>>> ['thispattern', 'aPaTtErN', 'mypatterns']
>>> younotyou(strings, ["*pattern*"], ["my*", "*is*"], case_sensitive=False)
>>> ['aPaTtErN']
>>> younotyou(strings, exclude_patterns=["*PaT*"])
>>> ['thispattern', 'mypatterns']
>>> younotyou(strings, exclude_patterns=["*PaT*"], case_sensitive=False)
>>> []
>>> younotyou(strings, include_patterns=["*PaT*"], exclude_patterns=["*PaT*"], case_sensitive=False)
>>> []