pypet/utils/dynamicimports.py
def create_class(class_name, dynamic_imports):
    """Dynamically creates a class.

    It is tried if the class can be created by the already given imports.
    If not the list of the dynamically loaded classes is used.

    """
    try:
        new_class = globals()[class_name]

        if not inspect.isclass(new_class):
            raise TypeError('Not a class!')

        return new_class
    except (KeyError, TypeError):
        for dynamic_class in dynamic_imports:
            # Dynamic classes can be provided directly as a Class instance,
            # for example as `MyCustomParameter`,
            # or as a string describing where to import the class from,
            # for instance as `'mypackage.mymodule.MyCustomParameter'`.
            if inspect.isclass(dynamic_class):
                if class_name == dynamic_class.__name__:
                    return dynamic_class
            else:
                # The class name is always the last in an import string,
                # e.g. `'mypackage.mymodule.MyCustomParameter'`
                class_name_to_test = dynamic_class.split('.')[-1]
                if class_name == class_name_to_test:
                    new_class = load_class(dynamic_class)
                    return new_class
        raise ImportError('Could not create the class named `%s`.' % class_name)
def create_class(class_name, dynamic_imports):
    """Dynamically creates a class.

    It is tried if the class can be created by the already given imports.
    If not the list of the dynamically loaded classes is used.

    """
    try:
        new_class = globals()[class_name]

        if not inspect.isclass(new_class):
            raise TypeError('Not a class!')

        return new_class
    except (KeyError, TypeError):
        for dynamic_class in dynamic_imports:
            # Dynamic classes can be provided directly as a Class instance,
            # for example as `MyCustomParameter`,
            # or as a string describing where to import the class from,
            # for instance as `'mypackage.mymodule.MyCustomParameter'`.
            if inspect.isclass(dynamic_class):
                if class_name == dynamic_class.__name__:
                    return dynamic_class
            else:
                # The class name is always the last in an import string,
                # e.g. `'mypackage.mymodule.MyCustomParameter'`
                class_name_to_test = dynamic_class.split('.')[-1]
                if class_name == class_name_to_test:
                    new_class = load_class(dynamic_class)
                    return new_class
        raise ImportError('Could not create the class named `%s`.' % class_name)
def create_class ( class_name , dynamic_imports ) : try : new_class = globals ( ) [ class_name ] if not inspect . isclass ( new_class ) : raise TypeError ( 'Not a class!' ) return new_class except ( KeyError , TypeError ) : for dynamic_class in dynamic_imports : # Dynamic classes can be provided directly as a Class instance, # for example as `MyCustomParameter`, # or as a string describing where to import the class from, # for instance as `'mypackage.mymodule.MyCustomParameter'`. if inspect . isclass ( dynamic_class ) : if class_name == dynamic_class . __name__ : return dynamic_class else : # The class name is always the last in an import string, # e.g. `'mypackage.mymodule.MyCustomParameter'` class_name_to_test = dynamic_class . split ( '.' ) [ - 1 ] if class_name == class_name_to_test : new_class = load_class ( dynamic_class ) return new_class raise ImportError ( 'Could not create the class named `%s`.' % class_name )
Dynamically creates a class.

    It is tried if the class can be created by the already given imports.
    If not the list of the dynamically loaded classes is used.
Dynamically creates a class .