| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import os
- from typing import Callable, Generic, TypeVar
- T = TypeVar("T")
- class cached_property(Generic[T]):
- """
- cached-property from https://github.com/pydanny/cached-property
- Add typing support
- A property that is only computed once per instance and then replaces itself
- with an ordinary attribute. Deleting the attribute resets the property.
- Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
- """
- def __init__(self, func: Callable[..., T]):
- self.func = func
- def __get__(self, obj, cls) -> T:
- if obj is None:
- return self
- value = obj.__dict__[self.func.__name__] = self.func(obj)
- return value
- def iter_folder(folder, is_dir=False, ext=None):
- """
- Args:
- folder (str):
- is_dir (bool): True to iter directories only
- ext (str): File extension, such as `.yaml`
- Yields:
- str: Absolute path of files
- """
- try:
- files = os.listdir(folder)
- except FileNotFoundError:
- return
- for file in files:
- sub = os.path.join(folder, file)
- if is_dir:
- if os.path.isdir(sub):
- yield sub.replace('\\\\', '/').replace('\\', '/')
- elif ext is not None:
- if not os.path.isdir(sub):
- _, extension = os.path.splitext(file)
- if extension == ext:
- yield os.path.join(folder, file).replace('\\\\', '/').replace('\\', '/')
- else:
- yield os.path.join(folder, file).replace('\\\\', '/').replace('\\', '/')
|