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
x
and attributei
, this method first tries to accessx[i]
, and then falls back tox.i
if an exception is raised.Warning
If an object
x
does not raise an exception whenx[i]
does not exist,get_value
will never check the valuex.i
. Consider overridingmarshmallow.fields.Field.get_value
in this case.
- marshmallow.utils.is_collection(obj)[source]¶
Return True if
obj
is a collection type, e.g list, tuple, queryset.
- marshmallow.utils.is_iterable_but_not_string(obj)[source]¶
Return True if
obj
is an iterable object that isn’t a string.
- marshmallow.utils.is_sequence_but_not_string(obj)[source]¶
Return True if
obj
is a sequence that isn’t a string.
- 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]
- marshmallow.utils.set_value(dct, key, value)[source]¶
Set a value in a dict. If
key
contains 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}}
- 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