1 """! pattern that can match one or more Parameters"""
3 from ._parameter
import Parameter
7 """! Pattern that can match one or more paramters
9 This is an internal class and should not be used outside of this module
11 A single "pattern" is a str that is one of the unary keywords OR
12 has a binary keyword followed by an "=" character followed by one
13 of that keywords values. All whitespace in a pattern is removed so
14 spaces can be added for easier readability.
18 Each pattern is made up of one or more operations where each operation
19 is separated by an ampersand "&" to reflect that a Pattern takes the
20 logical-and of the operations listed.
24 Each operation can be one of three things.
28 3. A binary operation.
30 ID numbers are the 5-digit millepede ID number for that parameter which encodes all
31 of the location information within it. It specifies a single millepede parameter.
33 Unary operations are special keywords that correspond to different logical grouping
34 of parameters. Some unary operations correspond to functions returning True/False
35 of the Parameter class while others are simple shortenings of common Pattern strings
38 Binary operations are a string formatted like "kw = val" where a *single* equal
39 sign is used. kw is a keyword corresponding to select functions from the Parameter
40 class while val is a keyword corresponding to specific options for each kw.
46 """!Make sure input is a valid ID number, otherwise return NotImplemented
48 A valid ID number is a 5-digit integer matching the Parameter.idn_str_pattern
51 if Parameter.idn_str_pattern.match(i):
57 """!Make sure input is a valid module number, otherwise return NotImplemented
59 A valid module number is an index counting from 0 at the front of the detector
64 if m < 0
or m > Pattern.NUM_MODULES-1:
71 """!Make sure input is a valid layer number, otherwise return NotImplemented
73 A valid layer number is an index counting axial/stereo pairs from 1 at
74 the front of the detector up to NUM_MODULES
78 if layer < 1
or layer > Pattern.NUM_MODULES:
89 'module': __validate_module,
90 'layer': __validate_layer,
91 'direction': {
'u': 1,
'v': 2,
'w': 3}
111 'trans':
'translation',
113 'tu':
'direction=u & translation',
114 'rw':
'direction=w & rotation'
118 """! add a check into our list of checks, making sure it is the correct format"""
119 if not isinstance(c, tuple)
or len(c) != 2:
120 raise ValueError(
"Checks have to be 2-tuples")
124 if kw
in Pattern.binary_keywords.keys():
126 possible_values = Pattern.binary_keywords[kw]
128 if isinstance(possible_values, dict):
129 if val
in possible_values.keys():
130 self.
_checks_checks.append((kw, possible_values[val]))
132 raise ValueError(f
'Value {val} for {kw} not recognized. Possible values:\n{possible_values.keys()}')
134 v = possible_values(val)
135 if v != NotImplemented:
136 self.
_checks_checks.append((kw, v))
138 raise ValueError(f
'Value {val} for {kw} is not recognized.')
139 elif kw
in Pattern.unary_keywords:
140 self.
_checks_checks.append((kw, c[1]))
141 elif kw
in Pattern.aliases:
142 self.
__init____init__(Pattern.aliases[kw], first=
False)
144 raise ValueError(f
'{kw} is not a recognized keyword')
151 if not isinstance(pattern, (int, str)):
152 raise ValueError(f
'Pattern {pattern} must be an int or str')
155 if isinstance(pattern, int)
or pattern.isnumeric():
157 self.
_add_check_add_check((
'id', str(pattern)))
160 ops = pattern.split(
'&')
164 split = op.split(
'=')
166 raise ValueError(f
'{op} does not have two sides of the equality')
171 if op.startswith(
'!'):
178 if not isinstance(p, Parameter):
179 return NotImplemented
181 for kw, val
in self.
_checks_checks:
182 if getattr(p, kw)() != val:
188 return self.
matchmatch(p)
Pattern that can match one or more paramters.
def __validate_layer(layer)
Make sure input is a valid layer number, otherwise return NotImplemented.
def __validate_module(m)
Make sure input is a valid module number, otherwise return NotImplemented.
def _add_check(self, c)
add a check into our list of checks, making sure it is the correct format
def __validate_id(i)
Make sure input is a valid ID number, otherwise return NotImplemented.
def __init__(self, pattern, *first=True)