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
117
118
119
120
121
122
123
124
125
126
127
128
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, *args: tuple[Any], **kwargs: dict[Any, Any]
    ) -> "BaseModel2DTM":
        """Load a MatchTemplateManager from a serialized JSON file.

        Parameters
        ----------
        json_path : str | os.PathLike
            Path to the JSON file to load.
        *args : tuple[Any]
            Additional positional arguments to pass to the constructor.
        **kwargs : dict[Any, Any]
            Additional keyword arguments to pass to the constructor.

        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(*args, **data, **kwargs)

    @classmethod
    def from_yaml(
        cls, yaml_path: str | os.PathLike, *args: tuple[Any], **kwargs: dict[Any, Any]
    ) -> "BaseModel2DTM":
        """Load a MatchTemplateManager from a serialized YAML file.

        Parameters
        ----------
        yaml_path : str | os.PathLike
            Path to the YAML file to load.
        *args : tuple[Any]
            Additional positional arguments to pass to the constructor.
        **kwargs : dict[Any, Any]
            Additional keyword arguments to pass to the constructor.

        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(*args, **data, **kwargs)

    ####################################
    ### 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, *args, **kwargs) 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
*args tuple[Any]

Additional positional arguments to pass to the constructor.

()
**kwargs dict[Any, Any]

Additional keyword arguments to pass to the constructor.

{}

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
64
65
66
67
68
69
@classmethod
def from_json(
    cls, json_path: str | os.PathLike, *args: tuple[Any], **kwargs: dict[Any, Any]
) -> "BaseModel2DTM":
    """Load a MatchTemplateManager from a serialized JSON file.

    Parameters
    ----------
    json_path : str | os.PathLike
        Path to the JSON file to load.
    *args : tuple[Any]
        Additional positional arguments to pass to the constructor.
    **kwargs : dict[Any, Any]
        Additional keyword arguments to pass to the constructor.

    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(*args, **data, **kwargs)

from_yaml(yaml_path, *args, **kwargs) 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
*args tuple[Any]

Additional positional arguments to pass to the constructor.

()
**kwargs dict[Any, Any]

Additional keyword arguments to pass to the constructor.

{}

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@classmethod
def from_yaml(
    cls, yaml_path: str | os.PathLike, *args: tuple[Any], **kwargs: dict[Any, Any]
) -> "BaseModel2DTM":
    """Load a MatchTemplateManager from a serialized YAML file.

    Parameters
    ----------
    yaml_path : str | os.PathLike
        Path to the YAML file to load.
    *args : tuple[Any]
        Additional positional arguments to pass to the constructor.
    **kwargs : dict[Any, Any]
        Additional keyword arguments to pass to the constructor.

    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(*args, **data, **kwargs)

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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)