Coverage for python/lsst/daf/butler/tests/dict_convertible_model.py: 88%
15 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-05 01:26 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-05 01:26 +0000
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22from __future__ import annotations
24__all__ = ()
26from collections.abc import Mapping
28from lsst.daf.butler._compat import _BaseModelCompat
29from pydantic import Field
32class DictConvertibleModel(_BaseModelCompat):
33 """A pydantic model to/from dict conversion in which the dict
34 representation is intentionally different from pydantics' own dict
35 conversions.
36 """
38 content: dict[str, str] = Field(default_factory=dict)
39 """Content of the logical dict that this object converts to (`dict`).
40 """
42 extra: str = Field(default="")
43 """Extra content that is not included in the dict representation (`str`).
44 """
46 @classmethod
47 def from_dict(cls, content: Mapping[str, str], extra: str = "from_dict") -> DictConvertibleModel:
48 """Construct an instance from a `dict`.
50 Parameters
51 ----------
52 content : `~collections.abc.Mapping`
53 Content of the logical dict that this object converts to.
54 extra : `str`, optional
55 Extra content that is not included in the dict representation
57 Returns
58 -------
59 model : `DictConvertibleModel`
60 New model.
61 """
62 return cls(content=dict(content), extra=extra)
64 def to_dict(self) -> dict[str, str]:
65 """Convert the model to a dictionary.
67 Returns
68 -------
69 content : `dict`
70 Copy of ``self.content``.
71 """
72 return self.content.copy()