RecordBase¶
-
class
RecordBase(object)¶ A superclass to all record classes
-
_fields: ClassVar[FieldDict]¶ a dict of fields, by name
-
static
__new__(cls, arg=<object object>, **kwargs)¶ Create an instance of the class.
- Parameters
arg – If there is exactly one required field in the class (called the trivial field), the single positional argument can be used to fill it. Alternatively, if
unary_parsehas been enabled,argcan be used (without keyword arguments) to parse the argument.kwargs – The mapping is used to fill the values of all fields in the record instance.
Note
If
argcan be interpreted as both a parsing argument and as the trivial field. ATypeErroris raised.
-
post_new() → Optional[T]¶ This method is called after an instance is created and all its fields filled. This method may throw an exception to signal an invalid configuration.
- Returns
May return a new instance, in which case it will replace the instance created, or
Noneto keep it as is.
-
to_dict() → Mapping[str, Any]¶ export an instance to a dictionary.
Note
This class method supports selection and exporting arguments.
-
to_json(*args, io=None, **kwargs) → str¶ export an instance to a JSON dictionary.
- Parameters
args – forwarded to either
json.dumporjson.dumpsio – If not
None, dumpsselfintoio.kwargs – forwarded to either
json.dumporjson.dumps
- Returns
Noneifiois notNone, otherwise a JSON string representingself
Note
This class method supports selection and exporting arguments.
-
to_pickle(*args, io=None, **kwargs) → bytes¶ export an instance to a pickled bytestring.
- Parameters
args – forwarded to either
pickle.dumporpickle.dumpsio – If not
None, dumpsselfintoio.kwargs – forwarded to either
pickle.dumporpickle.dumps
- Returns
Noneifiois notNone, otherwise a pickle bytestring representingself
-
__eq__(other)¶ Return self==value.
-
__hash__()¶ - Returns
hash(self)
Note
if the class is non-frozen, this function will be overridden
-
__repr__(**kwargs)¶ Return
repr(self)- Parameters
kwargs – forwarded to
self.do_dictused to calculate the items to return.
- class and static methods
-
classmethod
__init_subclass__(*, frozen: bool = False, unary_parse: Optional[bool] = None, ordered=False, default_type_check=<TypeCheckStyle.hollow: 2>, **kwargs)¶ sets up the record subclass
- Parameters
frozen – Whether the class should be considered immutable (and thus, hashable)
unary_parse – Whether to enable unary parsing. By default is only disabled if the class has exactly one required field.
default_type_check – The default type checking style of the class, all fields will use this style unless otherwise specified.
-
classmethod
default_type_check_style()¶ - Returns
The default type checking style.
-
from_instance(v, *maps: Mapping[str, Any], _select=<records.select.Select object>, **kwargs)¶ Convert an object to a Record instance by attributes.
- Parameters
v – An object to get attributes from.
maps – Mappings to combine into field values.
_select – a private Select to be used when extracting object attributes.
kwargs – Additional field name in the instance.
- Returns
An instance of
clswith arguments as described by the input namespace and mappings.
Note
This class method supports Selection.
Note
If the class is frozen, and there are no additional mappings or kwargs supplied, the method may return
v.Note
This class method is a registered parser that will be attempted when calling
cls.parse.
-
from_json(v, **kwargs)¶ Convert a JSON mapping to a Record instance.
- Parameters
v – An JSON dictionary string to get attributes from.
kwargs – Additional field name in the instance.
- Returns
An instance of
clswith arguments as described by the JSON.
Note
This class method supports Selection.
Note
This class method is a registered parser that will be attempted when calling
cls.parse.
-
from_json_io(v, **kwargs)¶ Convert a JSON file to a Record instance.
- Parameters
v – An JSON dictionary file string to get attributes from.
kwargs – Additional field name in the instance.
- Returns
An instance of
clswith arguments as described by the JSON.
Note
This class method supports Selection.
-
from_mapping(*maps: Mapping[str, Any], **kwargs: Any)¶ Convert a mapping to a Record instance.
- Parameters
maps – Mappings to combine into field values.
kwargs – Additional field name in the instance.
- Returns
An instance of
clswith arguments as described by the input mappings.
Note
This class method supports Selection.
Note
This class method is a registered parser that will be attempted when calling
cls.parse.
-
classmethod
from_pickle(v, *args, **kwargs)¶ Convert a pickled bytestring to a Record instance.
- Parameters
v – A bytestring object.
args – forwarded to
pickle.loadskwargs – forwarded to
pickle.loads
- Returns
An unpickled instance of
cls.
Note
If the unpickling result succeeds but the result is not an instance of
cls, thenclsattempts to parse the resulting object.
-
classmethod
from_pickle_io(v, *args, **kwargs)¶ Convert a pickled file to a Record instance.
- Parameters
v – A record bytestring object.
args – forwarded to
pickle.loadkwargs – forwarded to
pickle.load
- Returns
An unpickled instance of
cls.
Note
If the unpickling result succeeds but the result is nto an instance of
cls, thenclsattempts to parse the resulting object.
-
classmethod
is_frozen()¶ - Returns
Whether the class is frozen.
-
classmethod
parse(v)¶ Attempt to run all registered parsers of
clsto parse an object to aclsinstance.- Parameters
v – the object to attempt to parse.
- Returns
The result of the only parser to succeed (raises an exception in all other cases).
- Raises
ParseFailure – If none of the registered parsers succeed.
TypeError – If more than one of the registered parsers succeed.
-
classmethod
pre_bind()¶ A class method that gets called when the class is initialized, but before all the field fillers are bound to the class. Subclasses may override this method and add validators and coercers to fields.
-
classmethod
_to_dict(obj, include_defaults=False, sort=None, blacklist_tags: Union[Container[records.tags.Tag], records.tags.Tag] = frozenset({}), whitelist_keys: Union[Container[str], str] = frozenset({}), _rev_select: records.select.Select = <records.select.Select object>) → Dict[str, Any]¶ Create a dict representing the values of the class’s fields for an arbitrary objects
- Parameters
obj – the object to get attributes from
include_defaults – whether to include keys that match the default value.
sort – whether to sort the keys of the dictionary numerically. If falsish, the keys will not be sorted. If equal to -1, the items will be sorted in reverse lexicographic order. If callable, the items will be sorted according to
sortas a key. Otherwise, the items will be sorted lexicographically.blacklist_tags – A
Tagor set of Tags to ignore all fields with theTag.whitelist_keys – A field name or set of field names to include regardless of
blacklistandinclude_defaults._rev_select – A private
Select, intended to be applied over the result, the function will attempt to extract the appropriate keys to fit the select
- Returns
A
dictobject with key names as specified byinclude_defaults,sort,blacklist_tags,whitelist_keys, and with values according toobj’s attributes.- Raises
AttributeError – if
objlacks an attribute that has not been blacklisted
-
classmethod
-