CoolFace
Apppublic

jawahar-konathala/Tryon2

sourceHugging Facecc-by-nc-sa-4.0updated 2y agoView on Hugging Face
0likes
develop.py60 linesDownload Raw Back to utils
1# Copyright (c) Facebook, Inc. and its affiliates.2""" Utilities for developers only.3These are not visible to users (not automatically imported). And should not4appeared in docs."""5# adapted from https://github.com/tensorpack/tensorpack/blob/master/tensorpack/utils/develop.py6 7 8def create_dummy_class(klass, dependency, message=""):9    """10    When a dependency of a class is not available, create a dummy class which throws ImportError11    when used.12 13    Args:14        klass (str): name of the class.15        dependency (str): name of the dependency.16        message: extra message to print17    Returns:18        class: a class object19    """20    err = "Cannot import '{}', therefore '{}' is not available.".format(dependency, klass)21    if message:22        err = err + " " + message23 24    class _DummyMetaClass(type):25        # throw error on class attribute access26        def __getattr__(_, __):  # noqa: B90227            raise ImportError(err)28 29    class _Dummy(object, metaclass=_DummyMetaClass):30        # throw error on constructor31        def __init__(self, *args, **kwargs):32            raise ImportError(err)33 34    return _Dummy35 36 37def create_dummy_func(func, dependency, message=""):38    """39    When a dependency of a function is not available, create a dummy function which throws40    ImportError when used.41 42    Args:43        func (str): name of the function.44        dependency (str or list[str]): name(s) of the dependency.45        message: extra message to print46    Returns:47        function: a function object48    """49    err = "Cannot import '{}', therefore '{}' is not available.".format(dependency, func)50    if message:51        err = err + " " + message52 53    if isinstance(dependency, (list, tuple)):54        dependency = ",".join(dependency)55 56    def _dummy(*args, **kwargs):57        raise ImportError(err)58 59    return _dummy60