medcat.storage.jsonserialiser ============================= .. py:module:: medcat.storage.jsonserialiser Attributes ---------- .. autoapisummary:: medcat.storage.jsonserialiser.T medcat.storage.jsonserialiser._def_registry Classes ------- .. autoapisummary:: medcat.storage.jsonserialiser.Serialiser medcat.storage.jsonserialiser.AvailableSerialisers medcat.storage.jsonserialiser.TypeHandler medcat.storage.jsonserialiser.TypeRegistry medcat.storage.jsonserialiser.TypeBasedHandler medcat.storage.jsonserialiser.NumpyArrayHandler medcat.storage.jsonserialiser.SetHandler medcat.storage.jsonserialiser.DateTimeHandler medcat.storage.jsonserialiser.DataClassHandler medcat.storage.jsonserialiser.JsonSerialiser Module Contents --------------- .. py:class:: Serialiser Bases: :py:obj:`abc.ABC` The abstract serialiser base class. This class is responsible for both serialising and deserialising. .. py:attribute:: RAW_FILE :value: 'raw_dict.dat' .. py:property:: ser_type :type: AvailableSerialisers :abstractmethod: The serialiser type. .. py:method:: serialise(raw_parts, target_file) :abstractmethod: Serialise the raw attributes / objects. :param raw_parts: The raw objects to serialise. :type raw_parts: dict[str, Any] :param target_file: The file name to write to. :type target_file: str .. py:method:: deserialise(target_file) :abstractmethod: Deserialise data written to the specified file. :param target_file: The file to read from. :type target_file: str :Returns: **dict[str, Any]** -- The deserialised raw attributes / objects. .. py:method:: get_ser_type_file(folder) :classmethod: .. py:method:: save_ser_type_file(folder) Save the serialiser type into the specified folder. :param folder: The folder to use. :type folder: str .. py:method:: get_manually_serialised_path(folder) :classmethod: .. py:method:: check_ser_type(folder) Check that the folder contains data serialised by this serialiser. :param folder: Target folder. :type folder: str :raises TypeError: If the folder was not serialised by this serialiser. .. py:method:: serialise_all(obj, target_folder, overwrite = False) Serialise the entire object into the target folder. This finds the serialisable parts (attributes) of the object and calls the same method on them recursively. It also finds the raw attributes (if any) and serialises them. :param obj: The object to serialise. :type obj: Serialisable :param target_folder: The target folder. :type target_folder: str :param overwrite: Whether to allow overwriting. Defaults to False. :type overwrite: bool :raises IllegalSchemaException: If there's multiple parts with the same name or a file already exists. .. py:method:: deserialise_manually(folder_path, man_cls_path, **init_kwargs) :classmethod: .. py:method:: deserialise_all(folder_path, ignore_folders_prefix = set(), ignore_folders_suffix = set(), **kwargs) Deserialise contents of folder. Additional initialisation keyword arguments can be provided if needed. This loads both the raw attributes for this object as well as the serialisable parts / attributes recursively. :param folder_path: The folder path. :type folder_path: str :param ignore_folders_prefix: The prefixes of folders to ignore. :type ignore_folders_prefix: set[str] :param ignore_folders_suffix: The suffixes of folders to ignore. :type ignore_folders_suffix: set[str] :Returns: **Serialisable** -- The resulting object. .. py:attribute:: __slots__ :value: () .. py:class:: AvailableSerialisers Bases: :py:obj:`enum.Enum` Describes the available serialisers. .. py:attribute:: dill .. py:attribute:: json .. py:method:: write_to(file_path) .. py:method:: from_file(file_path) :classmethod: .. 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:data:: T .. py:class:: TypeHandler Bases: :py:obj:`Protocol`, :py:obj:`Generic`\ [\ :py:obj:`T`\ ] Base class for protocol classes. Protocol classes are defined as:: class Proto(Protocol): def meth(self) -> int: ... Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:: class C: def meth(self) -> int: return 0 def func(x: Proto) -> int: return x.meth() func(C()) # Passes static type check See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:: class GenProto(Protocol[T]): def meth(self) -> T: ... .. py:attribute:: type_name :type: str .. py:attribute:: type_cls :type: type .. py:method:: should_encode(obj) .. py:method:: encode(obj) Encode an object of the registered type. .. py:method:: decode(obj) Decode an object of the registered type. .. 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:: TypeRegistry .. py:attribute:: _type_key :value: '__type__' .. py:attribute:: _data_key :value: 'data' .. py:method:: __init__() .. py:attribute:: handlers :type: dict[str, TypeHandler] .. py:method:: register(handler) Register a new type handler. .. py:method:: encode(obj) Encode an object using the registered handler. .. py:method:: decode(obj) Decode an object using the registered handler. .. py:class:: TypeBasedHandler Bases: :py:obj:`TypeHandler`\ [\ :py:obj:`T`\ ] Base class for protocol classes. Protocol classes are defined as:: class Proto(Protocol): def meth(self) -> int: ... Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:: class C: def meth(self) -> int: return 0 def func(x: Proto) -> int: return x.meth() func(C()) # Passes static type check See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:: class GenProto(Protocol[T]): def meth(self) -> T: ... .. py:method:: should_encode(obj) .. py:attribute:: type_name :type: str .. py:attribute:: type_cls :type: type .. py:method:: encode(obj) Encode an object of the registered type. .. py:method:: decode(obj) Decode an object of the registered type. .. 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:: NumpyArrayHandler Bases: :py:obj:`TypeBasedHandler`\ [\ :py:obj:`numpy.ndarray`\ ] Base class for protocol classes. Protocol classes are defined as:: class Proto(Protocol): def meth(self) -> int: ... Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:: class C: def meth(self) -> int: return 0 def func(x: Proto) -> int: return x.meth() func(C()) # Passes static type check See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:: class GenProto(Protocol[T]): def meth(self) -> T: ... .. py:attribute:: type_name :value: 'ndarray' .. py:attribute:: type_cls .. py:attribute:: _dtype_key :value: 'dtype' .. py:attribute:: _data_key :value: 'data' .. py:attribute:: _shape_key :value: 'shape' .. py:method:: encode(obj) Encode numpy ndarray. .. py:method:: decode(obj) Decode to numpy ndarray. .. py:method:: should_encode(obj) .. 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:: SetHandler Bases: :py:obj:`TypeBasedHandler`\ [\ :py:obj:`set`\ ] Base class for protocol classes. Protocol classes are defined as:: class Proto(Protocol): def meth(self) -> int: ... Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:: class C: def meth(self) -> int: return 0 def func(x: Proto) -> int: return x.meth() func(C()) # Passes static type check See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:: class GenProto(Protocol[T]): def meth(self) -> T: ... .. py:attribute:: type_name :value: 'set' .. py:attribute:: type_cls .. py:method:: encode(obj) Encode set. .. py:method:: decode(obj) Decode to set. .. py:method:: should_encode(obj) .. 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:: DateTimeHandler Bases: :py:obj:`TypeBasedHandler`\ [\ :py:obj:`datetime.datetime`\ ] Base class for protocol classes. Protocol classes are defined as:: class Proto(Protocol): def meth(self) -> int: ... Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:: class C: def meth(self) -> int: return 0 def func(x: Proto) -> int: return x.meth() func(C()) # Passes static type check See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:: class GenProto(Protocol[T]): def meth(self) -> T: ... .. py:attribute:: type_name :value: 'datetime' .. py:attribute:: type_cls .. py:method:: encode(obj) Encode an object of the registered type. .. py:method:: decode(obj) Decode an object of the registered type. .. py:method:: should_encode(obj) .. 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:: DataClassHandler Bases: :py:obj:`TypeHandler`\ [\ :py:obj:`T`\ ] Base class for protocol classes. Protocol classes are defined as:: class Proto(Protocol): def meth(self) -> int: ... Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:: class C: def meth(self) -> int: return 0 def func(x: Proto) -> int: return x.meth() func(C()) # Passes static type check See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:: class GenProto(Protocol[T]): def meth(self) -> T: ... .. py:attribute:: type_name :value: 'dataclass' .. py:attribute:: type_cls .. py:attribute:: _cls_key :value: 'class-path' .. py:attribute:: _data_key :value: 'data' .. py:method:: should_encode(obj) .. py:method:: encode(obj) Encode an object of the registered type. .. py:method:: decode(obj) Decode an object of the registered type. .. 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:data:: _def_registry .. py:class:: JsonSerialiser Bases: :py:obj:`medcat.storage.serialisers.Serialiser` The abstract serialiser base class. This class is responsible for both serialising and deserialising. .. py:attribute:: ser_type The serialiser type. .. py:method:: serialise(raw_parts, target_file) Serialise the raw attributes / objects. :param raw_parts: The raw objects to serialise. :type raw_parts: dict[str, Any] :param target_file: The file name to write to. :type target_file: str .. py:method:: deserialise(target_file) Deserialise data written to the specified file. :param target_file: The file to read from. :type target_file: str :Returns: **dict[str, Any]** -- The deserialised raw attributes / objects. .. py:attribute:: RAW_FILE :value: 'raw_dict.dat' .. py:method:: get_ser_type_file(folder) :classmethod: .. py:method:: save_ser_type_file(folder) Save the serialiser type into the specified folder. :param folder: The folder to use. :type folder: str .. py:method:: get_manually_serialised_path(folder) :classmethod: .. py:method:: check_ser_type(folder) Check that the folder contains data serialised by this serialiser. :param folder: Target folder. :type folder: str :raises TypeError: If the folder was not serialised by this serialiser. .. py:method:: serialise_all(obj, target_folder, overwrite = False) Serialise the entire object into the target folder. This finds the serialisable parts (attributes) of the object and calls the same method on them recursively. It also finds the raw attributes (if any) and serialises them. :param obj: The object to serialise. :type obj: Serialisable :param target_folder: The target folder. :type target_folder: str :param overwrite: Whether to allow overwriting. Defaults to False. :type overwrite: bool :raises IllegalSchemaException: If there's multiple parts with the same name or a file already exists. .. py:method:: deserialise_manually(folder_path, man_cls_path, **init_kwargs) :classmethod: .. py:method:: deserialise_all(folder_path, ignore_folders_prefix = set(), ignore_folders_suffix = set(), **kwargs) Deserialise contents of folder. Additional initialisation keyword arguments can be provided if needed. This loads both the raw attributes for this object as well as the serialisable parts / attributes recursively. :param folder_path: The folder path. :type folder_path: str :param ignore_folders_prefix: The prefixes of folders to ignore. :type ignore_folders_prefix: set[str] :param ignore_folders_suffix: The suffixes of folders to ignore. :type ignore_folders_suffix: set[str] :Returns: **Serialisable** -- The resulting object. .. py:attribute:: __slots__ :value: ()