• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python inspect.safe_getattr函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sphinx.util.inspect.safe_getattr函数的典型用法代码示例。如果您正苦于以下问题:Python safe_getattr函数的具体用法?Python safe_getattr怎么用?Python safe_getattr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了safe_getattr函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: get_motor_attr

def get_motor_attr(motor_class, name, *defargs):
    """If any Motor attributes can't be accessed, grab the equivalent PyMongo
    attribute. While we're at it, store some info about each attribute
    in the global motor_info dict.
    """
    attr = safe_getattr(motor_class, name)
    method_class = safe_getattr(attr, 'im_class', None)
    from_pymongo = not safe_getattr(
        method_class, '__module__', '').startswith('motor')

    # Store some info for process_motor_nodes()
    full_name = '%s.%s.%s' % (
        motor_class.__module__, motor_class.__name__, name)

    is_async_method = getattr(attr, 'is_async_method', False)
    is_cursor_method = getattr(attr, 'is_motorcursor_chaining_method', False)
    if is_async_method or is_cursor_method:
        pymongo_method = getattr(
            motor_class.__delegate_class__, attr.pymongo_method_name)
    else:
        pymongo_method = None

    is_pymongo_docstring = from_pymongo or is_async_method or is_cursor_method

    motor_info[full_name] = {
        # These sub-attributes are set in motor.asynchronize()
        'is_async_method': is_async_method,
        'callback_required': getattr(attr, 'callback_required', False),
        'is_pymongo_docstring': is_pymongo_docstring,
        'pymongo_method': pymongo_method }

    return attr
开发者ID:Taejun,项目名称:motor,代码行数:32,代码来源:motor_extensions.py


示例2: get_members

    def get_members(clazz, obj, typ):
        names = set()
        items = []

        # the default dir
        for name in dir(obj):
            try:
                documenter = get_documenter(safe_getattr(obj, name), obj)
            except AttributeError:
                continue
            if documenter.objtype == typ and not name.startswith('_'):
                if name not in AutoCosmoSummary.exclude:
                    items.append((clazz,name))
                    names.add(name) # keep track of method/attribute conflicts

        # the delegate dro
        for n in obj.dro:
            for name in dir(n):
                try:
                    documenter = get_documenter(safe_getattr(n, name), n)
                except AttributeError:
                    continue

                # dont do conflicts
                if name not in names:
                    if documenter.objtype == typ and not name.startswith('_'):
                        if name not in AutoCosmoSummary.exclude:
                            x = "%s.%s" %(n.__module__, n.__name__)
                            items.append((x,name))
                            names.add(name)

        return ['~%s.%s' %item for item in sorted(items, key=lambda x: x[1])]
开发者ID:bccp,项目名称:nbodykit,代码行数:32,代码来源:conf.py


示例3: get_object_members

 def get_object_members(self, want_all):
     if want_all:
         # if not hasattr(self.object, '__all__'):
         #     for implicit module members, check __module__ to avoid
         #     documenting imported objects
             return True, safe_getmembers(self.object)
         # else:
         #     memberlist = self.object.__all__
         #     # Sometimes __all__ is broken...
         #     if not isinstance(memberlist, (list, tuple)) or not \
         #        all(isinstance(entry, string_types) for entry in memberlist):
         #         self.directive.warn(
         #             '__all__ should be a list of strings, not %r '
         #             '(in module %s) -- ignoring __all__' %
         #             (memberlist, self.fullname))
         #         # fall back to all members
         #         return True, safe_getmembers(self.object)
     else:
         memberlist = self.options.members or []
     ret = []
     for mname in memberlist:
         try:
             ret.append((mname, safe_getattr(self.object, mname)))
         except AttributeError:
             self.directive.warn(
                 'missing attribute mentioned in :members: or __all__: '
                 'module %s, attribute %s' % (
                     safe_getattr(self.object, '__name__', '???'), mname))
     return False, ret
开发者ID:cxh852456,项目名称:pwntools,代码行数:29,代码来源:conf.py


示例4: get_motor_attr

def get_motor_attr(motor_class, name, *defargs):
    """If any Motor attributes can't be accessed, grab the equivalent PyMongo
    attribute. While we're at it, store some info about each attribute
    in the global motor_info dict.
    """
    attr = safe_getattr(motor_class, name)
    method_class = safe_getattr(attr, "im_class", None)
    from_pymongo = not safe_getattr(method_class, "__module__", "").startswith("motor")

    # Store some info for process_motor_nodes()
    full_name = "%s.%s.%s" % (motor_class.__module__, motor_class.__name__, name)

    is_async_method = getattr(attr, "is_async_method", False)
    is_cursor_method = getattr(attr, "is_motorcursor_chaining_method", False)
    if is_async_method or is_cursor_method:
        pymongo_method = getattr(motor_class.__delegate_class__, attr.pymongo_method_name)
    else:
        pymongo_method = None

    is_pymongo_docstring = from_pymongo or is_async_method or is_cursor_method

    motor_info[full_name] = {
        # These sub-attributes are set in motor.asynchronize()
        "is_async_method": is_async_method,
        "is_pymongo_docstring": is_pymongo_docstring,
        "pymongo_method": pymongo_method,
    }

    return attr
开发者ID:jettify,项目名称:motor,代码行数:29,代码来源:motor_extensions.py


示例5: test_safe_getattr_with_exception

def test_safe_getattr_with_exception():
    class Foo:
        def __getattr__(self, item):
            raise Exception

    obj = Foo()

    try:
        inspect.safe_getattr(obj, 'bar')
    except AttributeError as exc:
        assert exc.args[0] == 'bar'
    else:
        pytest.fail('AttributeError not raised')
开发者ID:olivier-heurtier,项目名称:sphinx,代码行数:13,代码来源:test_util_inspect.py


示例6: test_safe_getattr_with_exception

    def test_safe_getattr_with_exception(self):
        class Foo(object):
            def __getattr__(self, item):
                raise Exception

        obj = Foo()

        try:
            inspect.safe_getattr(obj, 'bar')
        except AttributeError as exc:
            self.assertEqual(exc.args[0], 'bar')
        else:
            self.fail('AttributeError not raised')
开发者ID:nwf,项目名称:sphinx,代码行数:13,代码来源:test_util_inspect.py


示例7: test_safe_getattr_with___dict___override

    def test_safe_getattr_with___dict___override(self):
        class Foo(object):
            @property
            def __dict__(self):
                raise Exception

        obj = Foo()

        try:
            inspect.safe_getattr(obj, 'bar')
        except AttributeError as exc:
            self.assertEqual(exc.args[0], 'bar')
        else:
            self.fail('AttributeError not raised')
开发者ID:nwf,项目名称:sphinx,代码行数:14,代码来源:test_util_inspect.py


示例8: test_safe_getattr_with___dict___override

def test_safe_getattr_with___dict___override():
    class Foo:
        @property
        def __dict__(self):
            raise Exception

    obj = Foo()

    try:
        inspect.safe_getattr(obj, 'bar')
    except AttributeError as exc:
        assert exc.args[0] == 'bar'
    else:
        pytest.fail('AttributeError not raised')
开发者ID:olivier-heurtier,项目名称:sphinx,代码行数:14,代码来源:test_util_inspect.py


示例9: test_safe_getattr_with_property_exception

def test_safe_getattr_with_property_exception():
    class Foo(object):
        @property
        def bar(self):
            raise Exception

    obj = Foo()

    try:
        inspect.safe_getattr(obj, 'bar')
    except AttributeError as exc:
        assert exc.args[0] == 'bar'
    else:
        pytest.fail('AttributeError not raised')
开发者ID:mgeier,项目名称:sphinx,代码行数:14,代码来源:test_util_inspect.py


示例10: get_members_class

            def get_members_class(obj, typ, include_public=[],
                                  include_base=False):
                """
                typ = None -> all
                include_base -> include attrs that are from a base class
                """
                items = []

                # using dir gets all of the attributes, including the elements
                # from the base class, otherwise use __slots__ or __dict__
                if include_base:
                    names = dir(obj)
                else:
                    if hasattr(obj, '__slots__'):
                        names = tuple(getattr(obj, '__slots__'))
                    else:
                        names = getattr(obj, '__dict__').keys()

                for name in names:
                    try:
                        documenter = get_documenter(safe_getattr(obj, name),
                                                    obj)
                    except AttributeError:
                        continue
                    if typ is None or documenter.objtype == typ:
                        items.append(name)
                public = [x for x in items
                          if x in include_public or not x.startswith('_')]
                return public, items
开发者ID:bnaul,项目名称:gatspy,代码行数:29,代码来源:automodsumm.py


示例11: get_motor_attr

