bundle.core.data

Attributes

D

Classes

Data

Base data model class, providing utilities for serialization and deserialization

Functions

configuration([arbitrary_types_allowed, ...])

Public interface to define configuration settings for Pydantic models.

Module Contents

bundle.core.data.D
bundle.core.data.configuration(arbitrary_types_allowed: bool = True, from_attributes: bool = True, extra: str = 'forbid', json_encoders: dict[type, collections.abc.Callable] | None = None)[source]

Public interface to define configuration settings for Pydantic models.

Parameters:
  • arbitrary_types_allowed (bool) – Allows arbitrary types, defaults to True.

  • from_attributes (bool) – Whether to use model attributes for configuration, defaults to True.

  • extra (str) – Policy for extra attributes, defaults to “forbid”.

  • json_encoders (dict[type, Callable] | None) – Custom JSON encoders for specific types.

Returns:

A ConfigDict instance with the specified configuration settings.

class bundle.core.data.Data(/, **data: Any)[source]

Bases: pydantic.BaseModel

Base data model class, providing utilities for serialization and deserialization from/to JSON, along with JSON Schema generation.

model_config

Default model configuration settings.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

classmethod from_dict(data: dict) D[source]
Async:

Create an instance of the model from a dictionary.

Parameters:

data (dict) – The data dictionary from which to create the model instance.

Returns:

An instance of the model.

Raises:

Exception – If the model instantiation fails.

async as_dict() dict[source]

Create an instance of the model from a JSON file.

Parameters:

json_path (Path) – The path to the JSON file.

Returns:

An instance of the model.

Raises:

Exception – If the model instantiation from the JSON file fails.

classmethod from_json(json_source: str | pathlib.Path) D[source]
Async:

Create an instance of the model from either a JSON string or a path to a JSON file.

Parameters:

json_source (str | Path) – The JSON string or path to the JSON file.

Returns:

An instance of the model.

Raises:
  • RuntimeError – If the json_source is neither a string nor a Path instance.

  • Exception – If the model instantiation from the JSON source fails.

async as_json() str[source]

Serialize the model instance to a JSON string.

Returns:

A JSON string representation of the model instance.

Raises:

Exception – If serialization to JSON fails.

async dump_json(path: pathlib.Path) None[source]

Write the JSON representation of the model instance to a file.

Parameters:

path (Path) – The file path where the JSON data will be saved.

Raises:

Exception – If writing to the file fails.

classmethod as_jsonschema(mode: pydantic.json_schema.JsonSchemaMode = 'serialization') dict[source]
Async:

Generate the JSON Schema of the model.

Parameters:

mode (json_schema.JsonSchemaMode) – The mode of the JSON Schema, defaults to “serialization”.

Returns:

A dictionary representing the JSON Schema of the model.

Raises:

Exception – If generating the JSON Schema fails.

classmethod as_jsonschema_str(mode: pydantic.json_schema.JsonSchemaMode = 'serialization') str[source]
Async:

Serialize the JSON Schema of the model to a JSON string.

Parameters:

mode (json_schema.JsonSchemaMode) – The mode of the JSON Schema, defaults to “serialization”.

Returns:

A JSON string representation of the model’s JSON Schema.

Raises:

Exception – If serializing the JSON Schema to a string fails.

async dump_jsonschema(path: pathlib.Path, mode: pydantic.json_schema.JsonSchemaMode = 'serialization') None[source]

Write the JSON Schema of the model to a file.

Parameters:
  • path (Path) – The file path where the JSON Schema will be saved.

  • mode (json_schema.JsonSchemaMode) – The mode of the JSON Schema, defaults to “serialization”.

Raises:

Exception – If writing the JSON Schema to the file fails.