pg.KeySpec¶
Accessible via pg.KeySpec, pg.typing.KeySpec.
- class KeySpec[source]¶
Bases:
pg.Formattable,pg.JSONConvertibleInterface 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:
KeySpectypeClass
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
StrKeyas the key spec, while otherKeySpecsubclasses (e.g.ListKeyandTupleKey) 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.ConstStrKeyAttributes:
Returns whether current key is const.
- abstract extend(base)[source]¶
Extend base key specification and returns self.
NOTE(daiyip): When a
Fieldextends a base Field (from a base schema), it callsextendon both itsKeySpecandValueSpec.KeySpec.extendis to determine whether theFieldkey is allowed to be extended, andValueSpec.extendis to determine the finalValueSpecafter extension.- Return type:
- Parameters:
base – A base
KeySpecobject.- Returns:
An
KeySpecobject derived from this key spec by extending the base.
- from_str[source]¶
alias of
pg.typing.ConstStrKey