MCPcopy Index your code
hub / github.com/data-apis/python-record-api / preprocess

Function preprocess

record_api/core.py:163–192  ·  view source on GitHub ↗

Preserves lists, strings, and None. Tuples and dicts are recursed. All others primitive subclasses are replaced with their types. All non primitives subclasses are passed through to be handled by `default`.

(o)

Source from the content-addressed store, hash-verified

161
162
163def preprocess(o):
164 """
165 Preserves lists, strings, and None. Tuples and dicts are recursed.
166
167 All others primitive subclasses are replaced with their types.
168
169 All non primitives subclasses are passed through to be handled by `default`.
170 """
171 tp = type(o)
172 if tp == str and len(o) <= MAX_STRING:
173 return o
174 if tp == list:
175 return [preprocess(v) for v in o[:MAX_LIST]]
176 if tp == dict:
177 return {
178 "t": "dict",
179 "v": [
180 [preprocess(k), preprocess(v)] for k, v in list(o.items())[:MAX_DICT]
181 ],
182 }
183 if tp == tuple:
184 return {
185 "t": "tuple",
186 "v": [preprocess(v) for v in o[:MAX_TUPLE]],
187 }
188 if isinstance(o, (int, float, bool, list, str, dict, tuple)):
189 # Don't send literals of these
190 return {"t": encode_module_value(tp)}
191 # Other types will be encoded by the `default` function.
192 return o
193
194
195def default(o: object) -> object:

Callers 5

_encode_sliceFunction · 0.85
_encode_methodFunction · 0.85
createMethod · 0.85
log_callFunction · 0.85

Calls 2

encode_module_valueFunction · 0.85
itemsMethod · 0.45

Tested by

no test coverage detected