younotyou.younotyou

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

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)
>>> []