|
1
|
|
|
""" |
|
2
|
|
|
Utilities for filtering sequences. |
|
3
|
|
|
""" |
|
4
|
|
|
|
|
5
|
|
|
from itertools import product, chain |
|
6
|
|
|
from operator import attrgetter, itemgetter |
|
7
|
|
|
from typing import Dict, Generator, List, Tuple |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class AttributeFilter: |
|
11
|
|
|
""" |
|
12
|
|
|
Filter a sequence of objects/namedtuples by on attribute value predicates. |
|
13
|
|
|
|
|
14
|
|
|
Parameters |
|
15
|
|
|
---------- |
|
16
|
|
|
keys: group of attribute keys to use as filter set. |
|
17
|
|
|
predicates: values of attribute set to use in filtering. |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
slots = ('keys', 'predicates', 'indexer') |
|
21
|
|
|
|
|
22
|
|
|
def __init__(self, keys: Tuple[str], predicates: List[Tuple]): |
|
23
|
|
|
self.keys = keys |
|
24
|
|
|
self.predicates = set(predicates) |
|
25
|
|
|
self.indexer = attrgetter(*keys) |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def including(self, sequence) -> Generator: |
|
28
|
|
|
"""Include the sequence elements matching the filter set.""" |
|
29
|
|
|
return (element for element in sequence |
|
30
|
|
|
if self.indexer(element) in self.predicates) |
|
31
|
|
|
|
|
32
|
|
|
def excluding(self, sequence) -> Generator: |
|
33
|
|
|
"""Exclude the sequence elements matching the filter set.""" |
|
34
|
|
|
return (element for element in sequence |
|
35
|
|
|
if self.indexer(element) not in self.predicates) |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def create_key_filter(properties: Dict[str, list]) -> List[Tuple]: |
|
39
|
|
|
"""Generate combinations of key, value pairs for each key in properties. |
|
40
|
|
|
|
|
41
|
|
|
Examples |
|
42
|
|
|
-------- |
|
43
|
|
|
properties = {'ent': ['geo_rev', 'supply_chain'], 'own', 'fi'} |
|
44
|
|
|
>> create_key_filter(properties) |
|
45
|
|
|
--> [('ent', 'geo_rev'), ('ent', 'supply_chain'), ('own', 'fi')] |
|
46
|
|
|
""" |
|
47
|
|
|
|
|
48
|
|
|
combinations = (product([k], v) for k, v in properties.items()) |
|
49
|
|
|
|
|
50
|
|
|
return chain.from_iterable(combinations) |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def create_indexer(indexes: list): |
|
54
|
|
|
"""Create indexer function to pluck values from list.""" |
|
55
|
|
|
|
|
56
|
|
|
if len(indexes) == 1: |
|
57
|
|
|
index = indexes[0] |
|
58
|
|
|
return lambda x: (x[index],) |
|
59
|
|
|
else: |
|
60
|
|
|
return itemgetter(*indexes) |
|
|
|
|
|
|
61
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.