-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsubkey.py
More file actions
94 lines (84 loc) · 3.54 KB
/
Copy pathsubkey.py
File metadata and controls
94 lines (84 loc) · 3.54 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Defines a parameter from some origin Vset
"""
class Subkey:
def __init__(self, value, origin: str, output_matching: bool = False):
"""
Parameters
----------
value: Any
subkey value corresponding to a Vset vfunc
origin: str
name of the origin Vset of this Subkey
output_matching: bool (optional), default False
inherited from the Vset where the Subkey is created
"""
self.meta = {}
self.meta['value'] = value
self.meta['origin'] = origin
self.meta['inputs'] = None
self.meta['params'] = None
self.meta['vfunc'] = None
self.value = value
self.origin = origin
self.output_matching = output_matching
# sep_dicts_id identifies the particular call to sep_dicts() that this
# key's dictionary went through (if any).
self.sep_dicts_id = None
def is_matching(self):
"""Checks if subkey should be matched in other Vsets
"""
return self.output_matching or self.sep_dicts_id is not None
def matches_sep_dict_id(self, other: object):
"""Helper to match Subkey by _sep_dict_id
"""
if isinstance(other, self.__class__):
return self.sep_dicts_id is not None \
and self.sep_dicts_id == other.sep_dicts_id
return False
def matches(self, other: object):
"""When Subkey matching is required, determines if this Subkey is compatible
with another, meaning that the origins and values match, and either the
_sep_dicts_id matches or both Subkeys have _output_matching True.
"""
if isinstance(other, self.__class__):
# they're both matching
cond0 = self.is_matching() and other.is_matching()
# value and origins match
cond1 = self.value == other.value and self.origin == other.origin
# sep_dicts_id matches
cond2 = self.sep_dicts_id == other.sep_dicts_id or \
(self.output_matching and other.output_matching)
return cond0 and cond1 and cond2
return False
def mismatches(self, other: object):
"""When Subkey matching is required, determines if this Subkey and another are
a bad match, meaning either:
1. output_matching is True, origin is same, value is different
2. output_matching is False, sep_dicts_id is same and not None, origin
is same, value is different
"""
if isinstance(other, self.__class__):
# one of the two keys is output_matching
cond0 = self.output_matching or other.output_matching
# neither key is output_matching but sep_dict_ids not None and match
cond1 = not cond0 and self.matches_sep_dict_id(other)
# origins match and values mismatch
cond2 = self.origin == other.origin and self.value != other.value
return (cond0 or cond1) and cond2
return True
def __copy__(self):
"""Return a copy of this Subkey
"""
def __eq__(self, other: object):
"""Mainly used for testing purposes.
"""
if isinstance(other, self.__class__):
# value and origins match
return self.value == other.value and self.origin == other.origin
return False
def __repr__(self):
return str(self.value)
def __hash__(self):
"""Mainly used for testing purposes.
"""
return hash(self.value) ^ hash(self.origin) ^ hash(self.output_matching)