unpackAI / unpackai

The Unpack.AI library
https://www.unpackai.com
MIT License
20 stars 4 forks source link

šŸ§® Provide a function `ls` #44

Closed jfthuong closed 3 years ago

jfthuong commented 3 years ago

fastai provides a method ls in the enhanced Path.

We could have a separate ls function that would work outside of this Path.

Actually, the prototype of the function is:

ls(self: pathlib.Path, n_max=None, file_type=None, file_exts=None)

But as usual, the help of fastai is very esoteric and we don't know what is file_type or file_exts:

@patch
def ls(self:Path, n_max=None, file_type=None, file_exts=None):
    "Contents of path as a list"
    extns=L(file_exts)
    if file_type: extns += L(k for k,v in mimetypes.types_map.items() if v.startswith(file_type+'/'))
    has_extns = len(extns)==0
    res = (o for o in self.iterdir() if has_extns or o.suffix in extns)
    if n_max is not None: res = itertools.islice(res, n_max)
    return L(res)

Moreover, the function is not imported when you do import * but it's rather attached to Path.

Hence the idea to have a dedicated simpler function with following prototype:

ā£def ls(path:PathStr, extensions: Optional[Union[str, List[str]]], filter_images=False, list_dir=False) -> List[Path]:
    """Return a list of files from a given path, with optional filtering of images and optional listing of directories
    Args:
        path: root path to look for files (and directories if `list_dir` is True)
        extensions: optional one extension or list of extensions for the files (default: None => all files)
        filter_images: only return path of images if set to True (default: False)
        list_dir: also return list of directories if set to True
    """

Does that sound useful, @raynardj ?

jfthuong commented 3 years ago

Note: actually Path.ls will support following types (file_type):

{'text', 'audio', 'video', 'Application', 'image', 'message', 'application'}
jfthuong commented 3 years ago

Alternative: