コード例 #1
0
ファイル: names.py プロジェクト: merchise-autrement/xoutil
def module_name(item):
    '''Returns the full module name where the given object is declared.

    Examples::

       >>> module_name(module_name)
       'xoutil.names'

       >>> from xoutil import Unset
       >>> module_name(Unset)
       'xoutil.logical'

    '''
    from xoutil.inspect import get_attr_value
    if item is None:
        res = ''
    elif isinstance(item, base_string):
        res = item
    else:
        res = get_attr_value(item, '__module__', None)
        if res is None:
            res = get_attr_value(type(item), '__module__', '')
    if res.startswith('__') or res in ('builtins', 'exceptions', '<module>'):
        res = ''
    return str(res)
コード例 #2
0
ファイル: objects.py プロジェクト: olemis/xoutil
def fulldir(obj):
    '''Return a set with all attribute names defined in `obj`'''
    from xoutil.inspect import get_attr_value
    res = set()
    if isinstance(obj, type):
        for cls in type.mro(obj):
            res |= set(get_attr_value(cls, '__dict__', {}))
    else:
        res |= set(get_attr_value(obj, '__dict__', {}))
    cls = type(obj)
    if cls is not type:
        res |= set(dir(cls))
    return res
コード例 #3
0
ファイル: objects.py プロジェクト: olemis/xoutil
 def __get__(self, obj, owner):
     if obj is not None:
         from xoutil.inspect import get_attr_value
         res = get_attr_value(obj, self.inner_name, Unset)
         if res is not Unset:
             return res
         elif self.init is not Unset:
             try:
                 res = self.init()
             except:
                 print('>>>', self.init, '::', type(self.init))
                 raise
             self.__set__(obj, res)
             return res
         elif self.default is not Unset:
             res = self.default
             self.__set__(obj, res)
             return res
         else:
             from xoutil.eight import typeof
             msg = "'%s' object has no attribute '%s'"
             raise AttributeError(msg % (typeof(obj).__name__,
                                         self.attr_name))
     else:
         return self
コード例 #4
0
ファイル: names.py プロジェクト: merchise-autrement/xoutil
def identifier_from(*args):
    '''Build an valid identifier from the name extracted from an object.

    .. versionadded:: 1.5.6

    First, check if argument is a type and then returns the name of the type
    prefixed with `_` if valid; otherwise calls `nameof` function repeatedly
    until a valid identifier is found using the following order logic:
    ``inner=True``, without arguments looking-up a variable in the calling
    stack, and ``typed=True``.  Returns None if no valid value is found.

    Examples::

        >>> identifier_from({})
        'dict'

    '''
    if len(args) == 1:
        from xoutil.validators.identifiers import is_valid_identifier as valid
        from xoutil.inspect import get_attr_value
        res = None
        if isinstance(args[0], type):
            aux = get_attr_value(args[0], '__name__', None)
            if valid(aux):
                res = str('_%s' % aux)
        if res is None:
            tests = ({'inner': True}, {}, {'typed': True})
            names = (nameof(args[0], depth=2, **test) for test in tests)
            res = next((name for name in names if valid(name)), None)
        return res
    else:
        msg = 'identifier_from() takes exactly 1 argument (%s given)'
        raise TypeError(msg % len(args))
コード例 #5
0
ファイル: names.py プロジェクト: merchise-autrement/xoutil
def _get_mappings(source):
    '''Return a sequence of mappings from `source`.

    Source could be a stack frame, a single dictionary, or any sequence of
    dictionaries.

    '''
    from collections import Mapping
    if isinstance(source, Mapping):
        return (source,)
    else:
        from xoutil.inspect import get_attr_value
        l = get_attr_value(source,  'f_locals', _undef)
        g = get_attr_value(source,  'f_globals', _undef)
        if isinstance(l, Mapping) and isinstance(g, Mapping):
            return (l,) if l is g else (l, g)
        else:
            return tuple(source)