def module_attribute(dotpath, default=None, safe=False):
    '''Load an attribute from a module.

    If the module or the attribute is not available, return the default
    argument if *safe* is `True`.
    '''
    if dotpath:
        bits = str(dotpath).split(':')
        try:
            if len(bits) == 2:
                attr = bits[1]
                module_name = bits[0]
            else:
                bits = bits[0].split('.')
                if len(bits) > 1:
                    attr = bits[-1]
                    module_name = '.'.join(bits[:-1])
                else:
                    raise ValueError('Could not find attribute in %s'
                                     % dotpath)

            module = import_module(module_name)
            return getattr(module, attr)
        except Exception:
            if not safe:
                raise
            return default
    else:
        if not safe:
            raise ImportError
        return default
def module_attribute ( dotpath , default = None , safe = False ) : if dotpath : bits = str ( dotpath ) . split ( ':' ) try : if len ( bits ) == 2 : attr = bits [ 1 ] module_name = bits [ 0 ] else : bits = bits [ 0 ] . split ( '.' ) if len ( bits ) > 1 : attr = bits [ - 1 ] module_name = '.' . join ( bits [ : - 1 ] ) else : raise ValueError ( 'Could not find attribute in %s' % dotpath ) module = import_module ( module_name ) return getattr ( module , attr ) except Exception : if not safe : raise return default else : if not safe : raise ImportError return default
Load an attribute from a module.

    If the module or the attribute is not available, return the default
    argument if *safe* is `True`.