medcat.storage.serialisables
Classes
Describes the strategy for serialising. |
|
The base serialisable protocol. |
|
The abstract serialisable base class. |
|
The base serialisable protocol. |
|
Functions
|
Gets all serialisable elements from a list or tuple. |
|
Gets all serialisable members of an object. |
Module Contents
- class medcat.storage.serialisables.SerialisingStrategy
Bases:
enum.EnumDescribes the strategy for serialising.
- SERIALISABLE_ONLY
Only serialise attributes that are of Serialisable type
- SERIALISABLES_AND_DICT
Serialise attributes that are Serialisable as well as the rest of .__dict__
- DICT_ONLY
Only include the object’s .__dict__
- MANUAL
Use manual serialisation defined by the object itself.
- NOTE: In this case, most of the logic defined within here will
likely be ignored.
- _is_suitable_in_dict(attr_name, attr, obj)
- Parameters:
attr_name (str)
attr (Any)
obj (Serialisable)
- Return type:
bool
- _is_suitable_part(attr_name, part, obj)
- Parameters:
attr_name (str)
part (Any)
obj (Serialisable)
- Return type:
bool
- _iter_obj_items(obj)
- Parameters:
obj (Serialisable)
- Return type:
Iterable[tuple[str, Any]]
- _iter_obj_values(obj)
- Parameters:
obj (Serialisable)
- Return type:
Iterable[Any]
- get_dict(obj)
Gets the appropriate parts of the __dict__ of the object.
I.e this filters out parts that shouldn’t be included.
- Parameters:
obj (Serialisable) – The serialisable object.
- Returns:
dict[str, Any] – The filtered attributes map.
- Return type:
dict[str, Any]
- get_parts(obj)
Gets the matching serialisable parts of the object.
This includes only serialisable parts, and only if specified by the strategy.
- Returns:
list[tuple[Serialisable, str]] – The serialisable parts with names.
- Parameters:
obj (Serialisable)
- Return type:
list[tuple[Serialisable, str]]
- __new__(value)
- _generate_next_value_(start, count, last_values)
Generate the next value when not given.
name: the name of the member start: the initial start value or None count: the number of existing members last_value: the last value assigned or None
- classmethod _missing_(value)
- __repr__()
- __str__()
- __dir__()
Returns all members and all public methods
- __format__(format_spec)
Returns format using actual value type unless __str__ has been overridden.
- __hash__()
- __reduce_ex__(proto)
- name()
The name of the Enum member.
- value()
The value of the Enum member.
- class medcat.storage.serialisables.Serialisable
Bases:
ProtocolThe base serialisable protocol.
- get_strategy()
Get the serialisation strategy.
- Returns:
SerialisingStrategy – The strategy.
- Return type:
- classmethod get_init_attrs()
Get the names of the arguments needed for init upon deserialisation.
- Returns:
list[str] – The list of init arguments’ names.
- Return type:
list[str]
- classmethod ignore_attrs()
Get the names of attributes not to serialise.
- Returns:
list[str] – The attribute names that should not be serialised.
- Return type:
list[str]
- classmethod include_properties()
- Return type:
list[str]
- __slots__ = ()
- _is_protocol = True
- _is_runtime_protocol = False
- classmethod __init_subclass__(*args, **kwargs)
- classmethod __class_getitem__(params)
- class medcat.storage.serialisables.AbstractSerialisable
The abstract serialisable base class.
This defines some common defaults.
- get_strategy()
- Return type:
- classmethod get_init_attrs()
- Return type:
list[str]
- classmethod ignore_attrs()
- Return type:
list[str]
- classmethod include_properties()
- Return type:
list[str]
- __eq__(other)
- Parameters:
other (Any)
- Return type:
bool
- class medcat.storage.serialisables.ManualSerialisable
Bases:
Serialisable,ProtocolThe base serialisable protocol.
- serialise_to(folder_path)
Serialise to a folder.
- Parameters:
folder_path (str) – The folder to serialise to.
- Return type:
None
- classmethod deserialise_from(folder_path, **init_kwargs)
Deserialise from a specifc path.
The init keyword arguments are generally: - cnf: The config relevant to the components - tokenizer (BaseTokenizer): The base tokenizer for the model - cdb (CDB): The CDB for the model - vocab (Vocab): The Vocab for the model - model_load_path (Optional[str]): The model load path,
but not the component load path
- Parameters:
folder_path (str) – The path to deserialsie form.
- Returns:
ManualSerialisable – The deserialised object.
- Return type:
- get_strategy()
Get the serialisation strategy.
- Returns:
SerialisingStrategy – The strategy.
- Return type:
- classmethod get_init_attrs()
Get the names of the arguments needed for init upon deserialisation.
- Returns:
list[str] – The list of init arguments’ names.
- Return type:
list[str]
- classmethod ignore_attrs()
Get the names of attributes not to serialise.
- Returns:
list[str] – The attribute names that should not be serialised.
- Return type:
list[str]
- classmethod include_properties()
- Return type:
list[str]
- __slots__ = ()
- _is_protocol = True
- _is_runtime_protocol = False
- classmethod __init_subclass__(*args, **kwargs)
- classmethod __class_getitem__(params)
- class medcat.storage.serialisables.AbstractManualSerialisable
- get_strategy()
- Return type:
- classmethod get_init_attrs()
- Return type:
list[str]
- classmethod ignore_attrs()
- Return type:
list[str]
- classmethod include_properties()
- Return type:
list[str]
- medcat.storage.serialisables.name_all_serialisable_elements(target_list, name_start='', all_or_nothing=True)
Gets all serialisable elements from a list or tuple.
There’s two strategies for finding the parts: 1) If all_or_nothing == True either all the elements
in the list must be Serialisable or None of them.
- If all_or_nothing == False some elements may be
serialisable while others may not be.
- Parameters:
target_list (Union[list, tuple]) – The list/tuple of objects to look in.
name_start (str, optional) – The start of the name. Defaults to ‘’.
all_or_nothing (bool, optional) – Whether to disallow lists/tuple where only some elements are serialisable. Defaults to True.
- Raises:
ValueError – If all_or_nothing is specified and not all elements are serialisable.
- Returns:
list[tuple[Serialisable, str]] – The serialisable parts along with name.
- Return type:
list[tuple[Serialisable, str]]
- medcat.storage.serialisables.get_all_serialisable_members(object)
Gets all serialisable members of an object.
This looks for public and protected members, but not private ones. It should also be able to return parts of lists and tuples. It also provides the name of each serialisable object.
- Parameters:
object (Any) – The target object.
- Returns:
tuple[list[tuple[Serialisable, str]], dict[str, Any]] – list of serialisable objects along with their names
- Return type:
tuple[list[tuple[Serialisable, str]], dict[str, Any]]