Skip to content

Instantly share code, notes, and snippets.

@zeph1rus
Last active April 21, 2021 23:07
Show Gist options
  • Save zeph1rus/d32549e7dae38dbdaef3592e3533b3cf to your computer and use it in GitHub Desktop.
Save zeph1rus/d32549e7dae38dbdaef3592e3533b3cf to your computer and use it in GitHub Desktop.
from werkzeug.datastructures import ImmutableMultiDict
def count_werkzeug_multi_dict(multi_dict: ImmutableMultiDict) -> int:
"""
Counts all of the possible key/value pairs in a multidict, as they have no __len__ implementation, for example
Flask's request.files attribute (in case you want to know how many files got attached to a request
:param multi_dict: A MultiDict object from werkzeug, such as ImmutableMultiDict, should work for it's mutable
version too.
:type multi_dict: ImmutableMultiDict
:return: Returns the count of possible kv pairs (including duplicate keys) as an int
:rtype: int
"""
i: int = 0
for multi_dict_key in multi_dict.keys():
i += len(multi_dict.getlist(multi_dict_key))
return i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment