turbo_broccoli.context

A context object holds information about the (de)serialization process, such as the current position in the document, output paths, etc.

  1"""
  2A context object holds information about the (de)serialization process, such as
  3the current position in the document, output paths, etc.
  4"""
  5
  6import tempfile
  7from os import environ as ENV
  8from pathlib import Path
  9from typing import Literal
 10from uuid import uuid4
 11
 12from .exceptions import TypeIsNodecode
 13
 14
 15def _list_of_types_to_dict(lot: list[type]) -> dict[str, type]:
 16    """
 17    Converts a list of types `[T1, T2, ...]` to a dict that looks like `{"T1":
 18    T1, "T2": T2, ...}`.
 19    """
 20    return {t.__name__: t for t in lot}
 21
 22
 23class Context:
 24    """
 25    (De)Serialization context, which is an object that contains various
 26    information and parameters about the ongoing operation. If you want your
 27    (de)serialization to behave a certain way, create a context object and pass
 28    it to
 29    [`turbo_broccoli.to_json`](https://altaris.github.io/turbo-broccoli/turbo_broccoli/turbo_broccoli.html#to_json)
 30    or
 31    [`turbo_broccoli.from_json`](https://altaris.github.io/turbo-broccoli/turbo_broccoli/turbo_broccoli.html#from_json).
 32    For convenience,
 33    [`turbo_broccoli.save_json`](https://altaris.github.io/turbo-broccoli/turbo_broccoli/turbo_broccoli.html#save_json)
 34    and
 35    [`turbo_broccoli.load_json`](https://altaris.github.io/turbo-broccoli/turbo_broccoli/turbo_broccoli.html#load_json)
 36    take the context parameter's as kwargs.
 37    """
 38
 39    artifact_path: Path
 40    dataclass_types: dict[str, type]
 41    file_path: Path | None
 42    json_path: str
 43    keras_format: str
 44    min_artifact_size: int = 8000
 45    nacl_shared_key: bytes | None
 46    nodecode_types: list[str]
 47    pandas_format: str
 48    pandas_kwargs: dict
 49    pytorch_module_types: dict[str, type]
 50    compress: bool
 51
 52    def __init__(
 53        self,
 54        file_path: str | Path | None = None,
 55        artifact_path: str | Path | None = None,
 56        min_artifact_size: int | None = None,
 57        nodecode_types: list[str] | None = None,
 58        keras_format: Literal["keras", "tf", "h5"] | None = None,
 59        pandas_format: (
 60            Literal[
 61                "csv",
 62                "excel",
 63                "feather",
 64                "html",
 65                "json",
 66                "latex",
 67                "orc",
 68                "parquet",
 69                "pickle",
 70                "sql",
 71                "stata",
 72                "xml",
 73            ]
 74            | None
 75        ) = None,
 76        pandas_kwargs: dict | None = None,
 77        nacl_shared_key: bytes | None = None,
 78        dataclass_types: dict[str, type] | list[type] | None = None,
 79        pytorch_module_types: dict[str, type] | list[type] | None = None,
 80        json_path: str = "$",
 81        compress: bool = False,
 82    ) -> None:
 83        """
 84        Args:
 85            file_path (str | Path | None, optional): Output JSON file path.
 86            artifact_path (str | Path | None, optional): Artifact path.
 87                Defaults to the parent directory of `file_path`, or a new
 88                temporary directory if `file_path` is `None`.
 89            min_artifact_size (int, optional): Byte strings (and everything
 90                that serialize to byte strings such as numpy arrays) larget
 91                than this will be stored in artifact rather than be embedded in
 92                the output JSON string/file.
 93            nodecode_types (list[str], optional): List of type names which
 94                shall be deserialized to `None` rather than their true value.
 95                See
 96                [`TB_NODECODE`](https://altaris.github.io/turbo-broccoli/turbo_broccoli.html#environment-variables)
 97            keras_format ("keras", "tf", "h5", optional): Format for Keras
 98                artifacts
 99            pandas_format ("csv", "excel", "feather", "html", "json", "latex",
100                "orc", "parquet", "pickle", "sql", "stata", "xml", optional):
101                Format for pandas artifacts
102            pandas_kwargs (dict, optional): kwargs to forward to the pandas
103                `to_*` and `read_*` function. For example, if
104                `pandas_format="parquet"`, then the content of `pandas.kwargs`
105                will be forwarded to
106                [`pandas.DataFrame.to_parquet`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_parquet.html)
107                and
108                [`pandas.read_parquet`](https://pandas.pydata.org/docs/reference/api/pandas.read_parquet.html)
109            nacl_shared_key (bytes, optional): PyNaCl shared key. See also
110                [PyNaCl's
111                documentation](https://pynacl.readthedocs.io/en/latest/secret/#key)
112            dataclass_types (dict[str, type] | list[type], optional): List of
113                dataclass types for deserialization. See the
114                [README](https://altaris.github.io/turbo-broccoli/turbo_broccoli.html#supported-types).
115            pytorch_module_types (dict[str, type] | list[type], optional): List
116                of pytorch module types for deserialization. See the
117                [README](https://altaris.github.io/turbo-broccoli/turbo_broccoli.html#supported-types).
118            json_path (str, optional): Current JSONpath. Don't use.
119            compress (bool, optional): Wether to compress the output JSON file/
120                string. Defaults to `False`. If `file_path` is provided and
121                ends in `.json.gz`, then this parameter is overrode to `True`.
122        """
123        self.json_path = json_path
124        self.file_path = (
125            Path(file_path) if isinstance(file_path, str) else file_path
126        )
127        if artifact_path is None:
128            if p := ENV.get("TB_ARTIFACT_PATH"):
129                self.artifact_path = Path(p)
130            else:
131                self.artifact_path = (
132                    self.file_path.parent
133                    if self.file_path is not None
134                    else Path(tempfile.mkdtemp())
135                )
136        else:
137            self.artifact_path = Path(artifact_path)
138        self.min_artifact_size = (
139            min_artifact_size
140            if min_artifact_size is not None
141            else int(ENV.get("TB_MAX_NBYTES", 8000))
142        )
143        self.nodecode_types = nodecode_types or ENV.get(
144            "TB_NODECODE", ""
145        ).split(",")
146        self.keras_format = keras_format or str(
147            ENV.get("TB_KERAS_FORMAT", "tf")
148        )
149        self.pandas_format = pandas_format or str(
150            ENV.get("TB_PANDAS_FORMAT", "csv")
151        )
152        self.pandas_kwargs = pandas_kwargs or {}
153        if isinstance(nacl_shared_key, bytes):
154            self.nacl_shared_key = nacl_shared_key
155        elif "TB_SHARED_KEY" in ENV:
156            self.nacl_shared_key = str(ENV["TB_SHARED_KEY"]).encode("utf-8")
157        else:
158            self.nacl_shared_key = None
159        self.dataclass_types = (
160            _list_of_types_to_dict(dataclass_types)
161            if isinstance(dataclass_types, list)
162            else (dataclass_types or {})
163        )
164        self.pytorch_module_types = (
165            _list_of_types_to_dict(pytorch_module_types)
166            if isinstance(pytorch_module_types, list)
167            else (pytorch_module_types or {})
168        )
169        self.compress = (
170            True
171            if (
172                self.file_path is not None
173                and self.file_path.name.endswith(".json.gz")
174            )
175            else compress
176        )
177
178    def __repr__(self) -> str:
179        fp, ap = str(self.file_path), str(self.artifact_path)
180        return (
181            f"Context(file_path={fp}, artifact_path={ap}, "
182            f"json_path={self.json_path})"
183        )
184
185    def __truediv__(self, x: str | int) -> "Context":
186        """
187        Returns a copy of the current context but where the `json_path`
188        attribute is `self.json_path + "." + str(x)`. Use this when you're
189        going down the document.
190        """
191        kwargs = self.__dict__.copy()
192        kwargs["json_path"] = self.json_path + "." + str(x)
193        return Context(**kwargs)
194
195    def id_to_artifact_path(self, art_id: str, extension: str = "tb") -> Path:
196        """
197        Takes an artifact id (which is an UUID4 string) and returns the
198        absolute path to the corresponding artifact file.
199        """
200        art_fn = art_id + "." + extension
201        if self.file_path is not None:
202            art_fn = self.file_path.stem + "." + art_fn
203        return self.artifact_path / art_fn
204
205    def new_artifact_path(self, extension: str = "tb") -> tuple[Path, str]:
206        """Returns the path to a new artifact alongside the artifact's ID"""
207        art_id = str(uuid4())
208        return self.id_to_artifact_path(art_id, extension), art_id
209
210    def raise_if_nodecode(self, type_name: str) -> None:
211        """
212        Raises a `turbo_broccoli.exceptions.TypeIsNodecode` exception if
213        `type_name` or any prefix is set to not be decoded in this context (see
214        `nodecode_types` constructor argument).
215
216        For example, if `type_name` is `a.b.c`, then this method raises
217        `turbo_broccoli.exceptions.TypeIsNodecode` if either `a`, `a.b`, or
218        `a.b.c` is set as a nodecode type.
219        """
220        parts = type_name.split(".")
221        for i in range(1, len(parts) + 1):
222            t = ".".join(parts[:i])
223            if t in self.nodecode_types:
224                raise TypeIsNodecode(t)
class Context:
 24class Context:
 25    """
 26    (De)Serialization context, which is an object that contains various
 27    information and parameters about the ongoing operation. If you want your
 28    (de)serialization to behave a certain way, create a context object and pass
 29    it to
 30    [`turbo_broccoli.to_json`](https://altaris.github.io/turbo-broccoli/turbo_broccoli/turbo_broccoli.html#to_json)
 31    or
 32    [`turbo_broccoli.from_json`](https://altaris.github.io/turbo-broccoli/turbo_broccoli/turbo_broccoli.html#from_json).
 33    For convenience,
 34    [`turbo_broccoli.save_json`](https://altaris.github.io/turbo-broccoli/turbo_broccoli/turbo_broccoli.html#save_json)
 35    and
 36    [`turbo_broccoli.load_json`](https://altaris.github.io/turbo-broccoli/turbo_broccoli/turbo_broccoli.html#load_json)
 37    take the context parameter's as kwargs.
 38    """
 39
 40    artifact_path: Path
 41    dataclass_types: dict[str, type]
 42    file_path: Path | None
 43    json_path: str
 44    keras_format: str
 45    min_artifact_size: int = 8000
 46    nacl_shared_key: bytes | None
 47    nodecode_types: list[str]
 48    pandas_format: str
 49    pandas_kwargs: dict
 50    pytorch_module_types: dict[str, type]
 51    compress: bool
 52
 53    def __init__(
 54        self,
 55        file_path: str | Path | None = None,
 56        artifact_path: str | Path | None = None,
 57        min_artifact_size: int | None = None,
 58        nodecode_types: list[str] | None = None,
 59        keras_format: Literal["keras", "tf", "h5"] | None = None,
 60        pandas_format: (
 61            Literal[
 62                "csv",
 63                "excel",
 64                "feather",
 65                "html",
 66                "json",
 67                "latex",
 68                "orc",
 69                "parquet",
 70                "pickle",
 71                "sql",
 72                "stata",
 73                "xml",
 74            ]
 75            | None
 76        ) = None,
 77        pandas_kwargs: dict | None = None,
 78        nacl_shared_key: bytes | None = None,
 79        dataclass_types: dict[str, type] | list[type] | None = None,
 80        pytorch_module_types: dict[str, type] | list[type] | None = None,
 81        json_path: str = "$",
 82        compress: bool = False,
 83    ) -> None:
 84        """
 85        Args:
 86            file_path (str | Path | None, optional): Output JSON file path.
 87            artifact_path (str | Path | None, optional): Artifact path.
 88                Defaults to the parent directory of `file_path`, or a new
 89                temporary directory if `file_path` is `None`.
 90            min_artifact_size (int, optional): Byte strings (and everything
 91                that serialize to byte strings such as numpy arrays) larget
 92                than this will be stored in artifact rather than be embedded in
 93                the output JSON string/file.
 94            nodecode_types (list[str], optional): List of type names which
 95                shall be deserialized to `None` rather than their true value.
 96                See
 97                [`TB_NODECODE`](https://altaris.github.io/turbo-broccoli/turbo_broccoli.html#environment-variables)
 98            keras_format ("keras", "tf", "h5", optional): Format for Keras
 99                artifacts
100            pandas_format ("csv", "excel", "feather", "html", "json", "latex",
101                "orc", "parquet", "pickle", "sql", "stata", "xml", optional):
102                Format for pandas artifacts
103            pandas_kwargs (dict, optional): kwargs to forward to the pandas
104                `to_*` and `read_*` function. For example, if
105                `pandas_format="parquet"`, then the content of `pandas.kwargs`
106                will be forwarded to
107                [`pandas.DataFrame.to_parquet`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_parquet.html)
108                and
109                [`pandas.read_parquet`](https://pandas.pydata.org/docs/reference/api/pandas.read_parquet.html)
110            nacl_shared_key (bytes, optional): PyNaCl shared key. See also
111                [PyNaCl's
112                documentation](https://pynacl.readthedocs.io/en/latest/secret/#key)
113            dataclass_types (dict[str, type] | list[type], optional): List of
114                dataclass types for deserialization. See the
115                [README](https://altaris.github.io/turbo-broccoli/turbo_broccoli.html#supported-types).
116            pytorch_module_types (dict[str, type] | list[type], optional): List
117                of pytorch module types for deserialization. See the
118                [README](https://altaris.github.io/turbo-broccoli/turbo_broccoli.html#supported-types).
119            json_path (str, optional): Current JSONpath. Don't use.
120            compress (bool, optional): Wether to compress the output JSON file/
121                string. Defaults to `False`. If `file_path` is provided and
122                ends in `.json.gz`, then this parameter is overrode to `True`.
123        """
124        self.json_path = json_path
125        self.file_path = (
126            Path(file_path) if isinstance(file_path, str) else file_path
127        )
128        if artifact_path is None:
129            if p := ENV.get("TB_ARTIFACT_PATH"):
130                self.artifact_path = Path(p)
131            else:
132                self.artifact_path = (
133                    self.file_path.parent
134                    if self.file_path is not None
135                    else Path(tempfile.mkdtemp())
136                )
137        else:
138            self.artifact_path = Path(artifact_path)
139        self.min_artifact_size = (
140            min_artifact_size
141            if min_artifact_size is not None
142            else int(ENV.get("TB_MAX_NBYTES", 8000))
143        )
144        self.nodecode_types = nodecode_types or ENV.get(
145            "TB_NODECODE", ""
146        ).split(",")
147        self.keras_format = keras_format or str(
148            ENV.get("TB_KERAS_FORMAT", "tf")
149        )
150        self.pandas_format = pandas_format or str(
151            ENV.get("TB_PANDAS_FORMAT", "csv")
152        )
153        self.pandas_kwargs = pandas_kwargs or {}
154        if isinstance(nacl_shared_key, bytes):
155            self.nacl_shared_key = nacl_shared_key
156        elif "TB_SHARED_KEY" in ENV:
157            self.nacl_shared_key = str(ENV["TB_SHARED_KEY"]).encode("utf-8")
158        else:
159            self.nacl_shared_key = None
160        self.dataclass_types = (
161            _list_of_types_to_dict(dataclass_types)
162            if isinstance(dataclass_types, list)
163            else (dataclass_types or {})
164        )
165        self.pytorch_module_types = (
166            _list_of_types_to_dict(pytorch_module_types)
167            if isinstance(pytorch_module_types, list)
168            else (pytorch_module_types or {})
169        )
170        self.compress = (
171            True
172            if (
173                self.file_path is not None
174                and self.file_path.name.endswith(".json.gz")
175            )
176            else compress
177        )
178
179    def __repr__(self) -> str:
180        fp, ap = str(self.file_path), str(self.artifact_path)
181        return (
182            f"Context(file_path={fp}, artifact_path={ap}, "
183            f"json_path={self.json_path})"
184        )
185
186    def __truediv__(self, x: str | int) -> "Context":
187        """
188        Returns a copy of the current context but where the `json_path`
189        attribute is `self.json_path + "." + str(x)`. Use this when you're
190        going down the document.
191        """
192        kwargs = self.__dict__.copy()
193        kwargs["json_path"] = self.json_path + "." + str(x)
194        return Context(**kwargs)
195
196    def id_to_artifact_path(self, art_id: str, extension: str = "tb") -> Path:
197        """
198        Takes an artifact id (which is an UUID4 string) and returns the
199        absolute path to the corresponding artifact file.
200        """
201        art_fn = art_id + "." + extension
202        if self.file_path is not None:
203            art_fn = self.file_path.stem + "." + art_fn
204        return self.artifact_path / art_fn
205
206    def new_artifact_path(self, extension: str = "tb") -> tuple[Path, str]:
207        """Returns the path to a new artifact alongside the artifact's ID"""
208        art_id = str(uuid4())
209        return self.id_to_artifact_path(art_id, extension), art_id
210
211    def raise_if_nodecode(self, type_name: str) -> None:
212        """
213        Raises a `turbo_broccoli.exceptions.TypeIsNodecode` exception if
214        `type_name` or any prefix is set to not be decoded in this context (see
215        `nodecode_types` constructor argument).
216
217        For example, if `type_name` is `a.b.c`, then this method raises
218        `turbo_broccoli.exceptions.TypeIsNodecode` if either `a`, `a.b`, or
219        `a.b.c` is set as a nodecode type.
220        """
221        parts = type_name.split(".")
222        for i in range(1, len(parts) + 1):
223            t = ".".join(parts[:i])
224            if t in self.nodecode_types:
225                raise TypeIsNodecode(t)

(De)Serialization context, which is an object that contains various information and parameters about the ongoing operation. If you want your (de)serialization to behave a certain way, create a context object and pass it to turbo_broccoli.to_json or turbo_broccoli.from_json. For convenience, turbo_broccoli.save_json and turbo_broccoli.load_json take the context parameter's as kwargs.

Context( file_path: str | pathlib.Path | None = None, artifact_path: str | pathlib.Path | None = None, min_artifact_size: int | None = None, nodecode_types: list[str] | None = None, keras_format: Optional[Literal['keras', 'tf', 'h5']] = None, pandas_format: Optional[Literal['csv', 'excel', 'feather', 'html', 'json', 'latex', 'orc', 'parquet', 'pickle', 'sql', 'stata', 'xml']] = None, pandas_kwargs: dict | None = None, nacl_shared_key: bytes | None = None, dataclass_types: dict[str, type] | list[type] | None = None, pytorch_module_types: dict[str, type] | list[type] | None = None, json_path: str = '$', compress: bool = False)
 53    def __init__(
 54        self,
 55        file_path: str | Path | None = None,
 56        artifact_path: str | Path | None = None,
 57        min_artifact_size: int | None = None,
 58        nodecode_types: list[str] | None = None,
 59        keras_format: Literal["keras", "tf", "h5"] | None = None,
 60        pandas_format: (
 61            Literal[
 62                "csv",
 63                "excel",
 64                "feather",
 65                "html",
 66                "json",
 67                "latex",
 68                "orc",
 69                "parquet",
 70                "pickle",
 71                "sql",
 72                "stata",
 73                "xml",
 74            ]
 75            | None
 76        ) = None,
 77        pandas_kwargs: dict | None = None,
 78        nacl_shared_key: bytes | None = None,
 79        dataclass_types: dict[str, type] | list[type] | None = None,
 80        pytorch_module_types: dict[str, type] | list[type] | None = None,
 81        json_path: str = "$",
 82        compress: bool = False,
 83    ) -> None:
 84        """
 85        Args:
 86            file_path (str | Path | None, optional): Output JSON file path.
 87            artifact_path (str | Path | None, optional): Artifact path.
 88                Defaults to the parent directory of `file_path`, or a new
 89                temporary directory if `file_path` is `None`.
 90            min_artifact_size (int, optional): Byte strings (and everything
 91                that serialize to byte strings such as numpy arrays) larget
 92                than this will be stored in artifact rather than be embedded in
 93                the output JSON string/file.
 94            nodecode_types (list[str], optional): List of type names which
 95                shall be deserialized to `None` rather than their true value.
 96                See
 97                [`TB_NODECODE`](https://altaris.github.io/turbo-broccoli/turbo_broccoli.html#environment-variables)
 98            keras_format ("keras", "tf", "h5", optional): Format for Keras
 99                artifacts
100            pandas_format ("csv", "excel", "feather", "html", "json", "latex",
101                "orc", "parquet", "pickle", "sql", "stata", "xml", optional):
102                Format for pandas artifacts
103            pandas_kwargs (dict, optional): kwargs to forward to the pandas
104                `to_*` and `read_*` function. For example, if
105                `pandas_format="parquet"`, then the content of `pandas.kwargs`
106                will be forwarded to
107                [`pandas.DataFrame.to_parquet`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_parquet.html)
108                and
109                [`pandas.read_parquet`](https://pandas.pydata.org/docs/reference/api/pandas.read_parquet.html)
110            nacl_shared_key (bytes, optional): PyNaCl shared key. See also
111                [PyNaCl's
112                documentation](https://pynacl.readthedocs.io/en/latest/secret/#key)
113            dataclass_types (dict[str, type] | list[type], optional): List of
114                dataclass types for deserialization. See the
115                [README](https://altaris.github.io/turbo-broccoli/turbo_broccoli.html#supported-types).
116            pytorch_module_types (dict[str, type] | list[type], optional): List
117                of pytorch module types for deserialization. See the
118                [README](https://altaris.github.io/turbo-broccoli/turbo_broccoli.html#supported-types).
119            json_path (str, optional): Current JSONpath. Don't use.
120            compress (bool, optional): Wether to compress the output JSON file/
121                string. Defaults to `False`. If `file_path` is provided and
122                ends in `.json.gz`, then this parameter is overrode to `True`.
123        """
124        self.json_path = json_path
125        self.file_path = (
126            Path(file_path) if isinstance(file_path, str) else file_path
127        )
128        if artifact_path is None:
129            if p := ENV.get("TB_ARTIFACT_PATH"):
130                self.artifact_path = Path(p)
131            else:
132                self.artifact_path = (
133                    self.file_path.parent
134                    if self.file_path is not None
135                    else Path(tempfile.mkdtemp())
136                )
137        else:
138            self.artifact_path = Path(artifact_path)
139        self.min_artifact_size = (
140            min_artifact_size
141            if min_artifact_size is not None
142            else int(ENV.get("TB_MAX_NBYTES", 8000))
143        )
144        self.nodecode_types = nodecode_types or ENV.get(
145            "TB_NODECODE", ""
146        ).split(",")
147        self.keras_format = keras_format or str(
148            ENV.get("TB_KERAS_FORMAT", "tf")
149        )
150        self.pandas_format = pandas_format or str(
151            ENV.get("TB_PANDAS_FORMAT", "csv")
152        )
153        self.pandas_kwargs = pandas_kwargs or {}
154        if isinstance(nacl_shared_key, bytes):
155            self.nacl_shared_key = nacl_shared_key
156        elif "TB_SHARED_KEY" in ENV:
157            self.nacl_shared_key = str(ENV["TB_SHARED_KEY"]).encode("utf-8")
158        else:
159            self.nacl_shared_key = None
160        self.dataclass_types = (
161            _list_of_types_to_dict(dataclass_types)
162            if isinstance(dataclass_types, list)
163            else (dataclass_types or {})
164        )
165        self.pytorch_module_types = (
166            _list_of_types_to_dict(pytorch_module_types)
167            if isinstance(pytorch_module_types, list)
168            else (pytorch_module_types or {})
169        )
170        self.compress = (
171            True
172            if (
173                self.file_path is not None
174                and self.file_path.name.endswith(".json.gz")
175            )
176            else compress
177        )

