pg.KeySpec¶
Accessible via pg.KeySpec
, pg.typing.KeySpec
.
- class KeySpec[source]¶
Bases:
pg.Formattable
,pg.JSONConvertible
Interface for key specifications.
A key specification determines what keys are acceptable for a symbolic field (see
pg.Field
). Usually, symbolic attributes have an 1:1 relationship with symbolic fields. But in some cases (e.g. a dict with dynamic keys), a field can be used to describe a group of symbolic attributes:# A dictionary that accepts key 'x' with float value # or keys started with 'foo' with int values. d = pg.Dict(value_spec=pg.Dict([ ('x', pg.typing.Float(min_value=0.0)), (pg.typing.StrKey('foo.*'), pg.typing.Int()) ]))
You may noticed that the code above pass a string ‘x’ for the key spec for a field definition. The string is automatically converted to
pg.typing.ConstStrKey
.PyGlove’s Builtin key specifications are:
KeySpec
typeClass
Fixed string identifier
Dynamic string identifier
Key of a list
Key of a tuple
In most scenarios, the user either use a string or a
StrKey
as the key spec, while otherKeySpec
subclasses (e.g.ListKey
andTupleKey
) are used internally to constrain list size and tuple items.Methods:
extend
(base)Extend base key specification and returns self.
match
(key)Returns whether current key specification can match a key.
Classes:
alias of
pg.typing.ConstStrKey
Attributes:
Returns whether current key is const.
- abstract extend(base)[source]¶
Extend base key specification and returns self.
NOTE(daiyip): When a
Field
extends a base Field (from a base schema), it callsextend
on both itsKeySpec
andValueSpec
.KeySpec.extend
is to determine whether theField
key is allowed to be extended, andValueSpec.extend
is to determine the finalValueSpec
after extension.- Return type:
- Parameters:
base – A base
KeySpec
object.- Returns:
An
KeySpec
object derived from this key spec by extending the base.
- from_str[source]¶
alias of
pg.typing.ConstStrKey
Methods:format
(**kwargs)Format this object.
match
(key)Whether can match against an input key.
to_json
(**kwargs)Returns a plain Python value as a representation for this object.
Attributes:
is_const
Returns whether current key is const.
text
Text of this const string key.