Skip to content

Base Model Module

This section documents the base model components of the Nextmv Python SDK.

base_model

Provides base functionality for handling JSON data in models.

This module contains utilities for converting between dictionaries and model instances, facilitating data serialization and deserialization.

CLASS DESCRIPTION
BaseModel:

A base class extending Pydantic's BaseModel with additional methods for JSON data serialization and deserialization.

FUNCTION DESCRIPTION
from_dict:

Load a data model instance from a dictionary containing class information and attributes.

BaseModel

Bases: BaseModel

Base class for data wrangling tasks with JSON.

This class extends Pydantic's BaseModel to provide additional methods for converting between Python objects and JSON/dictionary representations.

from_dict classmethod

from_dict(data: Optional[dict[str, Any]] = None)

Instantiate the class from a dictionary.

PARAMETER DESCRIPTION
data

The dictionary containing the data to instantiate the class. If None, returns None.

TYPE: dict[str, Any] DEFAULT: None

RETURNS DESCRIPTION
cls or None

An instance of the class with the given data or None if data is None.

Source code in nextmv/nextmv/base_model.py
@classmethod
def from_dict(cls, data: Optional[dict[str, Any]] = None):
    """
    Instantiate the class from a dictionary.

    Parameters
    ----------
    data : dict[str, Any], optional
        The dictionary containing the data to instantiate the class.
        If None, returns None.

    Returns
    -------
    cls or None
        An instance of the class with the given data or None if data is None.
    """

    if data is None:
        return None

    return cls(**data)

to_dict

to_dict() -> dict[str, Any]

Convert the class instance to a dictionary.

The conversion uses Pydantic's model_dump method, excluding None values and using field aliases if defined.

RETURNS DESCRIPTION
dict[str, Any]

Dictionary representation of the class instance.

Source code in nextmv/nextmv/base_model.py
def to_dict(self) -> dict[str, Any]:
    """
    Convert the class instance to a dictionary.

    The conversion uses Pydantic's model_dump method, excluding None values
    and using field aliases if defined.

    Returns
    -------
    dict[str, Any]
        Dictionary representation of the class instance.
    """

    return self.model_dump(mode="json", exclude_none=True, by_alias=True)

from_dict

from_dict(data: dict[str, Any]) -> Any

Load a data model instance from a dict with associated class info.

PARAMETER DESCRIPTION

data

The data to load.

TYPE: dict[str, Any]

RETURNS DESCRIPTION
Any

The loaded data model instance.

Source code in nextmv/nextmv/base_model.py
def from_dict(data: dict[str, Any]) -> Any:
    """
    Load a data model instance from a `dict` with associated class info.

    Parameters
    ----------
    data : dict[str, Any]
        The data to load.

    Returns
    -------
    Any
        The loaded data model instance.
    """

    module = import_module(data["class"]["module"])
    cls = getattr(module, data["class"]["name"])

    return cls.from_dict(data["attributes"])