Args: file_path (str | Path | None, optional): Output JSON file path. artifact_path (str | Path | None, optional): Artifact path. Defaults to the parent directory of file_path, or a new temporary directory if file_path is None. min_artifact_size (int, optional): Byte strings (and everything that serialize to byte strings such as numpy arrays) larget than this will be stored in artifact rather than be embedded in the output JSON string/file. nodecode_types (list[str], optional): List of type names which shall be deserialized to None rather than their true value. See TB_NODECODE keras_format ("keras", "tf", "h5", optional): Format for Keras artifacts pandas_format ("csv", "excel", "feather", "html", "json", "latex", "orc", "parquet", "pickle", "sql", "stata", "xml", optional): Format for pandas artifacts pandas_kwargs (dict, optional): kwargs to forward to the pandas to_* and read_* function. For example, if pandas_format="parquet", then the content of pandas.kwargs will be forwarded to pandas.DataFrame.to_parquet and pandas.read_parquet nacl_shared_key (bytes, optional): PyNaCl shared key. See also PyNaCl's documentation dataclass_types (dict[str, type] | list[type], optional): List of dataclass types for deserialization. See the README. pytorch_module_types (dict[str, type] | list[type], optional): List of pytorch module types for deserialization. See the README. json_path (str, optional): Current JSONpath. Don't use. compress (bool, optional): Wether to compress the output JSON file/ string. Defaults to False. If file_path is provided and ends in .json.gz, then this parameter is overrode to True.

