Skip to content

medcat.storage.schema

Classes:

Functions:

  • load_schema

    Loads the schema for a folder of deserialisable files from the file.

  • save_schema

    Saves the schema of a class to the specified file.

Attributes:

DEFAULT_SCHEMA_FILE module-attribute

DEFAULT_SCHEMA_FILE = '.schema.json'

logger module-attribute

logger = getLogger(__name__)

IllegalSchemaException

IllegalSchemaException(*args)

Bases: ValueError

Source code in medcat-v2/medcat/storage/schema.py
61
62
def __init__(self, *args):
    super().__init__("Illegal schema:", *args)

load_schema

load_schema(file_name: str) -> tuple[str, list[str]]

Loads the schema for a folder of deserialisable files from the file.

Parameters:

  • file_name

    (str) –

    The schema file

Returns:

  • tuple[str, list[str]]

    tuple[str, list[str]]: The class package/name along with the parts needed for initialising.

Source code in medcat-v2/medcat/storage/schema.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def load_schema(file_name: str) -> tuple[str, list[str]]:
    """Loads the schema for a folder of deserialisable files from the file.

    Args:
        file_name (str): The schema file

    Returns:
        tuple[str, list[str]]: The class package/name along
            with the parts needed for initialising.
    """
    with open(file_name) as f:
        data = json.load(f)
    class_name, init_parts = data[_CLASS_PATH], data[_INIT_PARTS_PATH]
    if __package__.startswith("medcat.") and class_name.startswith("medcat2."):
        # if we're loading a beta-release (medcat2.* namespaced) schema
        # we need to convert it to the stable one
        logger.info(
            "Loading beta-release medcat2 schema at '%s'. "
            "Converting to release schema.", file_name)
        class_name = class_name.replace("medcat2.", "medcat.")
    return class_name, init_parts

save_schema

save_schema(file_name: str, cls: Type, init_parts: list[str]) -> None

Saves the schema of a class to the specified file.

Parameters:

  • file_name

    (str) –

    The file to save to.

  • cls

    (Type) –

    The class in question

  • init_parts list[str]

    The parts of the .

Source code in medcat-v2/medcat/storage/schema.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def save_schema(file_name: str, cls: Type, init_parts: list[str]) -> None:
    """Saves the schema of a class to the specified file.

    Args:
        file_name (str): The file to save to.
        cls (Type): The class in question
        init_parts list[str]: The parts of the .
    """
    out_data = {
        _CLASS_PATH: _cls2path(cls),
        _INIT_PARTS_PATH: init_parts,
    }
    with open(file_name, 'w') as f:
        json.dump(out_data, f)