Python inspect.getmembers() Examples

The following are 30 code examples of inspect.getmembers(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module inspect , or try the search function .
Example #1
Source File: document.py    From misp42splunk with GNU Lesser General Public License v3.0 7 votes vote down vote up
def collect_options(mcs, bases, attrs):
        """
        Collects options from the current class and its parent classes.

        :returns: a dictionary of options
        """
        options = {}
        # options from parent classes:
        for base in reversed(bases):
            if hasattr(base, '_options'):
                for key, value in inspect.getmembers(base._options):
                    if not key.startswith('_') and value is not None:
                        options[key] = value

        # options from the current class:
        if 'Options' in attrs:
            for key, value in inspect.getmembers(attrs['Options']):
                if not key.startswith('_') and value is not None:
                    # HACK HACK HACK
                    if inspect.ismethod(value) and value.im_self is None:
                        value = value.im_func
                    options[key] = value
        return options 
Example #2
Source File: base.py    From terraform-templates with Apache License 2.0 6 votes vote down vote up
def __init__(self, *args, **kwargs):
            self.pan_device = kwargs.pop('pan_device', None)
            pan.xapi.PanXapi.__init__(self, *args, **kwargs)
            pred = lambda x: inspect.ismethod(x) or inspect.isfunction(x) # inspect.ismethod needed for Python2, inspect.isfunction needed for Python3
            for name, method in inspect.getmembers(
                    pan.xapi.PanXapi,
                    pred):
                # Ignore hidden methods
                if name[0] == "_":
                    continue
                # Ignore non-api methods
                if name in ('xml_result', 'xml_root', 'cmd_xml'):
                    continue

                # Wrapper method.  This is used to create
                # methods in this class that match the methods in the
                # super class, and call the super class methods inside
                # a try/except block, which allows us to check and
                # analyze the exceptions and convert them to more
                # useful exceptions than generic PanXapiErrors.
                wrapper_method = PanDevice.XapiWrapper.make_method(name, method)

                # Create method matching each public method of the base class
                setattr(PanDevice.XapiWrapper, name, wrapper_method) 
Example #3
Source File: models.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _read_options(mcs, name, bases, attrs, options_members):
        """
        Parses model `Options` class into a `SchemaOptions` instance.
        """
        options_class = attrs.get('__optionsclass__', schema.SchemaOptions)
        if 'Options' in attrs:
            for key, value in inspect.getmembers(attrs['Options']):
                if key.startswith("__"):
                    continue
                elif key.startswith("_"):
                    extras = options_members.get("extras", {}).copy()
                    extras.update({key: value})
                    options_members["extras"] = extras
                elif key == "roles":
                    roles = options_members.get("roles", {}).copy()
                    roles.update(value)
                    options_members[key] = roles
                else:
                    options_members[key] = value
        return options_class(**options_members) 
Example #4
Source File: views.py    From py-flask-jsontools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(cls, name, bases, d):
        # Prepare
        methods = set(cls.methods or [])
        methods_map = defaultdict(dict)
        # Methods
        for view_name, func in inspect.getmembers(cls):
            # Collect methods decorated with methodview()
            info = _MethodViewInfo.get_info(func)
            if info is not None:
                # @methodview-decorated view
                for method in info.methods:
                    methods_map[method][view_name] = info
                    methods.add(method)

        # Finish
        cls.methods = tuple(sorted(methods_map.keys()))  # ('GET', ... )
        cls.methods_map = dict(methods_map)  # { 'GET': {'get': _MethodViewInfo } }
        super(MethodViewType, cls).__init__(name, bases, d) 
Example #5
Source File: variable_stat.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 6 votes vote down vote up
def main():
    args = make_args()
    config = configparser.ConfigParser()
    utils.load_config(config, args.config)
    for cmd in args.modify:
        utils.modify_config(config, cmd)
    with open(os.path.expanduser(os.path.expandvars(args.logging)), 'r') as f:
        logging.config.dictConfig(yaml.load(f))
    model_dir = utils.get_model_dir(config)
    path, step, epoch = utils.train.load_model(model_dir)
    state_dict = torch.load(path, map_location=lambda storage, loc: storage)
    mapper = [(inflection.underscore(name), member()) for name, member in inspect.getmembers(importlib.machinery.SourceFileLoader('', __file__).load_module()) if inspect.isclass(member)]
    path = os.path.join(model_dir, os.path.basename(os.path.splitext(__file__)[0])) + '.xlsx'
    with xlsxwriter.Workbook(path, {'strings_to_urls': False, 'nan_inf_to_errors': True}) as workbook:
        worksheet = workbook.add_worksheet(args.worksheet)
        for j, (key, m) in enumerate(mapper):
            worksheet.write(0, j, key)
            for i, (name, variable) in enumerate(state_dict.items()):
                value = m(name, variable)
                worksheet.write(1 + i, j, value)
            if hasattr(m, 'format'):
                m.format(workbook, worksheet, i, j)
        worksheet.autofilter(0, 0, i, len(mapper) - 1)
        worksheet.freeze_panes(1, 0)
    logging.info(path) 
Example #6
Source File: stateful.py    From brownie with MIT License 6 votes vote down vote up
def _generate_state_machine(rules_object: type) -> type:

    bases = (_BrownieStateMachine, rules_object, sf.RuleBasedStateMachine)
    machine = type("BrownieStateMachine", bases, {})
    strategies = {k: v for k, v in getmembers(rules_object) if isinstance(v, SearchStrategy)}

    for attr, fn in filter(_member_filter, getmembers(machine)):
        varnames = [[i] for i in fn.__code__.co_varnames[1 : fn.__code__.co_argcount]]
        if fn.__defaults__:
            for i in range(-1, -1 - len(fn.__defaults__), -1):
                varnames[i].append(fn.__defaults__[i])

        if _attr_filter(attr, "initialize"):
            wrapped = sf.initialize(**{key[0]: strategies[key[-1]] for key in varnames})
            setattr(machine, attr, wrapped(fn))
        elif _attr_filter(attr, "invariant"):
            setattr(machine, attr, sf.invariant()(fn))
        elif _attr_filter(attr, "rule"):
            wrapped = sf.rule(**{key[0]: strategies[key[-1]] for key in varnames})
            setattr(machine, attr, wrapped(fn))

    return machine 
Example #7
Source File: models.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _read_options(mcs, name, bases, attrs, options_members):
        """
        Parses model `Options` class into a `SchemaOptions` instance.
        """
        options_class = attrs.get('__optionsclass__', schema.SchemaOptions)
        if 'Options' in attrs:
            for key, value in inspect.getmembers(attrs['Options']):
                if key.startswith("__"):
                    continue
                elif key.startswith("_"):
                    extras = options_members.get("extras", {}).copy()
                    extras.update({key: value})
                    options_members["extras"] = extras
                elif key == "roles":
                    roles = options_members.get("roles", {}).copy()
                    roles.update(value)
                    options_members[key] = roles
                else:
                    options_members[key] = value
        return options_class(**options_members) 
Example #8
Source File: protocol.py    From GelReportModels with Apache License 2.0 6 votes vote down vote up
def getProtocolClasses(superclass=ProtocolElement):
    """
    Returns all the protocol classes that are subclasses of the
    specified superclass. Only 'leaf' classes are returned,
    corresponding directly to the classes defined in the protocol.
    """
    # We keep a manual list of the superclasses that we define here
    # so we can filter them out when we're getting the protocol
    # classes.
    superclasses = {
        ProtocolElement, SearchRequest, SearchResponse
    }
    thisModule = sys.modules[__name__]
    subclasses = []
    for name, class_ in inspect.getmembers(thisModule):
        if ((inspect.isclass(class_) and
                issubclass(class_, superclass) and
                class_ not in superclasses)):
            subclasses.append(class_)
    return subclasses 
Example #9
Source File: test_constants.py    From bot with MIT License 6 votes vote down vote up
def test_section_configuration_matches_type_specification(self):
        """"The section annotations should match the actual types of the sections."""

        sections = (
            cls
            for (name, cls) in inspect.getmembers(constants)
            if hasattr(cls, 'section') and isinstance(cls, type)
        )
        for section in sections:
            for name, annotation in section.__annotations__.items():
                with self.subTest(section=section.__name__, name=name, annotation=annotation):
                    value = getattr(section, name)
                    origin = typing.get_origin(annotation)
                    annotation_args = typing.get_args(annotation)
                    failure_msg = f"{value} is not an instance of {annotation}"

                    if origin is typing.Union:
                        is_instance = is_any_instance(value, annotation_args)
                        self.assertTrue(is_instance, failure_msg)
                    else:
                        is_instance = is_annotation_instance(value, annotation)
                        self.assertTrue(is_instance, failure_msg) 
Example #10
Source File: gng_impl.py    From loaner with Apache License 2.0 6 votes vote down vote up
def _get_oauth_scopes(modules=_API_CLIENT_MODULES):
  """Retrieves all OAuth2 Scopes required for management.

  Args:
    modules: Sequence[module], a sequence of modules to extract OAuth2 scopes
        from.

  Returns:
    A list of all of the OAuth2 Scopes required for management.
  """
  scopes = []
  for client in modules:
    for name, cls in inspect.getmembers(client, inspect.isclass):
      if name.endswith('API') and hasattr(cls, 'SCOPES'):
        scopes.extend(cls.SCOPES)
  return sorted(list(set(scopes))) 
Example #11
Source File: wuy.py    From wuy with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, log=True):
        global isLog
        isLog = log

        self._name = self.__class__.__name__

        self._routes = {
            n: v
            for n, v in inspect.getmembers(self, inspect.ismethod)
            if not v.__func__.__qualname__.startswith(("Base.", "Window.", "Server."))
        }
        self._routes.update(
            dict(set=self.set, get=self.get)
        )  # add get/set config methods
        if "init" in self._routes:
            del self._routes[
                "init"
            ]  # ensure that a server-side init() is not exposed on client-side
        self._clients = [] 
