55
66from typing import Union , Any , Mapping
77from contextlib import suppress
8- from enum import Enum
8+ from enum import Enum , Flag
99from inspect import isclass , isfunction , signature , _empty
1010
1111from daf .logging .tracing import TraceLEVELS , trace
1515import copy
1616import asyncio
1717import datetime
18+ import re
1819
1920import _discord as discord
2021
@@ -123,6 +124,10 @@ def import_class(path: str):
123124 "custom_encoder" : lambda object : object .total_seconds (),
124125 "custom_decoder" : lambda seconds : datetime .timedelta (seconds = seconds )
125126 },
127+ datetime .time : {
128+ "custom_encoder" : lambda object : object .isoformat (),
129+ "custom_decoder" : lambda string : datetime .time .fromisoformat (string )
130+ },
126131 bytes : {
127132 "custom_encoder" : lambda data : data .hex (),
128133 "custom_decoder" : lambda hex_str : bytes .fromhex (hex_str )
@@ -146,8 +151,13 @@ def import_class(path: str):
146151 },
147152 discord .VoiceChannel : {
148153 "attrs" : ["name" , "id" ],
154+ },
155+ re .Pattern : {
156+ "custom_encoder" : lambda data : {"pattern" : convert_object_to_semi_dict (data .pattern ), "flags" : data .flags },
157+ "custom_decoder" : lambda data : re .compile (data ["pattern" ], data .get ("flags" , 0 ))
149158 }
150159}
160+
151161"""
152162This is a custom conversion dictionary.
153163It's values are datatypes of objects which cannot be normally converted to JSON, so custom rules are required.
@@ -367,7 +377,7 @@ def _convert_json_slots(to_convert):
367377 to_convert = [convert_object_to_semi_dict (value ) for value in to_convert ]
368378 return to_convert
369379
370- if isinstance (to_convert , Enum ):
380+ if isinstance (to_convert , ( Enum , Flag ) ):
371381 return {"enum_type" : f"{ object_type .__module__ } .{ object_type .__name__ } " , "value" : to_convert .value }
372382
373383 # Class itself, not an actual instance. Can also be function as it only imports.
0 commit comments