def get_motor_attr(motor_class, name, *defargs):
    """If any Motor attributes can't be accessed, grab the equivalent PyMongo
    attribute. While we're at it, store some info about each attribute
    in the global motor_info dict.
    """
    from_pymongo = False
    try:
        attr = safe_getattr(motor_class, name, *defargs)
    except AttributeError:
        # Typically, this means 'name' is refers not to an async method like
        # MotorDatabase.command, but to a ReadOnlyProperty, e.g.
        # MotorClient.close(). The latter can't be accessed directly, but we
        # can get the docstring and method signature from the equivalent
        # PyMongo attribute, e.g. pymongo.mongo_client.MongoClient.close().
        attr = getattr(motor_class.__delegate_class__, name, *defargs)
        from_pymongo = True

    # Store some info for process_motor_nodes()
    full_name = '%s.%s.%s' % (
        motor_class.__module__, motor_class.__name__, name)

    is_async_method = getattr(attr, 'is_async_method', False)
    motor_info[full_name] = {
        # These sub-attributes are set in motor.asynchronize()
        'is_async_method': is_async_method,
        'callback_required': getattr(attr, 'callback_required', False),
        'is_pymongo_docstring': from_pymongo or is_async_method}

    return attr
开发者ID:chiehwen,项目名称:motor,代码行数:29,代码来源:motor_extensions.py


示例12: get_motor_attr

def get_motor_attr(motor_class, name, *defargs):
    """If any Motor attributes can't be accessed, grab the equivalent PyMongo
    attribute. While we're at it, store some info about each attribute
    in the global motor_info dict.
    """
    attr = safe_getattr(motor_class, name)

    # Store some info for process_motor_nodes()
    full_name = '%s.%s.%s' % (
        motor_class.__module__, motor_class.__name__, name)

    full_name_legacy = 'motor.%s.%s.%s' % (
        motor_class.__module__, motor_class.__name__, name)

    # These sub-attributes are set in motor.asynchronize()
    has_coroutine_annotation = getattr(attr, 'coroutine_annotation', False)
    is_async_method = getattr(attr, 'is_async_method', False)
    is_cursor_method = getattr(attr, 'is_motorcursor_chaining_method', False)
    if is_async_method or is_cursor_method:
        pymongo_method = getattr(
            motor_class.__delegate_class__, attr.pymongo_method_name)
    else:
        pymongo_method = None

    # attr.doc is set by statement like 'error = AsyncRead(doc="OBSOLETE")'.
    is_pymongo_doc = pymongo_method and attr.__doc__ == pymongo_method.__doc__

    motor_info[full_name] = motor_info[full_name_legacy] = {
        'is_async_method': is_async_method or has_coroutine_annotation,
        'is_pymongo_docstring': is_pymongo_doc,
        'pymongo_method': pymongo_method,
    }

    return attr
开发者ID:wujuguang,项目名称:motor,代码行数:34,代码来源:motor_extensions.py


示例13: add_content

 def add_content(self, more_content, no_docstring=False):
     if self.doc_as_attr:
         classname = safe_getattr(self.object, '__name__', None)
         if classname:
             content = ViewList(
                 [_('alias of :class:`%s`') % classname], source='')
             MatModuleLevelDocumenter.add_content(self, content,
                                               no_docstring=True)
     else:
         MatModuleLevelDocumenter.add_content(self, more_content)
开发者ID:Lemma1,项目名称:MINAMI,代码行数:10,代码来源:mat_documenters.py


示例14: def_members

 def def_members(obj, typ, include_public=[]):
     items = []
     try:
         obj_dict = safe_getattr(obj, '__dict__')
     except AttributeError:
         return []
     defined = obj_dict.keys()
     defined.sort()
     for name in defined:
         if sys.skip_member(name, obj): continue
         try:
             documenter = get_documenter(safe_getattr(obj, name), obj)
         except AttributeError:
             continue
         if documenter.objtype == typ:
             items.append(name)
     public = [x for x in items
               if x in include_public or not x.startswith('_')]
     return public
开发者ID:ChristenedGamer,项目名称:gui2py,代码行数:19,代码来源:generate.py


示例15: test_safe_getattr_with_default

def test_safe_getattr_with_default():
    class Foo:
        def __getattr__(self, item):
            raise Exception

    obj = Foo()

    result = inspect.safe_getattr(obj, 'bar', 'baz')

    assert result == 'baz'
开发者ID:olivier-heurtier,项目名称:sphinx,代码行数:10,代码来源:test_util_inspect.py


