Skip to content

Entities and data structures

Use a dataclass for an application result with known fields; BaseEntity adds recursive serialization and metadata. Use DotDict or StdClass when keys are determined at runtime. These utilities do not validate untrusted input like a Schema. The examples are independent of one another.

from dataclasses import dataclass, field
from enum import Enum
from orionis.support.entities import BaseEntity
class Status(Enum):
ACTIVE = "active"
@dataclass
class Account(BaseEntity):
name: str
status: Status = Status.ACTIVE
tags: list[str] = field(default_factory=list)
account = Account("Ada", tags=["admin"])
assert account.toDict() == {
"name": "Ada",
"status": "active",
"tags": ["admin"],
}
assert [item["name"] for item in account.getFields()] == ["name", "status", "tags"]
from orionis.support.types import DotDict, StdClass
settings = DotDict({"mail": {"driver": "smtp"}})
assert settings.mail.driver == "smtp"
assert settings.unknown is None
user = StdClass(name="Ada", active=True)
user.update(role="admin")
assert user.toDict()["role"] == "admin"
assert settings.export() == {"mail": {"driver": "smtp"}}
from types import MappingProxyType
from orionis.support.structures.freezer import FreezeThaw
source = {"hosts": ["a.example", "b.example"], "options": {"tls": True}}
frozen = FreezeThaw.freeze(source)
assert isinstance(frozen, MappingProxyType)
assert frozen["hosts"] == ("a.example", "b.example")
assert FreezeThaw.thaw(frozen) == source

base.py · dot_dict.py · std.py · freezer.py

Return to the module index.