helpers.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. def prettify_name(name):
  2. """
  3. Prettify pythonic variable name.
  4. For example, 'hello_world' will be converted to 'Hello World'
  5. :param name:
  6. Name to prettify
  7. """
  8. return name.replace('_', ' ').title()
  9. def get_mdict_item_or_list(mdict, key):
  10. """
  11. Return the value for the given key of the multidict.
  12. A werkzeug.datastructures.multidict can have a single
  13. value or a list of items. If there is only one item,
  14. return only this item, else the whole list as a tuple
  15. :param mdict: Multidict to search for the key
  16. :type mdict: werkzeug.datastructures.multidict
  17. :param key: key to look for
  18. :return: the value for the key or None if the Key has not be found
  19. """
  20. if hasattr(mdict, 'getlist'):
  21. v = mdict.getlist(key)
  22. if len(v) == 1:
  23. value = v[0]
  24. # Special case for empty strings, treat them as "no-value"
  25. if value == '':
  26. value = None
  27. return value
  28. elif len(v) == 0:
  29. return None
  30. else:
  31. return tuple(v)
  32. return None