Skip to content

metadata

zarr.abc.metadata

Metadata dataclass

Source code in zarr/abc/metadata.py
@dataclass(frozen=True)
class Metadata:
    def to_dict(self) -> dict[str, JSON]:
        """
        Recursively serialize this model to a dictionary.
        This method inspects the fields of self and calls `x.to_dict()` for any fields that
        are instances of `Metadata`. Sequences of `Metadata` are similarly recursed into, and
        the output of that recursion is collected in a list.
        """
        out_dict = {}
        for field in fields(self):
            key = field.name
            value = getattr(self, key)
            if isinstance(value, Metadata):
                out_dict[field.name] = getattr(self, field.name).to_dict()
            elif isinstance(value, str):
                out_dict[key] = value
            elif isinstance(value, Sequence):
                out_dict[key] = tuple(v.to_dict() if isinstance(v, Metadata) else v for v in value)
            else:
                out_dict[key] = value

        return out_dict

    @classmethod
    def from_dict(cls, data: dict[str, JSON]) -> Self:
        """
        Create an instance of the model from a dictionary
        """

        return cls(**data)

from_dict classmethod

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

Create an instance of the model from a dictionary

Source code in zarr/abc/metadata.py
@classmethod
def from_dict(cls, data: dict[str, JSON]) -> Self:
    """
    Create an instance of the model from a dictionary
    """

    return cls(**data)

to_dict

to_dict() -> dict[str, JSON]

Recursively serialize this model to a dictionary. This method inspects the fields of self and calls x.to_dict() for any fields that are instances of Metadata. Sequences of Metadata are similarly recursed into, and the output of that recursion is collected in a list.

Source code in zarr/abc/metadata.py
def to_dict(self) -> dict[str, JSON]:
    """
    Recursively serialize this model to a dictionary.
    This method inspects the fields of self and calls `x.to_dict()` for any fields that
    are instances of `Metadata`. Sequences of `Metadata` are similarly recursed into, and
    the output of that recursion is collected in a list.
    """
    out_dict = {}
    for field in fields(self):
        key = field.name
        value = getattr(self, key)
        if isinstance(value, Metadata):
            out_dict[field.name] = getattr(self, field.name).to_dict()
        elif isinstance(value, str):
            out_dict[key] = value
        elif isinstance(value, Sequence):
            out_dict[key] = tuple(v.to_dict() if isinstance(v, Metadata) else v for v in value)
        else:
            out_dict[key] = value

    return out_dict