-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
34 lines (26 loc) · 977 Bytes
/
index.py
File metadata and controls
34 lines (26 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from collections import defaultdict
class InvertedIndex:
PRICE_RANGE = ['200-500', '500+']
def __init__(self):
self._index = defaultdict(lambda: defaultdict(set))
for prange in self.PRICE_RANGE:
self._index['price_range'][prange] # noqa. used to init ranges.
@property
def index(self):
return self._index
def update_index(self, product):
for k, v in product.attrs.items():
self._index[k][v].add(product.id)
self.index_by_price_range(product)
def filter(self, k, v):
return self._index[k][v]
def get_group(self, k):
return self._index[k]
def index_by_price_range(self, product):
price = getattr(product, 'price', None)
if price is None:
return
if 200 <= price <= 500:
self._index['price_range']['200-500'].add(product)
elif price > 500:
self._index['price_range']['500+'].add(product)