artifact_path: pathlib.Path
dataclass_types: dict[str, type]
file_path: pathlib.Path | None
json_path: str
keras_format: str
min_artifact_size: int = 8000
nacl_shared_key: bytes | None
nodecode_types: list[str]
pandas_format: str
pandas_kwargs: dict
pytorch_module_types: dict[str, type]
compress: bool
def id_to_artifact_path(self, art_id: str, extension: str = 'tb') -> pathlib.Path:
196    def id_to_artifact_path(self, art_id: str, extension: str = "tb") -> Path:
197        """
198        Takes an artifact id (which is an UUID4 string) and returns the
199        absolute path to the corresponding artifact file.
200        """
201        art_fn = art_id + "." + extension
202        if self.file_path is not None:
203            art_fn = self.file_path.stem + "." + art_fn
204        return self.artifact_path / art_fn

Takes an artifact id (which is an UUID4 string) and returns the absolute path to the corresponding artifact file.

def new_artifact_path(self, extension: str = 'tb') -> tuple[pathlib.Path, str]:
206    def new_artifact_path(self, extension: str = "tb") -> tuple[Path, str]:
207        """Returns the path to a new artifact alongside the artifact's ID"""
208        art_id = str(uuid4())
209        return self.id_to_artifact_path(art_id, extension), art_id

Returns the path to a new artifact alongside the artifact's ID

def raise_if_nodecode(self, type_name: str) -> None:
211    def raise_if_nodecode(self, type_name: str) -> None:
212        """
213        Raises a `turbo_broccoli.exceptions.TypeIsNodecode` exception if
214        `type_name` or any prefix is set to not be decoded in this context (see
215        `nodecode_types` constructor argument).
216
217        For example, if `type_name` is `a.b.c`, then this method raises
218        `turbo_broccoli.exceptions.TypeIsNodecode` if either `a`, `a.b`, or
219        `a.b.c` is set as a nodecode type.
220        """
221        parts = type_name.split(".")
222        for i in range(1, len(parts) + 1):
223            t = ".".join(parts[:i])
224            if t in self.nodecode_types:
225                raise TypeIsNodecode(t)

Raises a turbo_broccoli.exceptions.TypeIsNodecode exception if type_name or any prefix is set to not be decoded in this context (see nodecode_types constructor argument).

For example, if type_name is a.b.c, then this method raises turbo_broccoli.exceptions.TypeIsNodecode if either a, a.b, or a.b.c is set as a nodecode type.