示例16: get_object_members

 def get_object_members(self, want_all):
     if want_all:
         if not hasattr(self.object, '__all__'):
             # for implicit module members, check __module__ to avoid
             # documenting imported objects
             return True, safe_getmembers(self.object)
         else:
             memberlist = self.object.__all__
     else:
         memberlist = self.options.members or []
     ret = []
     for mname in memberlist:
         try:
             ret.append((mname, safe_getattr(self.object, mname)))
         except AttributeError:
             self.directive.warn(
                 'missing attribute mentioned in :members: or __all__: '
                 'module %s, attribute %s' % (
                 safe_getattr(self.object, '__name__', '???'), mname))
     return False, ret
开发者ID:hurtado452,项目名称:battleAtSea-master,代码行数:20,代码来源:autodoc.py


示例17: get_members

 def get_members(obj, typ, include_public=[]):
     items = []
     for name in dir(obj):
         try:
             documenter = get_documenter(safe_getattr(obj, name), obj)
         except AttributeError:
             continue
         if documenter.objtype == typ:
             items.append(name)
     public = [x for x in items if x in include_public or not x.startswith("_")]
     return public, items
开发者ID:QuLogic,项目名称:sphinx,代码行数:11,代码来源:generate.py


示例18: get_motor_attr

def get_motor_attr(motor_class, name, *defargs):
    """If any Motor attributes can't be accessed, grab the equivalent PyMongo
    attribute. While we're at it, store some info about each attribute
    in the global motor_info dict.
    """
    attr = safe_getattr(motor_class, name)
    method_class = safe_getattr(attr, 'im_class', None)
    from_pymongo = not safe_getattr(
        method_class, '__module__', '').startswith('motor')

    # Store some info for process_motor_nodes()
    full_name = '%s.%s.%s' % (
        motor_class.__module__, motor_class.__name__, name)

    # TODO: hack! All docstrings should be updated with fully-qualified refs.
    full_name_legacy = 'motor.motor_tornado.%s.%s' % (
        motor_class.__name__, name)

    has_coroutine_annotation = getattr(attr, 'coroutine_annotation', False)
    is_async_method = getattr(attr, 'is_async_method', False)
    is_cursor_method = getattr(attr, 'is_motorcursor_chaining_method', False)
    if is_async_method or is_cursor_method:
        pymongo_method = getattr(
            motor_class.__delegate_class__, attr.pymongo_method_name)
    else:
        pymongo_method = None

    # attr.doc is set by statement like 'error = AsyncRead(doc="OBSOLETE")'.
    is_pymongo_doc = ((from_pymongo or is_async_method or is_cursor_method)
                      and not getattr(attr, 'doc', None))

    motor_info[full_name] = motor_info[full_name_legacy] = {
        'has_coroutine_annotation': has_coroutine_annotation,
        # These sub-attributes are set in motor.asynchronize()
        'is_async_method': is_async_method,
        'is_pymongo_docstring': is_pymongo_doc,
        'pymongo_method': pymongo_method,
    }

    return attr
开发者ID:adamchainz,项目名称:motor,代码行数:40,代码来源:motor_extensions.py


示例19: get_members

 def get_members(app, obj, typ, include_public=None):
     if not include_public:
         include_public = []
     items = []
     for name in vars(obj):
         try:
             documenter = get_documenter(app, safe_getattr(obj, name), obj)
         except AttributeError:
             continue
         if documenter.objtype == typ:
             items.append(name)
     items.sort()
     public = [x for x in items if x in include_public or not x.startswith('_')]
     return public, items
开发者ID:franzinc,项目名称:agraph-python,代码行数:14,代码来源:hacks.py


示例20: get_members

 def get_members(obj, typ, include_public=[]):
     # type: (Any, unicode, List[unicode]) -> Tuple[List[unicode], List[unicode]]
     items = []  # type: List[unicode]
     for name in dir(obj):
         try:
             documenter = get_documenter(safe_getattr(obj, name),
                                         obj)
         except AttributeError:
             continue
         if documenter.objtype == typ:
             items.append(name)
     public = [x for x in items
               if x in include_public or not x.startswith('_')]
     return public, items
开发者ID:JelteF,项目名称:sphinx,代码行数:14,代码来源:generate.py



注:本文中的sphinx.util.inspect.safe_getattr函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python jsdump.dumps函数代码示例发布时间:2022-05-27
下一篇:
Python i18n.format_date函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap