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_parse has been enabled, arg can 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 arg can be interpreted as both a parsing argument and as the trivial field. A TypeError is 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 None to 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.dump or json.dumps

  • io – If not None, dumps self into io.

  • kwargs – forwarded to either json.dump or json.dumps

Returns

None if io is not None, otherwise a JSON string representing self

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.dump or pickle.dumps

  • io – If not None, dumps self into io.

  • kwargs – forwarded to either pickle.dump or pickle.dumps

Returns

None if io is not None, otherwise a pickle bytestring representing self

__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_dict used 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 cls with 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 cls with 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 cls with 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 cls with 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.loads

  • kwargs – 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, then cls attempts 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.load

  • kwargs – 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, then cls attempts to parse the resulting object.

classmethod is_frozen()
Returns

Whether the class is frozen.

classmethod parse(v)

Attempt to run all registered parsers of cls to parse an object to a cls instance.

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 sort as a key. Otherwise, the items will be sorted lexicographically.

  • blacklist_tags – A Tag or set of Tags to ignore all fields with the Tag.

  • whitelist_keys – A field name or set of field names to include regardless of blacklist and include_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 dict object with key names as specified by include_defaults, sort, blacklist_tags, whitelist_keys, and with values according to obj’s attributes.

Raises

AttributeError – if obj lacks an attribute that has not been blacklisted