Utility functions¶
Utility methods for marshmallow.
- marshmallow.utils.callable_or_raise(obj)[source]¶
Check that an object is callable, else raise a
TypeError.
- marshmallow.utils.get_value(obj, key, default=<marshmallow.missing>)[source]¶
Helper for pulling a keyed value off various types of objects. Fields use this method by default to access attributes of the source object. For object
xand attributei, this method first tries to accessx[i], and then falls back tox.iif an exception is raised.Warning
If an object
xdoes not raise an exception whenx[i]does not exist,get_valuewill never check the valuex.i. Consider overridingmarshmallow.fields.Field.get_valuein this case.- Parameters:
key (int | str)
- marshmallow.utils.is_collection(obj)[source]¶
Return True if
objis a collection type, e.g list, tuple, queryset.- Return type:
TypeGuard[Iterable]
- marshmallow.utils.is_generator(obj)[source]¶
Return True if
objis a generator- Return type:
TypeGuard[Generator]
- marshmallow.utils.is_iterable_but_not_string(obj)[source]¶
Return True if
objis an iterable object that isn’t a string.- Return type:
TypeGuard[Iterable]
- marshmallow.utils.is_sequence_but_not_string(obj)[source]¶
Return True if
objis a sequence that isn’t a string.- Return type:
TypeGuard[Sequence]
- marshmallow.utils.pluck(dictlist, key)[source]¶
Extracts a list of dictionary values from a list of dictionaries.
>>> dlist = [{'id': 1, 'name': 'foo'}, {'id': 2, 'name': 'bar'}] >>> pluck(dlist, 'id') [1, 2]
- Parameters:
dictlist (list[dict[str, Any]])
key (str)
- marshmallow.utils.set_value(dct, key, value)[source]¶
Set a value in a dict. If
keycontains a ‘.’, it is assumed be a path (i.e. dot-delimited string) to the value’s location.>>> d = {} >>> set_value(d, 'foo.bar', 42) >>> d {'foo': {'bar': 42}}
- Parameters:
dct (dict[str, Any])
key (str)
value (Any)
- marshmallow.utils.timedelta_to_microseconds(value)[source]¶
Compute the total microseconds of a timedelta.
https://github.com/python/cpython/blob/v3.13.1/Lib/_pydatetime.py#L805-L807
- Parameters:
value (timedelta)
- Return type:
int