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.from_iso_date(value)[source]

Parse a string and return a datetime.date.

marshmallow.utils.from_iso_datetime(value)[source]

Parse a string and return a datetime.datetime.

This function supports time zone offsets. When the input contains one, the output uses a timezone with a fixed offset from UTC.

marshmallow.utils.from_iso_time(value)[source]

Parse a string and return a datetime.time.

This function doesn’t support time zone offsets.

marshmallow.utils.from_rfc(datestring)[source]

Parse a RFC822-formatted datetime string and return a datetime object.

https://stackoverflow.com/questions/885015/how-to-parse-a-rfc-2822-date-time-into-a-python-datetime # noqa: B950

Parameters:

datestring (str)

Return type:

datetime

marshmallow.utils.get_fixed_timezone(offset)[source]

Return a tzinfo instance with a fixed offset from UTC.

Parameters:

offset (float | timedelta)

Return type:

timezone

marshmallow.utils.get_func_args(func)[source]

Given a callable, return a list of argument names. Handles functools.partial objects and class-based callables.

Changed in version 3.0.0a1: Do not return bound arguments, eg. self.

Parameters:

func (Callable)

Return type:

list[str]

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 attribute i, this method first tries to access x[i], and then falls back to x.i if an exception is raised.

Warning

If an object x does not raise an exception when x[i] does not exist, get_value will never check the value x.i. Consider overriding marshmallow.fields.Field.get_value in this case.

Parameters:

key (int | str)

marshmallow.utils.is_collection(obj)[source]

Return True if obj is a collection type, e.g list, tuple, queryset.

Return type:

bool

marshmallow.utils.is_generator(obj)[source]

Return True if obj is a generator

Return type:

bool

marshmallow.utils.is_instance_or_subclass(val, class_)[source]

Return True if val is either a subclass or instance of class_.

Return type:

bool

marshmallow.utils.is_iterable_but_not_string(obj)[source]

Return True if obj is an iterable object that isn’t a string.

Return type:

bool

marshmallow.utils.is_keyed_tuple(obj)[source]

Return True if obj has keyed tuple behavior, such as namedtuples or SQLAlchemy’s KeyedTuples.

Return type:

bool

marshmallow.utils.isoformat(datetime)[source]

Return the ISO8601-formatted representation of a datetime object.

Parameters:

datetime (datetime) – The datetime.

Return type:

str

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:
marshmallow.utils.pprint(obj, *args, **kwargs)[source]

Pretty-printing function that can pretty-print OrderedDicts like regular dictionaries. Useful for printing the output of marshmallow.Schema.dump().

Deprecated since version 3.7.0: marshmallow.pprint will be removed in marshmallow 4.

Return type:

None

marshmallow.utils.resolve_field_instance(cls_or_instance)[source]

Return a field instance from a field class or instance.

Parameters:

cls_or_instance (type[Field] | Field) – Field class or instance.

Return type:

Field

marshmallow.utils.rfcformat(datetime)[source]

Return the RFC822-formatted representation of a datetime object.

Parameters:

datetime (datetime) – The datetime.

Return type:

str

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}}
Parameters:
marshmallow.utils.timedelta_to_microseconds(value)[source]

Compute the total microseconds of a timedelta

https://github.com/python/cpython/blob/bb3e0c240bc60fe08d332ff5955d54197f79751c/Lib/datetime.py#L665-L667 # noqa: B950

Parameters:

value (timedelta)

Return type:

int