/
key_specs.py
332 lines (258 loc) · 9.03 KB
/
key_specs.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# Copyright 2022 The PyGlove Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Concrete key specifications for field definition."""
import re
from typing import Any, Dict, Optional
from pyglove.core import object_utils
from pyglove.core.typing.class_schema import KeySpec
class KeySpecBase(KeySpec):
"""Base class for key specification subclasses."""
def extend(self, base: KeySpec) -> KeySpec:
"""Extend current key spec based on a base spec."""
if self != base:
raise KeyError(f'{self} cannot extend {base} for keys are different.')
return self
def __repr__(self) -> str:
"""Operator repr."""
return self.__str__()
def __ne__(self, other: Any) -> bool:
"""Operator !=."""
return not self.__eq__(other)
class ConstStrKey(KeySpecBase, object_utils.StrKey):
"""Class that represents a constant string key.
Example::
key = pg.typing.ConstStrKey('x')
assert key == 'x'
assert hash(key) == hash('x')
"""
__serialization_key__ = 'pyglove.typing.ConstStrKey'
@property
def is_const(self) -> bool:
return True
def __init__(self, text: str):
"""Constructor.
Args:
text: string value of this key.
Raises:
KeyError: If key contains dots ('.'), which is not allowed.
"""
if '.' in text:
raise KeyError(f'\'.\' cannot be used in key. Encountered: {text!r}.')
super().__init__()
self._text = text
@property
def text(self) -> str:
"""Text of this const string key."""
return self._text
def match(self, key: Any) -> bool:
"""Whether can match against an input key."""
return self._text == key
def format(self, **kwargs) -> str:
"""Format this object."""
return self._text
def to_json(self, **kwargs: Any) -> Dict[str, Any]:
return self.to_json_dict(
fields=dict(text=self._text),
**kwargs,
)
def __hash__(self) -> int:
"""Hash function.
NOTE(daiyip): ConstStrKey shares the same hash with its text, which
makes it easy to lookup a dict of string by an ConstStrKey object, and
vice versa.
Returns:
Hash code.
"""
return self._text.__hash__()
def __eq__(self, other: Any) -> bool:
"""Operator ==."""
if self is other:
return True
if isinstance(other, str):
return self.text == other
return isinstance(other, ConstStrKey) and other.text == self.text
class NonConstKey(KeySpecBase):
"""Base class of specification for non-const key.
Subclasses: :class:`pyglove.typing.StrKey`.
"""
@property
def is_const(self) -> bool:
return False
class StrKey(NonConstKey):
"""Class that represents a variable string key.
Example::
# Create a key spec that specifies all string keys started with 'foo'.
key = pg.typing.StrKey('foo.*')
assert key.match('foo')
assert key.match('foo1')
assert not key.match('bar')
"""
__serialization_key__ = 'pyglove.typing.StrKey'
def __init__(self, regex: Optional[str] = None):
"""Constructor.
Args:
regex: An optional regular expression. If set to None, any string value is
acceptable.
"""
super().__init__()
self._regex = re.compile(regex) if regex else None
def match(self, key: Any) -> bool:
"""Whether this key spec can match against input key."""
if not isinstance(key, str):
return False
if self._regex:
return self._regex.match(key) is not None
return True
@property
def regex(self):
"""Returns regular expression of this key spec."""
return self._regex
def format(self, **kwargs):
"""Format this object."""
regex_str = object_utils.kvlist_str([
('regex', object_utils.quote_if_str(
self._regex.pattern if self._regex else None), None)
])
return f'StrKey({regex_str})'
def to_json(self, **kwargs: Any) -> Dict[str, Any]:
regex = self._regex.pattern if self._regex is not None else None
return self.to_json_dict(
fields=dict(regex=(regex, None)),
exclude_default=True,
**kwargs,
)
def __hash__(self):
"""Hash function."""
if self._regex:
return self._regex.pattern.__hash__()
else:
return '.*'.__hash__()
def __eq__(self, other):
"""Operator ==."""
if self is other:
return True
return isinstance(other, StrKey) and self._regex == other.regex
class ListKey(NonConstKey):
"""Class that represents key specification for a list.
Example::
# Create a key spec that specifies list items from 1 to 5 (zero-based).
key = pg.typing.ListKey(min_value=1, max_value=5)
assert key.match(1)
assert key.match(5)
assert not key.match(0)
"""
__serialization_key__ = 'pyglove.typing.ListKey'
def __init__(
self, min_value: int = 0, max_value: Optional[int] = None):
"""Constructor.
Args:
min_value: Min value that is acceptable for the list index.
max_value: Max value that is acceptable for the list index. If None, there
is no upper bound for list index.
"""
super().__init__()
self._min_value = min_value
self._max_value = max_value
def extend(self, base: KeySpec) -> 'ListKey':
"""Extend current key spec on top of base spec."""
if not isinstance(base, ListKey):
raise TypeError(f'{self} cannot extend {base}: incompatible type.')
if self.min_value < base.min_value:
raise TypeError(f'{self} cannot extend {base}: min_value is smaller.')
if base.max_value is None:
return self
if self.max_value is None:
self._max_value = base.max_value
elif self.max_value > base.max_value:
raise TypeError(f'{self} cannot extend {base}: max_value is greater.')
return self
@property
def min_value(self) -> int:
"""Returns min value of acceptable list index value."""
return self._min_value
@property
def max_value(self) -> Optional[int]:
"""Returns max value of acceptable list index value."""
return self._max_value
def match(self, key: Any) -> bool:
"""Returns whether this key spec can match against input key."""
return (isinstance(key, int) and (self._min_value <= key) and
(not self._max_value or self._max_value > key))
def format(self, **kwargs):
"""Format this object."""
return f'ListKey(min_value={self._min_value}, max_value={self._max_value})'
def to_json(self, **kwargs: Any) -> Dict[str, Any]:
return self.to_json_dict(
fields=dict(
min_value=(self._min_value, None),
max_value=(self._max_value, None)),
exclude_default=True,
**kwargs,
)
def __eq__(self, other):
"""Operator==."""
if self is other:
return True
return (isinstance(other, ListKey) and
self._min_value == other.min_value and
self._max_value == other.max_value)
class TupleKey(NonConstKey):
"""Class that represents a key specification for tuple.
Example::
# Create a key spec that specifies item 0 of a tuple.
key = pg.typing.TupleKey(0)
assert key.match(0)
assert not key.match(1)
"""
__serialization_key__ = 'pyglove.typing.TupleKey'
def __init__(self, index: Optional[int] = None):
"""Constructor.
Args:
index: index of the tuple field that this key spec applies to.
If None, this tuple value spec applies to all elements of a
variable-length tuple.
"""
super().__init__()
self._index = index
def extend(self, base: KeySpec) -> 'TupleKey':
"""Extends this key spec on top of a base spec."""
if not isinstance(base, TupleKey):
raise TypeError(f'{self} cannot extend {base}: incompatible type.')
if self._index is None:
self._index = base.index
elif base.index is not None and base.index != self.index:
raise KeyError(f'{self} cannot extend {base}: unmatched index.')
return self
@property
def index(self) -> Optional[int]:
"""Returns the index of tuple field that the key applies to."""
return self._index
def match(self, key: Any) -> bool:
"""Returns whether this key spec can match against input key."""
return isinstance(key, int) and (self._index is None or self._index == key)
def format(self, **kwargs):
"""Format this object."""
return 'TupleKey(index={self._index})'
def to_json(self, **kwargs: Any) -> Dict[str, Any]:
return self.to_json_dict(
fields=dict(index=(self._index, None)),
exclude_default=True,
**kwargs,
)
def __eq__(self, other):
"""Operator==."""
if self is other:
return True
return isinstance(other, TupleKey) and self._index == other.index
KeySpec.from_str = ConstStrKey