1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
"""Collection of functions for building custom `json_default` functions.
In general functions come in pairs of `use_x_default` and `x_default`, where the former is used
to determine if you should call the latter.
Most `use_x_default` functions also act as a [`TypeGuard`](https://mypy.readthedocs.io/en/stable/type_narrowing.html#user-defined-type-guards).
"""
### IMPORTS
### ============================================================================
## Future
from __future__ import annotations
## Standard Library
import base64
import dataclasses
import datetime
import enum
import sys
from types import TracebackType
from typing import Any
import traceback
import uuid
if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
from typing_extensions import TypeGuard
## Installed
## Application
### FUNCTIONS
### ============================================================================
def unknown_default(obj: Any) -> str:
"""Backup default function for any object type.
Will attempt to use `str` or `repr`. If both functions error will return
the string `"__could_not_encode__"`.
Args:
obj: object to handle
"""
try:
return str(obj)
except Exception: # pylint: disable=broad-exception-caught
pass
try:
return repr(obj)
except Exception: # pylint: disable=broad-exception-caught
pass
return "__could_not_encode__"
## Types
## -----------------------------------------------------------------------------
def use_type_default(obj: Any) -> TypeGuard[type]:
"""Default check function for `type` objects (aka classes)."""
return isinstance(obj, type)
def type_default(obj: type) -> str:
"""Default function for `type` objects.
Args:
obj: object to handle
"""
return obj.__name__
## Dataclasses
## -----------------------------------------------------------------------------
def use_dataclass_default(obj: Any) -> bool:
"""Default check function for dataclass instances"""
return dataclasses.is_dataclass(obj) and not isinstance(obj, type)
def dataclass_default(obj) -> dict[str, Any]:
"""Default function for dataclass instances
Args:
obj: object to handle
"""
return dataclasses.asdict(obj)
## Dates and Times
## -----------------------------------------------------------------------------
def use_time_default(obj: Any) -> TypeGuard[datetime.time]:
"""Default check function for `datetime.time` instances"""
return isinstance(obj, datetime.time)
def time_default(obj: datetime.time) -> str:
"""Default function for `datetime.time` instances
Args:
obj: object to handle
"""
return obj.isoformat()
def use_date_default(obj: Any) -> TypeGuard[datetime.date]:
"""Default check function for `datetime.date` instances"""
return isinstance(obj, datetime.date)
def date_default(obj: datetime.date) -> str:
"""Default function for `datetime.date` instances
Args:
obj: object to handle
"""
return obj.isoformat()
def use_datetime_default(obj: Any) -> TypeGuard[datetime.datetime]:
"""Default check function for `datetime.datetime` instances"""
return isinstance(obj, datetime.datetime)
def datetime_default(obj: datetime.datetime) -> str:
"""Default function for `datetime.datetime` instances
Args:
obj: object to handle
"""
return obj.isoformat()
def use_datetime_any(obj: Any) -> TypeGuard[datetime.time | datetime.date | datetime.datetime]:
"""Default check function for `datetime` related instances"""
return isinstance(obj, (datetime.time, datetime.date, datetime.datetime))
def datetime_any(obj: datetime.time | datetime.date | datetime.date) -> str:
"""Default function for `datetime` related instances
Args:
obj: object to handle
"""
return obj.isoformat()
## Exception and Tracebacks
## -----------------------------------------------------------------------------
def use_exception_default(obj: Any) -> TypeGuard[BaseException]:
"""Default check function for exception instances.
Exception classes are not treated specially and should be handled by the
`[use_]type_default` functions.
"""
return isinstance(obj, BaseException)
def exception_default(obj: BaseException) -> str:
"""Default function for exception instances
Args:
obj: object to handle
"""
return f"{obj.__class__.__name__}: {obj}"
def use_traceback_default(obj: Any) -> TypeGuard[TracebackType]:
"""Default check function for tracebacks"""
return isinstance(obj, TracebackType)
def traceback_default(obj: TracebackType) -> str:
"""Default function for tracebacks
Args:
obj: object to handle
"""
return "".join(traceback.format_tb(obj)).strip()
## Enums
## -----------------------------------------------------------------------------
def use_enum_default(obj: Any) -> TypeGuard[enum.Enum | enum.EnumMeta]:
"""Default check function for enums.
Supports both enum classes and enum values.
"""
return isinstance(obj, (enum.Enum, enum.EnumMeta))
def enum_default(obj: enum.Enum | enum.EnumMeta) -> Any | list[Any]:
"""Default function for enums.
Supports both enum classes and enum values.
Args:
obj: object to handle
"""
if isinstance(obj, enum.Enum):
return obj.value
return [e.value for e in obj] # type: ignore[var-annotated]
## UUIDs
## -----------------------------------------------------------------------------
def use_uuid_default(obj: Any) -> TypeGuard[uuid.UUID]:
"""Default check function for `uuid.UUID` instances"""
return isinstance(obj, uuid.UUID)
def uuid_default(obj: uuid.UUID) -> str:
"""Default function for `uuid.UUID` instances
Formats the UUID using "hyphen" format.
Args:
obj: object to handle
"""
return str(obj)
## Bytes
## -----------------------------------------------------------------------------
def use_bytes_default(obj: Any) -> TypeGuard[bytes | bytearray]:
"""Default check function for bytes"""
return isinstance(obj, (bytes, bytearray))
def bytes_default(obj: bytes | bytearray, url_safe: bool = True) -> str:
"""Default function for bytes
Args:
obj: object to handle
url_safe: use URL safe base 64 character set.
Returns:
The byte data as a base 64 string.
"""
if url_safe:
return base64.urlsafe_b64encode(obj).decode("utf8")
return base64.b64encode(obj).decode("utf8")
|