def getobject(fullname): """todo""" if not fullname: return # todo: validate pkgname.modname.object.key names = fullname.split(".") module = None for i, _ in enumerate(names): name = ".".join(names[0:i + 1]) if find_module(name): module = load_module(name) if i == len(names) - 1: return module else: break if not module: # module not found # __builtins__ object ? if hasattr(builtins, fullname): return getattr(builtins, fullname) # module ? if not module: # raise ImportError(fullname) return if objectname(module, fullname=True).lower() == fullname.lower(): return module # module.object ? obj = module for i in range(i, len(names)): attr = names[i] if hasattr(obj, attr): obj = getattr(obj, attr) if i == len(names) - 1: return obj
def formatfunction(function, name=True, fullname=True, formatvalue=formatvalue.formatvalue, firstarg=True): """todo""" _id = "" if name: _id = objectname(function, fullname=fullname) try: argspec = inspect.getargspec(function) except Exception: # TypeError: <built-in function chdir> is not a Python function # python2 # python3 OK # but TypeError not catched :( return "%s()" % str(_id) if not formatvalue: def _formatvalue(value): return "" formatvalue = _formatvalue args = argspec.args if not firstarg: cls = getclass(function) if inspect.isclass(function) or cls: args = args[1:] result = inspect.formatargspec( args, *argspec[1:], formatvalue=formatvalue ) # no space = .replace(", ",",") return str(_id + result)
def pkgname(obj): """ return object package """ fullname = objectname(obj, fullname=True) if fullname: return fullname.split(".")[0]
def _err_msg(func, args, pos, required_types): name = objectname(func, fullname=True) lines = [ "%s() argument #%s is not instance of %s" % (name, pos, required_types) ] lines += ["args: %s" % str(list(args))] lines += ["arg#%s: %s" % (pos, args[pos])] return "\n".join(lines)
def formatbases(cls, fullname=True): __bases__ = cls.__bases__ if not __bases__: return "" clsnames = [] for base in __bases__: module = inspect.getmodule(base) if cls.__module__ == module: clsnames.append(base.__name__) else: name = objectname(base, fullname=fullname) clsnames.append(name) return "(%s)" % ",".join(clsnames)
def getattr_info(obj, name): # no default :) but we can show more info for debug fullname = objectname(obj, fullname=True) _type = gettype(obj) err = None # todo: isgenerator, istraceback, etc... if inspect.isbuiltin(obj) and inspect.isroutine(obj): _type = "builtin function" fullname = fullname.split(".")[-1] if isobject(obj) and getclass(obj) is not None: # default: 'clsname' object has no attribute 'name' cls = getclass(obj) if cls: fullname = objectname(cls, fullname=True) err = "%s object has no attribute '%s'" % (fullname, name) # _type = "object" if _type: err = "%s '%s' has no attribute '%s'" % (_type, fullname, name) if err: raise AttributeError(err) # default builtins.getattr(obj, name)
def formatvalue(value): if value is None: return "=None" if isstring(value): return "='%s'" % value.replace("\n", "").replace("/", ":") if inspect.isclass(value): if inspect.getmodule(value) != builtins: return "=%s" % objectname(value, fullname=True) else: return "=%s" % value.__name__ if isfilehandler(value): value = value.name if isregex(value): value = 'r"%s"' % value.pattern if "instance at" in str(value) or "object at" in str(value): cls = objectname(value.__class__, fullname=True) cls = cls.replace("__builtin__.", "") value = "<%s instance>" % cls if inspect.isfunction(value): if inspect.isbuiltin(value): return "=%s" % value.__name__ else: return "=%s" % objectname(value, fullname=True) return "=%s" % str(value)
def __str__(self): return "<%s object '%s'>" % (objectname(self.__class__, fullname=True), self.path)
#!/usr/bin/env python from objectname import objectname class CLS: def method(self): pass name = objectname(CLS.method) print(name) name = objectname(CLS.method, fullname=True) print(name)