Example #12
Source File: command.py    From vergeml with MIT License 6 votes vote down vote up
def find_functions(obj):
        """Find all functions of an object or class that define a command."""
        # get all functions defined by the model
        fns = [m[1] for m in inspect.getmembers(obj) if not m[0].startswith("_") and callable(m[1])]

        # sort by the order defined in code
        fns = list(sorted(fns, key=lambda f: f.__code__.co_firstlineno))

        # filter methods where a command is defined
        fns = filter(lambda f: hasattr(f, _CMD_META_KEY), fns)

        return list(fns) 
Example #13
Source File: test_client.py    From spectacles with MIT License 6 votes vote down vote up
def get_client_method_names() -> List[str]:
    """Extracts method names from LookerClient to test for bad responses"""
    client_members: List[Tuple[str, Callable]] = inspect.getmembers(
        LookerClient, predicate=inspect.isroutine
    )
    client_methods: List[str] = [
        member[0] for member in client_members if not member[0].startswith("__")
    ]
    for skip_method in (
        "authenticate",
        "cancel_query_task",
        "request",
        "get",
        "post",
        "put",
        "patch",
        "delete",
    ):
        client_methods.remove(skip_method)
    return client_methods 
Example #14
Source File: docscrape.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def handle_class(val, class_name):
    cls_errors = []
    docstring = inspect.getdoc(val)
    if docstring is None:
        cls_errors.append((class_name,
                           '**missing** class-level docstring'))
    else:
        cls_errors = [
            (e,) for e in
            NumpyClassDocString(docstring, class_name, val).get_errors()
        ]
        # Get public methods and parse their docstrings
        methods = dict(((name, func) for name, func in inspect.getmembers(val)
                        if not name.startswith('_') and callable(func) and
                        type(func) is not type))
        for m_name, method in six.iteritems(methods):
            # skip error check if the method was inherited
            # from a parent class (which means it wasn't
            # defined in this source file)
            if inspect.getmodule(method) is not None:
                continue
            cls_errors.extend(handle_method(method, m_name, class_name))
    return cls_errors 
Example #15
Source File: full.py    From tekore with MIT License 6 votes vote down vote up
def test_all_endpoints_have_scope_attributes(self, client):
        # Skip paging calls and options
        skips = {
            'next',
            'previous',
            'all_pages',
            'all_items',
            'chunked',
            'max_limits',
            'token_as',
        }
        for name, method in getmembers(client, predicate=ismethod):
            if name.startswith('_') or name in skips:
                continue
            assert isinstance(method.scope, Scope)
            assert isinstance(method.required_scope, Scope)
            assert isinstance(method.optional_scope, Scope)
            assert method.scope == method.required_scope + method.optional_scope 
Example #16
Source File: magic.py    From Facedancer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _use_inner_classes_automatically(cls):
    """ Decorator that acts as if all inner classes were defined with `use_automatically`. """

    # Iterate over the relevant class...
    for name, member in inspect.getmembers(cls):

        # ... and tag each inner class with both use_automatically
        # -and- use_inner_classes_automatically. The former
        if inspect.isclass(member) and issubclass(member, AutoInstantiable):

            wrapped_class = _use_inner_classes_automatically(member)
            wrapped_class = use_automatically(member)

            setattr(cls, name, wrapped_class)

    return cls 
Example #17
Source File: magic.py    From Facedancer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def instantiate_subordinates(obj, expected_type):
    """ Automatically instantiates any inner classes with a matching type.

    This is used by objects that represent USB hardware behaviors (e.g. USBDevice, USBConfiguration,
    USBInterface, USBEndpoint) in order to automatically create objects of any inner class
    decorated with ``@use_automatically``.
    """

    instances = {}

    # Search our class for anything decorated with an AutoInstantiator of the relevant type.
    for _, member in inspect.getmembers(obj):
        if isinstance(member, AutoInstantiator) and member.creates_instance_of(expected_type):

            # And instantiate it.
            identifier, target = member(obj)
            instances[identifier] = target

    return instances 
Example #18
Source File: rules.py    From pulseaudio-dlna with GNU General Public License v3.0 5 votes vote down vote up
def load_rules():
    if len(RULES) == 0:
        logger.debug('Loaded rules:')
        for name, _type in inspect.getmembers(sys.modules[__name__]):
            if inspect.isclass(_type) and issubclass(_type, BaseRule):
                if _type is not BaseRule:
                    logger.debug('  {} = {}'.format(name, _type))
                    RULES[name] = _type
    return None 
Example #19
Source File: common.py    From vulscan with MIT License 5 votes vote down vote up
def getPublicTypeMembers(type_, onlyValues=False):
    """
    Useful for getting members from types (e.g. in enums)

    >>> [_ for _ in getPublicTypeMembers(OS, True)]
    ['Linux', 'Windows']
    """

    for name, value in inspect.getmembers(type_):
        if not name.startswith('__'):
            if not onlyValues:
                yield (name, value)
            else:
                yield value 
Example #20
Source File: covermodes.py    From pulseaudio-dlna with GNU General Public License v3.0 5 votes vote down vote up
def load_modes():
    if len(MODES) == 0:
        for name, _type in inspect.getmembers(sys.modules[__name__]):
            if inspect.isclass(_type) and issubclass(_type, BaseCoverMode):
                if _type is not BaseCoverMode:
                    MODES[_type.IDENTIFIER] = _type
    return None 
Example #21
Source File: test_docstring_parameters.py    From celer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_documented():
    """Test that public functions and classes are documented."""
    public_modules_ = public_modules[:]

    doc_file = op.abspath(op.join(op.dirname(__file__), '..', '..', 'doc',
                                  'api.rst'))
    if not op.isfile(doc_file):
        raise SkipTest('Documentation file not found: %s' % doc_file)
    known_names = list()
    with open(doc_file, 'rb') as fid:
        for line in fid:
            line = line.decode('utf-8')
            if not line.startswith('  '):  # at least two spaces
                continue
            line = line.split()
            if len(line) == 1 and line[0] != ':':
                known_names.append(line[0].split('.')[-1])
    known_names = set(known_names)

    missing = []
    for name in public_modules_:
        with warnings.catch_warnings(record=True):  # traits warnings
            module = __import__(name, globals())
        for submod in name.split('.')[1:]:
            module = getattr(module, submod)
        classes = inspect.getmembers(module, inspect.isclass)
        functions = inspect.getmembers(module, inspect.isfunction)
        checks = list(classes) + list(functions)
        for name, cf in checks:
            if not name.startswith('_') and name not in known_names:
                from_mod = inspect.getmodule(cf).__name__
                if (from_mod.startswith('celer') and
                        from_mod not in documented_ignored_mods and
                        name not in documented_ignored_names):
                    missing.append('%s (%s.%s)' % (name, from_mod, name))
    if len(missing) > 0:
        raise AssertionError('\n\nFound new public members missing from '
                             'doc/python_reference.rst:\n\n* ' +
                             '\n* '.join(sorted(set(missing)))) 
Example #22
Source File: src2asciidoc.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def _get_configtypes():
    """Get configtypes classes to document."""
    predicate = lambda e: (
        inspect.isclass(e) and
        # pylint: disable=protected-access
        e not in [configtypes.BaseType, configtypes.MappingType,
                  configtypes._Numeric, configtypes.FontBase] and
        # pylint: enable=protected-access
        issubclass(e, configtypes.BaseType))
    yield from inspect.getmembers(configtypes, predicate) 
Example #23
Source File: channel.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
def traverse_vars(self, node, edge):
        if hasattr(node, 'variable'):
            name = self.var_name[node.variable.data._cdata]
            edge = self.modify(name, edge)
        tensors = [t for name, t in inspect.getmembers(node) if torch.is_tensor(t)]
        if hasattr(node, 'saved_tensors'):
            tensors += node.saved_tensors
        for tensor in tensors:
            name = self.var_name[tensor._cdata]
            edge = self.modify(name, edge)
            if hasattr(self, 'dot'):
                self._draw_tensor(node, tensor, edge)
        return edge 
Example #24
Source File: wrappers.py    From linter-pylama with MIT License 5 votes vote down vote up
def __dict__(self):
        return self.__wrapped__.__dict__

    # Need to also propagate the special __weakref__ attribute for case
    # where decorating classes which will define this. If do not define
    # it and use a function like inspect.getmembers() on a decorator
    # class it will fail. This can't be in the derived classes. 
Example #25
Source File: exceptions.py    From linter-pylama with MIT License 5 votes vote down vote up
def _builtin_exceptions():
    def predicate(obj):
        return isinstance(obj, type) and issubclass(obj, BaseException)

    members = inspect.getmembers(six.moves.builtins, predicate)
    return {exc.__name__ for (_, exc) in members} 
Example #26
Source File: tb_utility.py    From thingsboard-gateway with Apache License 2.0 5 votes vote down vote up
def check_and_import(extension_type, module_name):
        if TBUtility.loaded_extensions.get(extension_type + module_name) is None:
            extensions_paths = (path.abspath(path.dirname(path.dirname(__file__)) + '/connectors/'.replace('/', path.sep) + extension_type.lower()),
                                '/var/lib/thingsboard_gateway/extensions/'.replace('/', path.sep) + extension_type.lower(),
                                path.abspath(path.dirname(path.dirname(__file__)) + '/extensions/'.replace('/', path.sep) + extension_type.lower()))
            try:
                for extension_path in extensions_paths:
                    if path.exists(extension_path):
                        for file in listdir(extension_path):
                            if not file.startswith('__') and file.endswith('.py'):
                                try:
                                    module_spec = util.spec_from_file_location(module_name, extension_path + path.sep + file)
                                    log.debug(module_spec)

                                    if module_spec is None:
                                        log.error('Module: %s not found', module_name)
                                        continue

                                    module = util.module_from_spec(module_spec)
                                    log.debug(str(module))
                                    module_spec.loader.exec_module(module)
                                    for extension_class in getmembers(module, isclass):
                                        if module_name in extension_class:
                                            log.debug("Import %s from %s.", module_name, extension_path)
                                            # Save class into buffer
                                            TBUtility.loaded_extensions[extension_type + module_name] = extension_class[1]
                                            return extension_class[1]
                                except ImportError:
                                    continue
                    else:
                        log.error("Import %s failed, path %s doesn't exist", module_name, extension_path)
            except Exception as e:
                log.exception(e)
        else:
            log.debug("Class %s found in TBUtility buffer.", module_name)
            return TBUtility.loaded_extensions[extension_type + module_name] 
