"""Preconfigured converters for msgpack."""fromdatetimeimportdate,datetime,time,timezonefromtypingimportAny,Type,TypeVar,Unionfrommsgpackimportdumps,loadsfromcattrs._compatimportAbstractSetfrom..convertersimportBaseConverter,Converterfrom..strategiesimportconfigure_union_passthroughfrom.importwrapT=TypeVar("T")
[ドキュメント]defconfigure_converter(converter:BaseConverter):""" Configure the converter for use with the msgpack library. * datetimes are serialized as timestamp floats * sets are serialized as lists """converter.register_unstructure_hook(datetime,lambdav:v.timestamp())converter.register_structure_hook(datetime,lambdav,_:datetime.fromtimestamp(v,timezone.utc))converter.register_unstructure_hook(date,lambdav:datetime.combine(v,time(tzinfo=timezone.utc)).timestamp())converter.register_structure_hook(date,lambdav,_:datetime.fromtimestamp(v,timezone.utc).date())configure_union_passthrough(Union[str,bool,int,float,None,bytes],converter)