utils.py 452 B

12345678910111213141516171819202122
  1. from collections.abc import Iterable
  2. def str_coercible(cls):
  3. def __str__(self):
  4. return self.__unicode__()
  5. cls.__str__ = __str__
  6. return cls
  7. def is_sequence(value):
  8. return (
  9. isinstance(value, Iterable) and not isinstance(value, str)
  10. )
  11. def starts_with(iterable, prefix):
  12. """
  13. Returns whether or not given iterable starts with given prefix.
  14. """
  15. return list(iterable)[0:len(prefix)] == list(prefix)