Skip to content

filtering

quickdict.fitlering module.

Filtering of dictionaries.

qd_filter(p: None | Callable[[V], bool], m: Mapping[K, V]) -> dict[K, V]

Return a filtered dict with the predicate applied to the values.

p might be

  • None: filtered by bool(v),
  • otherwise: filtered by p(v).

Python implementation.

Source code in quickdict\filtering\_pyfiltering.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def qd_filter(p:None|Callable[[V],bool], m:Mapping[K,V]) -> dict[K,V]:
    """Return a filtered dict with the predicate applied to the values.

    `p` might be

    - `None`: filtered by `bool(v)`,
    - otherwise: filtered by `p(v)`.

    Python implementation.
    """
    if not (p is None or callable(p)):
        raise TypeError('p must be None or callable')
    if not isinstance(m, Mapping):
        raise TypeError('m must be a mapping')

    if p is None:
        return {k:v for k, v in m.items() if v}
    else:
        return {k:v for k, v in m.items() if p(v)}

qd_kfilter(p: Callable[[K], bool], m: Mapping[K, V]) -> dict[K, V]

Return a filtered dict with the predicate applied to the keys.

Filtered by p(k).

Python implementation.

Source code in quickdict\filtering\_pyfiltering.py
40
41
42
43
44
45
46
47
48
49
50
51
52
def qd_kfilter(p:Callable[[K],bool], m:Mapping[K,V]) -> dict[K,V]:
    """Return a filtered dict with the predicate applied to the keys.

    Filtered by `p(k)`.

    Python implementation.
    """
    if not callable(p):
        raise TypeError('p must be callable')
    if not isinstance(m, Mapping):
        raise TypeError('m must be a mapping')

    return {k:v for k, v in m.items() if p(k)}

qd_kvfilter(p: Callable[[K, V], bool], m: Mapping[K, V]) -> dict[K, V]

Return a filtered dict with the predicate applied to the items.

Filtered by p(k, v).

Python implementation.

Source code in quickdict\filtering\_pyfiltering.py
54
55
56
57
58
59
60
61
62
63
64
65
66
def qd_kvfilter(p:Callable[[K,V],bool], m:Mapping[K,V]) -> dict[K,V]:
    """Return a filtered dict with the predicate applied to the items.

    Filtered by `p(k, v)`.

    Python implementation.
    """
    if not callable(p):
        raise TypeError('p must be callable')
    if not isinstance(m, Mapping):
        raise TypeError('m must be a mapping')

    return {k:v for k, v in m.items() if p(k, v)}

qd_ifilter(p: None | Callable[[V], bool], m: MutableMapping[K, V]) -> MutableMapping[K, V]

Filter with the predicate applied to the values.

p might be

  • None: filtered by bool(v),
  • otherwise: filtered by p(v).

Python implementation.

Source code in quickdict\filtering\_pyfiltering.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def qd_ifilter(p:None|Callable[[V],bool], m:MutableMapping[K,V]) -> MutableMapping[K,V]:
    """Filter with the predicate applied to the values.

    `p` might be

    - `None`: filtered by `bool(v)`,
    - otherwise: filtered by `p(v)`.

    Python implementation.
    """
    if not (p is None or callable(p)):
        raise TypeError('p must be None or callable')
    if not isinstance(m, MutableMapping):
        raise TypeError('m must be a mutable mapping')

    if p is None:
        for k in [k for k, v in m.items() if not v]:
            del m[k]
    else:
        for k in [k for k, v in m.items() if not p(v)]:
            del m[k]
    return m

qd_ikfilter(p: Callable[[K], bool], m: MutableMapping[K, V]) -> MutableMapping[K, V]

Filter a mapping with the predicate applied to the keys.

Filtered by p(k).

Python implementation.

Source code in quickdict\filtering\_pyfiltering.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def qd_ikfilter(p:Callable[[K],bool], m:MutableMapping[K,V]) -> MutableMapping[K,V]:
    """Filter a mapping with the predicate applied to the keys.

    Filtered by `p(k)`.

    Python implementation.
    """
    if not callable(p):
        raise TypeError('p must be callable')
    if not isinstance(m, MutableMapping):
        raise TypeError('m must be a mutable mapping')

    for k in [k for k, v in m.items() if not p(k)]:
        del m[k]
    return m

qd_ikvfilter(p: Callable[[K, V], bool], m: MutableMapping[K, V]) -> MutableMapping[K, V]

Filter a mapping with the predicate applied to the items.

Filtered by p(k, v)

Python implementation.

Source code in quickdict\filtering\_pyfiltering.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def qd_ikvfilter(p:Callable[[K,V],bool], m:MutableMapping[K,V]) -> MutableMapping[K,V]:
    """Filter a mapping with the predicate applied to the items.

    Filtered by `p(k, v)`

    Python implementation.
    """
    if not callable(p):
        raise TypeError('p must be callable')
    if not isinstance(m, MutableMapping):
        raise TypeError('m must be a mutable mapping')

    for k in [k for k, v in m.items() if not p(k, v)]:
        del m[k]
    return m