medcat.storage.serialisables ============================ .. py:module:: medcat.storage.serialisables Classes ------- .. autoapisummary:: medcat.storage.serialisables.SerialisingStrategy medcat.storage.serialisables.Serialisable medcat.storage.serialisables.AbstractSerialisable medcat.storage.serialisables.ManualSerialisable medcat.storage.serialisables.AbstractManualSerialisable Functions --------- .. autoapisummary:: medcat.storage.serialisables.name_all_serialisable_elements medcat.storage.serialisables.get_all_serialisable_members Module Contents --------------- .. py:class:: SerialisingStrategy Bases: :py:obj:`enum.Enum` Describes the strategy for serialising. .. py:attribute:: SERIALISABLE_ONLY Only serialise attributes that are of Serialisable type .. py:attribute:: SERIALISABLES_AND_DICT Serialise attributes that are Serialisable as well as the rest of .__dict__ .. py:attribute:: DICT_ONLY Only include the object's .__dict__ .. py:attribute:: MANUAL Use manual serialisation defined by the object itself. NOTE: In this case, most of the logic defined within here will likely be ignored. .. py:method:: _is_suitable_in_dict(attr_name, attr, obj) .. py:method:: _is_suitable_part(attr_name, part, obj) .. py:method:: _iter_obj_items(obj) .. py:method:: _iter_obj_values(obj) .. py:method:: get_dict(obj) Gets the appropriate parts of the __dict__ of the object. I.e this filters out parts that shouldn't be included. :param obj: The serialisable object. :type obj: Serialisable :Returns: **dict[str, Any]** -- The filtered attributes map. .. py:method:: 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. .. py:method:: __new__(value) .. py:method:: _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 .. py:method:: _missing_(value) :classmethod: .. py:method:: __repr__() .. py:method:: __str__() .. py:method:: __dir__() Returns all members and all public methods .. py:method:: __format__(format_spec) Returns format using actual value type unless __str__ has been overridden. .. py:method:: __hash__() .. py:method:: __reduce_ex__(proto) .. py:method:: name() The name of the Enum member. .. py:method:: value() The value of the Enum member. .. py:class:: Serialisable Bases: :py:obj:`Protocol` The base serialisable protocol. .. py:method:: get_strategy() Get the serialisation strategy. :Returns: **SerialisingStrategy** -- The strategy. .. py:method:: get_init_attrs() :classmethod: Get the names of the arguments needed for init upon deserialisation. :Returns: **list[str]** -- The list of init arguments' names. .. py:method:: ignore_attrs() :classmethod: Get the names of attributes not to serialise. :Returns: **list[str]** -- The attribute names that should not be serialised. .. py:method:: include_properties() :classmethod: .. py:attribute:: __slots__ :value: () .. py:attribute:: _is_protocol :value: True .. py:attribute:: _is_runtime_protocol :value: False .. py:method:: __init_subclass__(*args, **kwargs) :classmethod: .. py:method:: __class_getitem__(params) :classmethod: .. py:class:: AbstractSerialisable The abstract serialisable base class. This defines some common defaults. .. py:method:: get_strategy() .. py:method:: get_init_attrs() :classmethod: .. py:method:: ignore_attrs() :classmethod: .. py:method:: include_properties() :classmethod: .. py:method:: __eq__(other) .. py:class:: ManualSerialisable Bases: :py:obj:`Serialisable`, :py:obj:`Protocol` The base serialisable protocol. .. py:method:: serialise_to(folder_path) Serialise to a folder. :param folder_path: The folder to serialise to. :type folder_path: str .. py:method:: deserialise_from(folder_path, **init_kwargs) :classmethod: 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 :param folder_path: The path to deserialsie form. :type folder_path: str :Returns: **ManualSerialisable** -- The deserialised object. .. py:method:: get_strategy() Get the serialisation strategy. :Returns: **SerialisingStrategy** -- The strategy. .. py:method:: get_init_attrs() :classmethod: Get the names of the arguments needed for init upon deserialisation. :Returns: **list[str]** -- The list of init arguments' names. .. py:method:: ignore_attrs() :classmethod: Get the names of attributes not to serialise. :Returns: **list[str]** -- The attribute names that should not be serialised. .. py:method:: include_properties() :classmethod: .. py:attribute:: __slots__ :value: () .. py:attribute:: _is_protocol :value: True .. py:attribute:: _is_runtime_protocol :value: False .. py:method:: __init_subclass__(*args, **kwargs) :classmethod: .. py:method:: __class_getitem__(params) :classmethod: .. py:class:: AbstractManualSerialisable .. py:method:: get_strategy() .. py:method:: get_init_attrs() :classmethod: .. py:method:: ignore_attrs() :classmethod: .. py:method:: include_properties() :classmethod: .. py:function:: 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. 2) If `all_or_nothing == False` some elements may be serialisable while others may not be. :param target_list: The list/tuple of objects to look in. :type target_list: Union[list, tuple] :param name_start: The start of the name. Defaults to ''. :type name_start: str, optional :param all_or_nothing: Whether to disallow lists/tuple where only some elements are serialisable. Defaults to True. :type all_or_nothing: bool, optional :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. .. py:function:: 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. :param object: The target object. :type object: Any :Returns: **tuple[list[tuple[Serialisable, str]], dict[str, Any]]** -- list of serialisable objects along with their names