Skip to content

custom_types

Additional type definitions and hints for Pydantic models.

BaseModel2DTM

Bases: BaseModel

Implementation of a Pydantic BaseModel with additional, useful methods.

Currently, only additional import/export methods are implemented and this class can effectively be treated as the pydantic.BaseModel class.

Attributes:

Name Type Description
None

Methods:

Name Description
from_json

Load a BaseModel2DTM subclass from a serialized JSON file.

from_yaml

Load a BaseModel2DTM subclass from a serialized YAML file.

to_json

Serialize the BaseModel2DTM subclass to a JSON file.

to_yaml

Serialize the BaseModel2DTM subclass to a YAML file.

Source code in src/leopard_em/pydantic_models/custom_types.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class BaseModel2DTM(BaseModel):
    """Implementation of a Pydantic BaseModel with additional, useful methods.

    Currently, only additional import/export methods are implemented and this
    class can effectively be treated as the `pydantic.BaseModel` class.

    Attributes
    ----------
    None

    Methods
    -------
    from_json(json_path: str | os.PathLike) -> BaseModel2DTM
        Load a BaseModel2DTM subclass from a serialized JSON file.
    from_yaml(yaml_path: str | os.PathLike) -> BaseModel2DTM
        Load a BaseModel2DTM subclass from a serialized YAML file.
    to_json(json_path: str | os.PathLike) -> None
        Serialize the BaseModel2DTM subclass to a JSON file.
    to_yaml(yaml_path: str | os.PathLike) -> None
        Serialize the BaseModel2DTM subclass to a YAML file.
    """

    model_config: ClassVar = ConfigDict(extra="forbid")

    #####################################
    ### Import/instantiation methods ###
    #####################################

    @classmethod
    def from_json(cls, json_path: str | os.PathLike) -> "BaseModel2DTM":
        """Load a MatchTemplateManager from a serialized JSON file.

        Parameters
        ----------
        json_path : str | os.PathLike
            Path to the JSON file to load.

        Returns
        -------
        BaseModel2DTM
            Instance of the BaseModel2DTM subclass loaded from the JSON file.
        """
        with open(json_path, encoding="utf-8") as f:
            data = json.load(f)

        return cls(**data)

    @classmethod
    def from_yaml(cls, yaml_path: str | os.PathLike) -> "BaseModel2DTM":
        """Load a MatchTemplateManager from a serialized YAML file.

        Parameters
        ----------
        yaml_path : str | os.PathLike
            Path to the YAML file to load.

        Returns
        -------
        BaseModel2DTM
            Instance of the BaseModel2DTM subclass loaded from the YAML file.
        """
        with open(yaml_path, encoding="utf-8") as f:
            data = yaml.safe_load(f)

        return cls(**data)

    ####################################
    ### Export/serialization methods ###
    ####################################

    def to_json(self, json_path: str | os.PathLike) -> None:
        """Serialize the MatchTemplateManager to a JSON file.

        Parameters
        ----------
        json_path : str | os.PathLike
            Path to the JSON file to save.

        Returns
        -------
        None
        """
        with open(json_path, "w", encoding="utf-8") as f:
            json.dump(self.model_dump(), f)

    def to_yaml(self, yaml_path: str | os.PathLike) -> None:
        """Serialize the MatchTemplateManager to a YAML file.

        Parameters
        ----------
        yaml_path : str | os.PathLike
            Path to the YAML file to save.

        Returns
        -------
        None
        """
        with open(yaml_path, "w", encoding="utf-8") as f:
            yaml.dump(self.model_dump(), f)

from_json(json_path) classmethod

Load a MatchTemplateManager from a serialized JSON file.

Parameters:

Name Type Description Default
json_path str | PathLike

Path to the JSON file to load.

required

Returns:

Type Description
BaseModel2DTM

Instance of the BaseModel2DTM subclass loaded from the JSON file.

Source code in src/leopard_em/pydantic_models/custom_types.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@classmethod
def from_json(cls, json_path: str | os.PathLike) -> "BaseModel2DTM":
    """Load a MatchTemplateManager from a serialized JSON file.

    Parameters
    ----------
    json_path : str | os.PathLike
        Path to the JSON file to load.

    Returns
    -------
    BaseModel2DTM
        Instance of the BaseModel2DTM subclass loaded from the JSON file.
    """
    with open(json_path, encoding="utf-8") as f:
        data = json.load(f)

    return cls(**data)

from_yaml(yaml_path) classmethod

Load a MatchTemplateManager from a serialized YAML file.

Parameters:

Name Type Description Default
yaml_path str | PathLike

Path to the YAML file to load.

required

Returns:

Type Description
BaseModel2DTM

Instance of the BaseModel2DTM subclass loaded from the YAML file.

Source code in src/leopard_em/pydantic_models/custom_types.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@classmethod
def from_yaml(cls, yaml_path: str | os.PathLike) -> "BaseModel2DTM":
    """Load a MatchTemplateManager from a serialized YAML file.

    Parameters
    ----------
    yaml_path : str | os.PathLike
        Path to the YAML file to load.

    Returns
    -------
    BaseModel2DTM
        Instance of the BaseModel2DTM subclass loaded from the YAML file.
    """
    with open(yaml_path, encoding="utf-8") as f:
        data = yaml.safe_load(f)

    return cls(**data)

to_json(json_path)

Serialize the MatchTemplateManager to a JSON file.

Parameters:

Name Type Description Default
json_path str | PathLike

Path to the JSON file to save.

required

Returns:

Type Description
None
Source code in src/leopard_em/pydantic_models/custom_types.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def to_json(self, json_path: str | os.PathLike) -> None:
    """Serialize the MatchTemplateManager to a JSON file.

    Parameters
    ----------
    json_path : str | os.PathLike
        Path to the JSON file to save.

    Returns
    -------
    None
    """
    with open(json_path, "w", encoding="utf-8") as f:
        json.dump(self.model_dump(), f)

to_yaml(yaml_path)

Serialize the MatchTemplateManager to a YAML file.

Parameters:

Name Type Description Default
yaml_path str | PathLike

Path to the YAML file to save.

required

Returns:

Type Description
None
Source code in src/leopard_em/pydantic_models/custom_types.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def to_yaml(self, yaml_path: str | os.PathLike) -> None:
    """Serialize the MatchTemplateManager to a YAML file.

    Parameters
    ----------
    yaml_path : str | os.PathLike
        Path to the YAML file to save.

    Returns
    -------
    None
    """
    with open(yaml_path, "w", encoding="utf-8") as f:
        yaml.dump(self.model_dump(), f)