Example #27
Source File: managers.py    From pylxd with Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        manager_for = self.manager_for
        module = '.'.join(manager_for.split('.')[0:-1])
        obj = manager_for.split('.')[-1]
        target_module = importlib.import_module(module)
        target = getattr(target_module, obj)

        methods = inspect.getmembers(target, predicate=inspect.ismethod)
        for name, method in methods:
            func = functools.partial(method, *args, **kwargs)
            setattr(self, name, func)
        return super(BaseManager, self).__init__() 
Example #28
Source File: alias.py    From bot with MIT License 5 votes vote down vote up
def aliases_command(self, ctx: Context) -> None:
        """Show configured aliases on the bot."""
        embed = Embed(
            title='Configured aliases',
            colour=Colour.blue()
        )
        await LinePaginator.paginate(
            (
                f"• `{ctx.prefix}{value.name}` "
                f"=> `{ctx.prefix}{name[:-len('_alias')].replace('_', ' ')}`"
                for name, value in inspect.getmembers(self)
                if isinstance(value, Command) and name.endswith('_alias')
            ),
            ctx, embed, empty=False, max_lines=20
        ) 
Example #29
Source File: test_data.py    From chatterbot-corpus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_language_names(self):
        """
        Each language directroy should adhere to the same nameing convention.
        """
        valid_language_names = []
        language_classes = inspect.getmembers(sys.modules[languages.__name__])

        for _name, obj in language_classes:
            if inspect.isclass(obj):
                valid_language_names.append(obj.ENGLISH_NAME.lower())

        for directory_name in os.listdir(DATA_DIRECTORY):
            self.assertIn(directory_name, valid_language_names) 
Example #30
Source File: bootstrap.py    From loaner with Apache License 2.0 5 votes vote down vote up
def get_bootstrap_functions(get_all=False):
  """Gets all functions necessary for bootstrap.

  This function collects only the functions necessary for the bootstrap
  process. Specifically, it will collect tasks specific to a new or existing
  deployment (an update). Additionally, it will collect any failed tasks so that
  they can be attempted again.

  Args:
    get_all: bool, return all bootstrap tasks, defaults to False.

  Returns:
    Dict, all functions necessary for bootstrap.
  """
  module_functions = inspect.getmembers(
      sys.modules[__name__], inspect.isfunction)
  bootstrap_functions = {
      key: value
      for key, value in dict(module_functions)
      .iteritems() if key.startswith('bootstrap_')
  }

  if get_all or _is_new_deployment():
    return bootstrap_functions

  if is_update():
    bootstrap_functions = {
        key: value for key, value in bootstrap_functions.iteritems()
        if key in _BOOTSTRAP_UPDATE_TASKS
    }
  else:  # Collect all bootstrap functions that failed and all update tasks.
    for function_name in bootstrap_functions.keys():
      status_entity = bootstrap_status_model.BootstrapStatus.get_by_id(
          function_name)
      if (status_entity and
          status_entity.success and
          function_name not in _BOOTSTRAP_UPDATE_TASKS):
        del bootstrap_functions[function_name]
  return bootstrap_functions