converters.py 863 B

1234567891011121314151617181920212223242526
  1. # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
  2. # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
  3. def asbool(obj):
  4. if isinstance(obj, (str, unicode)):
  5. obj = obj.strip().lower()
  6. if obj in ['true', 'yes', 'on', 'y', 't', '1']:
  7. return True
  8. elif obj in ['false', 'no', 'off', 'n', 'f', '0']:
  9. return False
  10. else:
  11. raise ValueError(
  12. "String is not true/false: %r" % obj)
  13. return bool(obj)
  14. def aslist(obj, sep=None, strip=True):
  15. if isinstance(obj, (str, unicode)):
  16. lst = obj.split(sep)
  17. if strip:
  18. lst = [v.strip() for v in lst]
  19. return lst
  20. elif isinstance(obj, (list, tuple)):
  21. return obj
  22. elif obj is None:
  23. return []
  24. else:
  25. return [obj]