utils.py 674 B

1234567891011121314151617181920212223242526272829
  1. import importlib
  2. import io
  3. import os
  4. def import_from_string(imp_str, if_missing="raise"):
  5. try:
  6. return importlib.import_module(imp_str)
  7. except ImportError:
  8. if if_missing == "ignore":
  9. return None
  10. raise
  11. def is_filelike(value):
  12. """Is file-like object or string of file path
  13. See: https://stackoverflow.com/a/1661354/13696660"""
  14. try:
  15. return hasattr(value, "read") or os.path.isfile(value)
  16. except TypeError:
  17. return False
  18. def is_bytes(value):
  19. return isinstance(value, (bytes, bytearray))
  20. def is_pathlike(value):
  21. "Check if the value is path-like"
  22. return isinstance(value, os.PathLike)