is_iterable.py 829 B

12345678910111213141516171819202122232425262728293031
  1. from typing import (
  2. Any,
  3. ByteString,
  4. Collection,
  5. Iterable,
  6. Mapping,
  7. Text,
  8. ValuesView,
  9. )
  10. __all__ = ["is_collection", "is_iterable"]
  11. collection_types: Any = Collection
  12. if not isinstance({}.values(), Collection): # Python < 3.7.2
  13. collection_types = (Collection, ValuesView)
  14. iterable_types: Any = Iterable
  15. not_iterable_types: Any = (ByteString, Mapping, Text)
  16. def is_collection(value: Any) -> bool:
  17. """Check if value is a collection, but not a string or a mapping."""
  18. return isinstance(value, collection_types) and not isinstance(
  19. value, not_iterable_types
  20. )
  21. def is_iterable(value: Any) -> bool:
  22. """Check if value is an iterable, but not a string or a mapping."""
  23. return isinstance(value, iterable_types) and not isinstance(
  24. value, not_iterable_types
  25. )