turbo_broccoli.custom
Custom type encoder and decoders, all grouped in a dedicated submodule
1# pylint: disable=bare-except 2"""Custom type encoder and decoders, all grouped in a dedicated submodule""" 3 4from typing import Any, Callable 5 6from turbo_broccoli.context import Context 7from turbo_broccoli.custom import bytes as _bytes 8from turbo_broccoli.custom import collections as _collections 9from turbo_broccoli.custom import dataclass as _dataclass 10from turbo_broccoli.custom import datetime as _datetime 11from turbo_broccoli.custom import dct as _dict 12from turbo_broccoli.custom import embedded as _embedded 13from turbo_broccoli.custom import external as _external 14from turbo_broccoli.custom import generic as _generic 15from turbo_broccoli.custom import networkx as _networkx 16from turbo_broccoli.custom import pathlib as _pathlib 17from turbo_broccoli.custom import uuid as _uuid 18 19try: 20 from turbo_broccoli.custom import keras as _keras 21 22 HAS_KERAS = True 23except: 24 HAS_KERAS = False 25 26try: 27 from turbo_broccoli.custom import numpy as _numpy 28 29 HAS_NUMPY = True 30except: 31 HAS_NUMPY = False 32 33try: 34 from turbo_broccoli.custom import pandas as _pandas 35 36 HAS_PANDAS = True 37except: 38 HAS_PANDAS = False 39 40 41try: 42 from turbo_broccoli.custom import secret as _secret 43 44 HAS_SECRET = True 45except: 46 HAS_SECRET = False 47 48try: 49 from turbo_broccoli.custom import tensorflow as _tensorflow 50 51 HAS_TENSORFLOW = True 52except: 53 HAS_TENSORFLOW = False 54 55try: 56 from turbo_broccoli.custom import pytorch as _pytorch 57 58 HAS_PYTORCH = True 59except: 60 HAS_PYTORCH = False 61 62try: 63 from turbo_broccoli.custom import scipy as _scipy 64 65 HAS_SCIPY = True 66except: 67 HAS_SCIPY = False 68 69try: 70 from turbo_broccoli.custom import sklearn as _sklearn 71 72 HAS_SKLEARN = True 73except: 74 HAS_SKLEARN = False 75 76try: 77 from turbo_broccoli.custom import bokeh as _bokeh 78 79 HAS_BOKEH = True 80except: 81 HAS_BOKEH = False 82 83 84def get_decoders() -> dict[str, Callable[[dict, Context], Any]]: 85 """ 86 Returns the dict of all available decoders, which looks like this: 87 88 ```py 89 { 90 "mytype": mytype_decoder, 91 ... 92 } 93 ``` 94 95 `mytype_decoder` is a function that takes an vanilla JSON dict that 96 looks like this (excluding comments): 97 98 ```py 99 { 100 "__type__": "mytype.mysubtype", # or simply "mytype" 101 "__version__": <int>, 102 ... 103 } 104 ``` 105 """ 106 decoders: dict[str, Callable[[dict, Context], Any]] = { 107 "bytes": _bytes.from_json, 108 "datetime": _datetime.from_json, 109 "dict": _dict.from_json, 110 "external": _external.from_json, 111 "networkx": _networkx.from_json, 112 "pathlib": _pathlib.from_json, 113 "uuid": _uuid.from_json, 114 } 115 if HAS_KERAS: 116 decoders["keras"] = _keras.from_json 117 if HAS_NUMPY: 118 decoders["numpy"] = _numpy.from_json 119 if HAS_PANDAS: 120 decoders["pandas"] = _pandas.from_json 121 if HAS_PYTORCH: 122 decoders["pytorch"] = _pytorch.from_json 123 if HAS_SECRET: 124 decoders["secret"] = _secret.from_json 125 if HAS_TENSORFLOW: 126 decoders["tensorflow"] = _tensorflow.from_json 127 if HAS_SCIPY: 128 decoders["scipy"] = _scipy.from_json 129 if HAS_SKLEARN: 130 decoders["sklearn"] = _sklearn.from_json 131 if HAS_BOKEH: 132 decoders["bokeh"] = _bokeh.from_json 133 # Intentionally put last 134 decoders["collections"] = _collections.from_json 135 decoders["dataclass"] = _dataclass.from_json 136 decoders["embedded"] = _embedded.from_json 137 return decoders 138 139 140def get_encoders() -> list[Callable[[Any, Context], dict]]: 141 """ 142 Returns the dict of all available encoder. An encoder is a function that 143 takes an object and returns a readily vanilla JSON-serializable dict. This 144 this should be of the form 145 146 ```py 147 { 148 "__type__": "mytype.mysubtype", # or simply "mytype" 149 "__version__": <int>, 150 ... 151 } 152 ``` 153 154 The encoder should raise a `turbo_broccoli.utils.TypeNotSupported` if it 155 doesn't handle the kind of object it was given. 156 """ 157 encoders: list[Callable[[Any, Context], dict]] = [ 158 _bytes.to_json, 159 _datetime.to_json, 160 _dict.to_json, 161 _external.to_json, 162 _networkx.to_json, 163 _pathlib.to_json, 164 _uuid.to_json, 165 ] 166 if HAS_KERAS: 167 encoders.append(_keras.to_json) 168 if HAS_NUMPY: 169 encoders.append(_numpy.to_json) 170 if HAS_PANDAS: 171 encoders.append(_pandas.to_json) 172 if HAS_PYTORCH: 173 encoders.append(_pytorch.to_json) 174 if HAS_SECRET: 175 encoders.append(_secret.to_json) 176 if HAS_TENSORFLOW: 177 encoders.append(_tensorflow.to_json) 178 if HAS_SCIPY: 179 encoders.append(_scipy.to_json) 180 if HAS_SKLEARN: 181 encoders.append(_sklearn.to_json) 182 if HAS_BOKEH: 183 encoders.append(_bokeh.to_json) 184 # Intentionally put last 185 encoders += [ 186 _collections.to_json, 187 _dataclass.to_json, 188 _generic.to_json, 189 _embedded.to_json, 190 ] 191 return encoders
def
get_decoders() -> dict[str, typing.Callable[[dict, turbo_broccoli.context.Context], typing.Any]]:
85def get_decoders() -> dict[str, Callable[[dict, Context], Any]]: 86 """ 87 Returns the dict of all available decoders, which looks like this: 88 89 ```py 90 { 91 "mytype": mytype_decoder, 92 ... 93 } 94 ``` 95 96 `mytype_decoder` is a function that takes an vanilla JSON dict that 97 looks like this (excluding comments): 98 99 ```py 100 { 101 "__type__": "mytype.mysubtype", # or simply "mytype" 102 "__version__": <int>, 103 ... 104 } 105 ``` 106 """ 107 decoders: dict[str, Callable[[dict, Context], Any]] = { 108 "bytes": _bytes.from_json, 109 "datetime": _datetime.from_json, 110 "dict": _dict.from_json, 111 "external": _external.from_json, 112 "networkx": _networkx.from_json, 113 "pathlib": _pathlib.from_json, 114 "uuid": _uuid.from_json, 115 } 116 if HAS_KERAS: 117 decoders["keras"] = _keras.from_json 118 if HAS_NUMPY: 119 decoders["numpy"] = _numpy.from_json 120 if HAS_PANDAS: 121 decoders["pandas"] = _pandas.from_json 122 if HAS_PYTORCH: 123 decoders["pytorch"] = _pytorch.from_json 124 if HAS_SECRET: 125 decoders["secret"] = _secret.from_json 126 if HAS_TENSORFLOW: 127 decoders["tensorflow"] = _tensorflow.from_json 128 if HAS_SCIPY: 129 decoders["scipy"] = _scipy.from_json 130 if HAS_SKLEARN: 131 decoders["sklearn"] = _sklearn.from_json 132 if HAS_BOKEH: 133 decoders["bokeh"] = _bokeh.from_json 134 # Intentionally put last 135 decoders["collections"] = _collections.from_json 136 decoders["dataclass"] = _dataclass.from_json 137 decoders["embedded"] = _embedded.from_json 138 return decoders
Returns the dict of all available decoders, which looks like this:
{
"mytype": mytype_decoder,
...
}
mytype_decoder
is a function that takes an vanilla JSON dict that
looks like this (excluding comments):
{
"__type__": "mytype.mysubtype", # or simply "mytype"
"__version__": <int>,
...
}
141def get_encoders() -> list[Callable[[Any, Context], dict]]: 142 """ 143 Returns the dict of all available encoder. An encoder is a function that 144 takes an object and returns a readily vanilla JSON-serializable dict. This 145 this should be of the form 146 147 ```py 148 { 149 "__type__": "mytype.mysubtype", # or simply "mytype" 150 "__version__": <int>, 151 ... 152 } 153 ``` 154 155 The encoder should raise a `turbo_broccoli.utils.TypeNotSupported` if it 156 doesn't handle the kind of object it was given. 157 """ 158 encoders: list[Callable[[Any, Context], dict]] = [ 159 _bytes.to_json, 160 _datetime.to_json, 161 _dict.to_json, 162 _external.to_json, 163 _networkx.to_json, 164 _pathlib.to_json, 165 _uuid.to_json, 166 ] 167 if HAS_KERAS: 168 encoders.append(_keras.to_json) 169 if HAS_NUMPY: 170 encoders.append(_numpy.to_json) 171 if HAS_PANDAS: 172 encoders.append(_pandas.to_json) 173 if HAS_PYTORCH: 174 encoders.append(_pytorch.to_json) 175 if HAS_SECRET: 176 encoders.append(_secret.to_json) 177 if HAS_TENSORFLOW: 178 encoders.append(_tensorflow.to_json) 179 if HAS_SCIPY: 180 encoders.append(_scipy.to_json) 181 if HAS_SKLEARN: 182 encoders.append(_sklearn.to_json) 183 if HAS_BOKEH: 184 encoders.append(_bokeh.to_json) 185 # Intentionally put last 186 encoders += [ 187 _collections.to_json, 188 _dataclass.to_json, 189 _generic.to_json, 190 _embedded.to_json, 191 ] 192 return encoders
Returns the dict of all available encoder. An encoder is a function that takes an object and returns a readily vanilla JSON-serializable dict. This this should be of the form
{
"__type__": "mytype.mysubtype", # or simply "mytype"
"__version__": <int>,
...
}
The encoder should raise a turbo_broccoli.utils.TypeNotSupported
if it
doesn't handle the kind of object it was given.