Total Complexity | 12 |
Total Lines | 52 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | """Managers for multi cardinality vertex properties""" |
||
2 | |||
3 | |||
4 | class VertexPropertyManager: |
||
5 | def __init__(self, data_type, vertex_prop, card): |
||
6 | self._data_type = data_type |
||
7 | self._vertex_prop = vertex_prop |
||
8 | self._card = card |
||
9 | self._mapper_func = vertex_prop.__mapping__.mapper_func |
||
10 | |||
11 | @property |
||
12 | def mapper_func(self): |
||
13 | return self._mapper_func |
||
14 | |||
15 | def __call__(self, val): |
||
16 | results = [] |
||
17 | for v in self: |
||
18 | if v.value == val: |
||
19 | results.append(v) |
||
20 | if len(results) == 1: |
||
21 | results = results[0] |
||
22 | elif not results: |
||
23 | results = None |
||
24 | return results |
||
25 | |||
26 | |||
27 | class ListVertexPropertyManager(list, VertexPropertyManager): |
||
28 | def __init__(self, data_type, vertex_prop, card, obj): |
||
29 | VertexPropertyManager.__init__(self, data_type, vertex_prop, card) |
||
30 | list.__init__(self, obj) |
||
31 | self._vp_map = {} |
||
32 | |||
33 | @property |
||
34 | def vp_map(self): |
||
35 | return self._vp_map |
||
36 | |||
37 | def append(self, val): |
||
38 | vp = self._vertex_prop(self._data_type, card=self._card) |
||
39 | vp.value = self._data_type.validate(val) |
||
40 | super().append(vp) |
||
41 | |||
42 | |||
43 | class SetVertexPropertyManager(set, VertexPropertyManager): |
||
44 | def __init__(self, data_type, vertex_prop, card, obj): |
||
45 | VertexPropertyManager.__init__(self, data_type, vertex_prop, card) |
||
46 | set.__init__(self, obj) |
||
47 | |||
48 | def add(self, val): |
||
49 | vp = self._vertex_prop(self._data_type, card=self._card) |
||
50 | vp.value = self._data_type.validate(val) |
||
51 | super().add(vp) |
||
52 |