some random stuff. caelestia incoming
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,68 @@
|
||||
from collections.abc import Mapping
|
||||
from typing import Any, Type, TypeVar, final
|
||||
|
||||
from mashumaro.core.meta.mixin import (
|
||||
compile_mixin_packer,
|
||||
compile_mixin_unpacker,
|
||||
)
|
||||
|
||||
__all__ = ["DataClassDictMixin"]
|
||||
|
||||
|
||||
T = TypeVar("T", bound="DataClassDictMixin")
|
||||
|
||||
|
||||
class DataClassDictMixin:
|
||||
__slots__ = ()
|
||||
|
||||
__mashumaro_builder_params = {"packer": {}, "unpacker": {}} # type: ignore
|
||||
|
||||
def __init_subclass__(cls: Type[T], **kwargs: Any):
|
||||
super().__init_subclass__(**kwargs)
|
||||
for ancestor in cls.__mro__[-1:0:-1]:
|
||||
builder_params_ = f"_{ancestor.__name__}__mashumaro_builder_params"
|
||||
builder_params = getattr(ancestor, builder_params_, None)
|
||||
if builder_params:
|
||||
compile_mixin_unpacker(cls, **builder_params["unpacker"])
|
||||
compile_mixin_packer(cls, **builder_params["packer"])
|
||||
|
||||
@final
|
||||
def to_dict(
|
||||
self: T,
|
||||
# *
|
||||
# keyword-only arguments that exist with the code generation options:
|
||||
# omit_none: bool = False
|
||||
# by_alias: bool = False
|
||||
# dialect: Type[Dialect] = None
|
||||
**kwargs: Any,
|
||||
) -> dict[Any, Any]: ...
|
||||
|
||||
@classmethod
|
||||
@final
|
||||
def from_dict(
|
||||
cls: Type[T],
|
||||
d: Mapping,
|
||||
# *
|
||||
# keyword-only arguments that exist with the code generation options:
|
||||
# dialect: Type[Dialect] = None
|
||||
**kwargs: Any,
|
||||
) -> T: ...
|
||||
|
||||
@classmethod
|
||||
def __pre_deserialize__(
|
||||
cls: Type[T], d: dict[Any, Any]
|
||||
) -> dict[Any, Any]: ...
|
||||
|
||||
@classmethod
|
||||
def __post_deserialize__(cls: Type[T], obj: T) -> T: ...
|
||||
|
||||
def __pre_serialize__(
|
||||
self: T,
|
||||
# context: Any = None, # added with ADD_SERIALIZATION_CONTEXT option
|
||||
) -> T: ...
|
||||
|
||||
def __post_serialize__(
|
||||
self: T,
|
||||
d: dict[Any, Any],
|
||||
# context: Any = None, # added with ADD_SERIALIZATION_CONTEXT option
|
||||
) -> dict[Any, Any]: ...
|
||||
@@ -0,0 +1,32 @@
|
||||
import json
|
||||
from collections.abc import Callable
|
||||
from typing import Any, Type, TypeVar, Union
|
||||
|
||||
from mashumaro.mixins.dict import DataClassDictMixin
|
||||
|
||||
T = TypeVar("T", bound="DataClassJSONMixin")
|
||||
|
||||
|
||||
EncodedData = Union[str, bytes, bytearray]
|
||||
Encoder = Callable[[Any], EncodedData]
|
||||
Decoder = Callable[[EncodedData], dict[Any, Any]]
|
||||
|
||||
|
||||
class DataClassJSONMixin(DataClassDictMixin):
|
||||
__slots__ = ()
|
||||
|
||||
def to_json(
|
||||
self: T,
|
||||
encoder: Encoder = json.dumps,
|
||||
**to_dict_kwargs: Any,
|
||||
) -> EncodedData:
|
||||
return encoder(self.to_dict(**to_dict_kwargs))
|
||||
|
||||
@classmethod
|
||||
def from_json(
|
||||
cls: Type[T],
|
||||
data: EncodedData,
|
||||
decoder: Decoder = json.loads,
|
||||
**from_dict_kwargs: Any,
|
||||
) -> T:
|
||||
return cls.from_dict(decoder(data), **from_dict_kwargs)
|
||||
@@ -0,0 +1,67 @@
|
||||
from collections.abc import Callable
|
||||
from typing import Any, Type, TypeVar, final
|
||||
|
||||
import msgpack
|
||||
|
||||
from mashumaro.dialect import Dialect
|
||||
from mashumaro.helper import pass_through
|
||||
from mashumaro.mixins.dict import DataClassDictMixin
|
||||
|
||||
T = TypeVar("T", bound="DataClassMessagePackMixin")
|
||||
|
||||
|
||||
EncodedData = bytes
|
||||
Encoder = Callable[[Any], EncodedData]
|
||||
Decoder = Callable[[EncodedData], dict[Any, Any]]
|
||||
|
||||
|
||||
class MessagePackDialect(Dialect):
|
||||
no_copy_collections = (list, dict)
|
||||
serialization_strategy = {
|
||||
bytes: pass_through,
|
||||
bytearray: {
|
||||
"deserialize": bytearray,
|
||||
"serialize": pass_through,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def default_encoder(data: Any) -> EncodedData:
|
||||
return msgpack.packb(data, use_bin_type=True)
|
||||
|
||||
|
||||
def default_decoder(data: EncodedData) -> dict[Any, Any]:
|
||||
return msgpack.unpackb(data, raw=False)
|
||||
|
||||
|
||||
class DataClassMessagePackMixin(DataClassDictMixin):
|
||||
__slots__ = ()
|
||||
|
||||
__mashumaro_builder_params = {
|
||||
"packer": {
|
||||
"format_name": "msgpack",
|
||||
"dialect": MessagePackDialect,
|
||||
"encoder": default_encoder,
|
||||
},
|
||||
"unpacker": {
|
||||
"format_name": "msgpack",
|
||||
"dialect": MessagePackDialect,
|
||||
"decoder": default_decoder,
|
||||
},
|
||||
}
|
||||
|
||||
@final
|
||||
def to_msgpack(
|
||||
self: T,
|
||||
encoder: Encoder = default_encoder,
|
||||
**to_dict_kwargs: Any,
|
||||
) -> EncodedData: ...
|
||||
|
||||
@classmethod
|
||||
@final
|
||||
def from_msgpack(
|
||||
cls: Type[T],
|
||||
data: EncodedData,
|
||||
decoder: Decoder = default_decoder,
|
||||
**from_dict_kwargs: Any,
|
||||
) -> T: ...
|
||||
@@ -0,0 +1,69 @@
|
||||
from collections.abc import Callable
|
||||
from datetime import date, datetime, time
|
||||
from typing import Any, Type, TypeVar, Union, final
|
||||
from uuid import UUID
|
||||
|
||||
import orjson
|
||||
|
||||
from mashumaro.core.helpers import ConfigValue
|
||||
from mashumaro.dialect import Dialect
|
||||
from mashumaro.helper import pass_through
|
||||
from mashumaro.mixins.dict import DataClassDictMixin
|
||||
|
||||
T = TypeVar("T", bound="DataClassORJSONMixin")
|
||||
|
||||
|
||||
EncodedData = Union[str, bytes, bytearray]
|
||||
Encoder = Callable[[Any], EncodedData]
|
||||
Decoder = Callable[[EncodedData], dict[Any, Any]]
|
||||
|
||||
|
||||
class OrjsonDialect(Dialect):
|
||||
no_copy_collections = (list, dict)
|
||||
serialization_strategy = {
|
||||
datetime: {"serialize": pass_through},
|
||||
date: {"serialize": pass_through},
|
||||
time: {"serialize": pass_through},
|
||||
UUID: {"serialize": pass_through},
|
||||
}
|
||||
|
||||
|
||||
class DataClassORJSONMixin(DataClassDictMixin):
|
||||
__slots__ = ()
|
||||
|
||||
__mashumaro_builder_params = {
|
||||
"packer": {
|
||||
"format_name": "jsonb",
|
||||
"dialect": OrjsonDialect,
|
||||
"encoder": orjson.dumps,
|
||||
"encoder_kwargs": {
|
||||
"option": ("orjson_options", ConfigValue("orjson_options")),
|
||||
},
|
||||
},
|
||||
"unpacker": {
|
||||
"format_name": "json",
|
||||
"dialect": OrjsonDialect,
|
||||
"decoder": orjson.loads,
|
||||
},
|
||||
}
|
||||
|
||||
@final
|
||||
def to_jsonb(
|
||||
self: T,
|
||||
encoder: Encoder = orjson.dumps,
|
||||
*,
|
||||
orjson_options: int = ...,
|
||||
**to_dict_kwargs: Any,
|
||||
) -> bytes: ...
|
||||
|
||||
def to_json(self: T, **kwargs: Any) -> str:
|
||||
return self.to_jsonb(**kwargs).decode()
|
||||
|
||||
@classmethod
|
||||
@final
|
||||
def from_json(
|
||||
cls: Type[T],
|
||||
data: EncodedData,
|
||||
decoder: Decoder = orjson.loads,
|
||||
**from_dict_kwargs: Any,
|
||||
) -> T: ...
|
||||
@@ -0,0 +1,42 @@
|
||||
from collections.abc import Callable
|
||||
from typing import Any, Type, TypeVar, Union, final
|
||||
|
||||
import orjson
|
||||
|
||||
from mashumaro.dialect import Dialect
|
||||
from mashumaro.mixins.dict import DataClassDictMixin
|
||||
|
||||
T = TypeVar("T", bound="DataClassORJSONMixin")
|
||||
|
||||
EncodedData = Union[str, bytes, bytearray]
|
||||
Encoder = Callable[[Any], EncodedData]
|
||||
Decoder = Callable[[EncodedData], dict[Any, Any]]
|
||||
|
||||
class OrjsonDialect(Dialect):
|
||||
serialization_strategy: Any
|
||||
|
||||
class DataClassORJSONMixin(DataClassDictMixin):
|
||||
__slots__ = ()
|
||||
@final
|
||||
def to_jsonb(
|
||||
self: T,
|
||||
encoder: Encoder = orjson.dumps,
|
||||
*,
|
||||
orjson_options: int = ...,
|
||||
**to_dict_kwargs: Any,
|
||||
) -> bytes: ...
|
||||
def to_json(
|
||||
self: T,
|
||||
encoder: Encoder = orjson.dumps,
|
||||
*,
|
||||
orjson_options: int = ...,
|
||||
**to_dict_kwargs: Any,
|
||||
) -> str: ...
|
||||
@classmethod
|
||||
@final
|
||||
def from_json(
|
||||
cls: Type[T],
|
||||
data: EncodedData,
|
||||
decoder: Decoder = orjson.loads,
|
||||
**from_dict_kwargs: Any,
|
||||
) -> T: ...
|
||||
@@ -0,0 +1,64 @@
|
||||
from collections.abc import Callable
|
||||
from datetime import date, datetime, time
|
||||
from typing import Any, Type, TypeVar, final
|
||||
|
||||
import tomli_w
|
||||
|
||||
from mashumaro.dialect import Dialect
|
||||
from mashumaro.helper import pass_through
|
||||
from mashumaro.mixins.dict import DataClassDictMixin
|
||||
|
||||
try:
|
||||
import tomllib
|
||||
except ModuleNotFoundError:
|
||||
import tomli as tomllib # type: ignore
|
||||
|
||||
T = TypeVar("T", bound="DataClassTOMLMixin")
|
||||
|
||||
|
||||
EncodedData = str
|
||||
Encoder = Callable[[Any], EncodedData]
|
||||
Decoder = Callable[[EncodedData], dict[Any, Any]]
|
||||
|
||||
|
||||
class TOMLDialect(Dialect):
|
||||
no_copy_collections = (list, dict)
|
||||
omit_none = True
|
||||
serialization_strategy = {
|
||||
datetime: pass_through,
|
||||
date: pass_through,
|
||||
time: pass_through,
|
||||
}
|
||||
|
||||
|
||||
class DataClassTOMLMixin(DataClassDictMixin):
|
||||
__slots__ = ()
|
||||
|
||||
__mashumaro_builder_params = {
|
||||
"packer": {
|
||||
"format_name": "toml",
|
||||
"dialect": TOMLDialect,
|
||||
"encoder": tomli_w.dumps,
|
||||
},
|
||||
"unpacker": {
|
||||
"format_name": "toml",
|
||||
"dialect": TOMLDialect,
|
||||
"decoder": tomllib.loads,
|
||||
},
|
||||
}
|
||||
|
||||
@final
|
||||
def to_toml(
|
||||
self: T,
|
||||
encoder: Encoder = tomli_w.dumps,
|
||||
**to_dict_kwargs: Any,
|
||||
) -> EncodedData: ...
|
||||
|
||||
@classmethod
|
||||
@final
|
||||
def from_toml(
|
||||
cls: Type[T],
|
||||
data: EncodedData,
|
||||
decoder: Decoder = tomllib.loads,
|
||||
**from_dict_kwargs: Any,
|
||||
) -> T: ...
|
||||
@@ -0,0 +1,45 @@
|
||||
from collections.abc import Callable
|
||||
from typing import Any, Type, TypeVar, Union
|
||||
|
||||
import yaml
|
||||
|
||||
from mashumaro.mixins.dict import DataClassDictMixin
|
||||
|
||||
T = TypeVar("T", bound="DataClassYAMLMixin")
|
||||
|
||||
|
||||
EncodedData = Union[str, bytes]
|
||||
Encoder = Callable[[Any], EncodedData]
|
||||
Decoder = Callable[[EncodedData], dict[Any, Any]]
|
||||
|
||||
|
||||
DefaultLoader = getattr(yaml, "CSafeLoader", yaml.SafeLoader)
|
||||
DefaultDumper = getattr(yaml, "CDumper", yaml.Dumper)
|
||||
|
||||
|
||||
def default_encoder(data: Any) -> EncodedData:
|
||||
return yaml.dump(data, Dumper=DefaultDumper)
|
||||
|
||||
|
||||
def default_decoder(data: EncodedData) -> dict[Any, Any]:
|
||||
return yaml.load(data, DefaultLoader)
|
||||
|
||||
|
||||
class DataClassYAMLMixin(DataClassDictMixin):
|
||||
__slots__ = ()
|
||||
|
||||
def to_yaml(
|
||||
self: T,
|
||||
encoder: Encoder = default_encoder,
|
||||
**to_dict_kwargs: Any,
|
||||
) -> EncodedData:
|
||||
return encoder(self.to_dict(**to_dict_kwargs))
|
||||
|
||||
@classmethod
|
||||
def from_yaml(
|
||||
cls: Type[T],
|
||||
data: EncodedData,
|
||||
decoder: Decoder = default_decoder,
|
||||
**from_dict_kwargs: Any,
|
||||
) -> T:
|
||||
return cls.from_dict(decoder(data), **from_dict_kwargs)
|
||||
Reference in New Issue
Block a user