Python pydoc.locate() Examples

The following are 30 code examples of pydoc.locate(). 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 pydoc , or try the search function .
Example #1
Source File: test_model.py    From gordo with GNU Affero General Public License v3.0 7 votes vote down vote up
def test_keras_autoencoder_scoring(model, kind, n_features_out):
    """
    Test the KerasAutoEncoder and KerasLSTMAutoEncoder have a working scoring function
    """
    Model = pydoc.locate(f"gordo.machine.model.models.{model}")
    model = Pipeline([("model", Model(kind=kind))])
    X = np.random.random((8, 2))

    # Should be able to deal with y output different than X input features
    y = np.random.random((8, n_features_out))

    with pytest.raises(NotFittedError):
        model.score(X, y)

    model.fit(X, y)
    score = model.score(X, y)
    logger.info(f"Score: {score:.4f}") 
Example #2
Source File: test_pydoc.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_builtin(self):
        for name in ('str', 'str.translate', 'builtins.str',
                     'builtins.str.translate'):
            # test low-level function
            self.assertIsNotNone(pydoc.locate(name))
            # test high-level function
            try:
                pydoc.render_doc(name)
            except ImportError:
                self.fail('finding the doc of {!r} failed'.format(name))

        for name in ('notbuiltins', 'strrr', 'strr.translate',
                     'str.trrrranslate', 'builtins.strrr',
                     'builtins.str.trrranslate'):
            self.assertIsNone(pydoc.locate(name))
            self.assertRaises(ImportError, pydoc.render_doc, name) 
Example #3
Source File: test_pydoc.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_builtin(self):
        for name in ('str', 'str.translate', '__builtin__.str',
                     '__builtin__.str.translate'):
            # test low-level function
            self.assertIsNotNone(pydoc.locate(name))
            # test high-level function
            try:
                pydoc.render_doc(name)
            except ImportError:
                self.fail('finding the doc of {!r} failed'.format(o))

        for name in ('not__builtin__', 'strrr', 'strr.translate',
                     'str.trrrranslate', '__builtin__.strrr',
                     '__builtin__.str.trrranslate'):
            self.assertIsNone(pydoc.locate(name))
            self.assertRaises(ImportError, pydoc.render_doc, name) 
Example #4
Source File: dev.py    From dffml with MIT License 6 votes vote down vote up
def config_get(self, op, key, definition):
        # TODO De-duplicate code from dffml/base.py
        try:
            value = traverse_config_get(self.extra_config, key)
        except KeyError as error:
            raise MissingConfig("%s missing %s" % (op.name, key))
        # TODO Argparse nargs and Arg and primitives need to be unified
        if "Dict" in definition.primitive:
            # TODO handle Dict / spec completely
            self.logger.critical(
                "Dict / spec'd arguments are not yet completely handled"
            )
            value = json.loads(value[0])
        else:
            typecast = pydoc.locate(
                definition.primitive.replace("List[", "").replace("]", "")
            )
            # TODO This is a oversimplification of argparse's nargs
            if definition.primitive.startswith("List["):
                value = list(map(typecast, value))
            else:
                value = typecast(value[0])
                if typecast is str and value in ["True", "False"]:
                    raise MissingConfig("%s missing %s" % (op.name, key))
        return value 
Example #5
Source File: configurable.py    From dket with GNU General Public License v3.0 6 votes vote down vote up
def resolve(clz, module=None):
    """Resolve the configurable type."""

    unresolve = 'could not resolve type `{}`'
    notsubclass = 'resolved type {} is not subclass of {}'

    ctype = pydoc.locate(clz)
    if not ctype and module:
        if isinstance(module, six.string_types):
            clz = module + '.' + clz
        else:  # use it as a module
            clz = module.__name__ + '.' + clz
        ctype = resolve(clz)

    if not ctype:
        raise RuntimeError(unresolve.format(clz))
    if not issubclass(ctype, Configurable):
        raise RuntimeError(notsubclass.format(str(ctype), str(Configurable)))
    return ctype 
Example #6
Source File: test_pydoc.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_builtin(self):
        for name in ('str', 'str.translate', '__builtin__.str',
                     '__builtin__.str.translate'):
            # test low-level function
            self.assertIsNotNone(pydoc.locate(name))
            # test high-level function
            try:
                pydoc.render_doc(name)
            except ImportError:
                self.fail('finding the doc of {!r} failed'.format(name))

        for name in ('not__builtin__', 'strrr', 'strr.translate',
                     'str.trrrranslate', '__builtin__.strrr',
                     '__builtin__.str.trrranslate'):
            self.assertIsNone(pydoc.locate(name))
            self.assertRaises(ImportError, pydoc.render_doc, name) 
Example #7
Source File: spectral_machinery.py    From GraphWaveMachine with GNU General Public License v3.0 6 votes vote down vote up
def transform_and_save_embedding(self):
        """
        Transforming the numpy array with real and imaginary values.
        Creating a pandas dataframe and saving it as a csv.
        """
        print("\nSaving the embedding.")
        features = [self.real_and_imaginary.real, self.real_and_imaginary.imag]
        self.real_and_imaginary = np.concatenate(features, axis=1)
        columns_1 = ["reals_"+str(x) for x in range(self.settings.sample_number)]
        columns_2 = ["imags_"+str(x) for x in range(self.settings.sample_number)]
        columns = columns_1 + columns_2
        self.real_and_imaginary = pd.DataFrame(self.real_and_imaginary, columns=columns)
        self.real_and_imaginary.index = self.index
        self.real_and_imaginary.index = self.real_and_imaginary.index.astype(locate(self.settings.node_label_type))
        self.real_and_imaginary = self.real_and_imaginary.sort_index()
        self.real_and_imaginary.to_csv(self.settings.output) 
Example #8
Source File: util.py    From AIT-Core with MIT License 6 votes vote down vote up
def __load_functions__ (symtbl):
    """Loads all Python functions from the module specified in the
    ``functions`` configuration parameter (in config.yaml) into the given
    symbol table (Python dictionary).
    """
    modname = ait.config.get('functions', None)

    if modname:
        module = pydoc.locate(modname)

        if module is None:
            msg = 'No module named %d (from config.yaml functions: parameter)'
            raise ImportError(msg % modname)

        for name in dir(module):
            func = getattr(module, name)
            if callable(func):
                symtbl[name] = func 
Example #9
Source File: services.py    From opentaps_seas with GNU Lesser General Public License v3.0 6 votes vote down vote up
def run_service(service_name, params):
    logger.info('run_service %s with %s', service_name, params)
    service = get_service(service_name)
    if not service:
        raise Exception('Service not found {}'.format(service_name))
    # locate the service implementation
    Service = locate(service.get('engine'))
    # load the default parameter values from the service definition
    parameters = {}
    definition = service.get('input', {})
    for k, v in definition.items():
        if 'default' in v:
            parameters[k] = v.get('default')
    # merge with the given parameters
    if params:
        parameters.update(params)
    service = Service(parameters, service=service)
    if not service.execute:
        raise Exception('Service {} does not have an "execute" method at {}'.format(service_name, service.engine))
    return service.execute() 
Example #10
Source File: server.py    From open-syllabus-project with Apache License 2.0 6 votes vote down vote up
def queue_page(model_import, job_import, worker_count, offset):

    """
    Spool a page of model instances for a job.

    Args:
        model_import (str)
        job_import (str)
        worker_count (int)
        offset (int)
    """

    # Import callables.
    model = locate(model_import)
    job = locate(job_import)

    for row in model.page_cursor(worker_count, offset):
        config.rq.enqueue(job, row.id) 
Example #11
Source File: decode_text.py    From reaction_prediction_seq2seq with Apache License 2.0 6 votes vote down vote up
def __init__(self, params):
    super(DecodeText, self).__init__(params)
    self._unk_mapping = None
    self._unk_replace_fn = None

    if self.params["unk_mapping"] is not None:
      self._unk_mapping = _get_unk_mapping(self.params["unk_mapping"])
    if self.params["unk_replace"]:
      self._unk_replace_fn = functools.partial(
          _unk_replace, mapping=self._unk_mapping)

    self._postproc_fn = None
    if self.params["postproc_fn"]:
      self._postproc_fn = locate(self.params["postproc_fn"])
      if self._postproc_fn is None:
        raise ValueError("postproc_fn not found: {}".format(
            self.params["postproc_fn"])) 
Example #12
Source File: test_pydoc.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_builtin(self):
        for name in ('str', 'str.translate', '__builtin__.str',
                     '__builtin__.str.translate'):
            # test low-level function
            self.assertIsNotNone(pydoc.locate(name))
            # test high-level function
            try:
                pydoc.render_doc(name)
            except ImportError:
                self.fail('finding the doc of {!r} failed'.format(name))

        for name in ('not__builtin__', 'strrr', 'strr.translate',
                     'str.trrrranslate', '__builtin__.strrr',
                     '__builtin__.str.trrranslate'):
            self.assertIsNone(pydoc.locate(name))
            self.assertRaises(ImportError, pydoc.render_doc, name) 
Example #13
Source File: utils.py    From DeepChatModels with MIT License 6 votes vote down vote up
def create_bot(flags=TEST_FLAGS, return_dataset=False):
    """Chatbot factory: Creates and returns a fresh bot. Nice for 
    testing specific methods quickly.
    """
    # Wipe the graph and update config if needed.
    tf.reset_default_graph()
    config = io_utils.parse_config(flags=flags)
    io_utils.print_non_defaults(config)

    # Instantiate a new dataset.
    print("Setting up", config['dataset'], "dataset.")
    dataset_class = locate(config['dataset']) \
                    or getattr(data, config['dataset'])
    dataset = dataset_class(config['dataset_params'])

    # Instantiate a new chatbot.
    print("Creating", config['model'], ". . . ")
    bot_class = locate(config['model']) or getattr(chatbot, config['model'])
    bot = bot_class(dataset, config)

    if return_dataset:
        return bot, dataset
    else:
        return bot 
Example #14
Source File: attention_seq2seq.py    From natural-language-summary-generation-from-structured-data with MIT License 6 votes vote down vote up
def _create_decoder(self, encoder_output, features, _labels):
    attention_class = locate(self.params["attention.class"]) or \
      getattr(decoders.attention, self.params["attention.class"])
    attention_layer = attention_class(
        params=self.params["attention.params"], mode=self.mode)

    # If the input sequence is reversed we also need to reverse
    # the attention scores.
    reverse_scores_lengths = None
    if self.params["source.reverse"]:
      reverse_scores_lengths = features["source_len"]
      if self.use_beam_search:
        reverse_scores_lengths = tf.tile(
            input=reverse_scores_lengths,
            multiples=[self.params["inference.beam_search.beam_width"]])

    return self.decoder_class(
        params=self.params["decoder.params"],
        mode=self.mode,
        vocab_size=self.target_vocab_info.total_size,
        attention_values=encoder_output.attention_values,
        attention_values_length=encoder_output.attention_values_length,
        attention_keys=encoder_output.outputs,
        attention_fn=attention_layer,
        reverse_scores_lengths=reverse_scores_lengths) 
Example #15
Source File: attention_seq2seq.py    From reaction_prediction_seq2seq with Apache License 2.0 6 votes vote down vote up
def _create_decoder(self, encoder_output, features, _labels):
    attention_class = locate(self.params["attention.class"]) or \
      getattr(decoders.attention, self.params["attention.class"])
    attention_layer = attention_class(
        params=self.params["attention.params"], mode=self.mode)

    # If the input sequence is reversed we also need to reverse
    # the attention scores.
    reverse_scores_lengths = None
    if self.params["source.reverse"]:
      reverse_scores_lengths = features["source_len"]
      if self.use_beam_search:
        reverse_scores_lengths = tf.tile(
            input=reverse_scores_lengths,
            multiples=[self.params["inference.beam_search.beam_width"]])

    return self.decoder_class(
        params=self.params["decoder.params"],
        mode=self.mode,
        vocab_size=self.target_vocab_info.total_size,
        attention_values=encoder_output.attention_values,
        attention_values_length=encoder_output.attention_values_length,
        attention_keys=encoder_output.outputs,
        attention_fn=attention_layer,
        reverse_scores_lengths=reverse_scores_lengths) 
Example #16
Source File: generate_connector_list.py    From os-brick with Apache License 2.0 6 votes vote down vote up
def _ensure_loaded(connector_list):
    """Loads everything in a given path.

    This will make sure all classes have been loaded and therefore all
    decorators have registered class.

    :param start_path: The starting path to load.
    """
    classes = []
    for conn in connector_list:
        try:
            conn_class = locate(conn)
            classes.append(conn_class)
        except Exception:
            pass

    return classes 
Example #17
Source File: test_pydoc.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_builtin(self):
        for name in ('str', 'str.translate', '__builtin__.str',
                     '__builtin__.str.translate'):
            # test low-level function
            self.assertIsNotNone(pydoc.locate(name))
            # test high-level function
            try:
                pydoc.render_doc(name)
            except ImportError:
                self.fail('finding the doc of {!r} failed'.format(name))

        for name in ('not__builtin__', 'strrr', 'strr.translate',
                     'str.trrrranslate', '__builtin__.strrr',
                     '__builtin__.str.trrranslate'):
            self.assertIsNone(pydoc.locate(name))
            self.assertRaises(ImportError, pydoc.render_doc, name) 
Example #18
Source File: test_pydoc.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_builtin(self):
        for name in ('str', 'str.translate', 'builtins.str',
                     'builtins.str.translate'):
            # test low-level function
            self.assertIsNotNone(pydoc.locate(name))
            # test high-level function
            try:
                pydoc.render_doc(name)
            except ImportError:
                self.fail('finding the doc of {!r} failed'.format(name))

        for name in ('notbuiltins', 'strrr', 'strr.translate',
                     'str.trrrranslate', 'builtins.strrr',
                     'builtins.str.trrranslate'):
            self.assertIsNone(pydoc.locate(name))
            self.assertRaises(ImportError, pydoc.render_doc, name) 
Example #19
Source File: decode_text.py    From natural-language-summary-generation-from-structured-data with MIT License 6 votes vote down vote up
def __init__(self, params):
    super(DecodeText, self).__init__(params)
    self._unk_mapping = None
    self._unk_replace_fn = None

    if self.params["unk_mapping"] is not None:
      self._unk_mapping = _get_unk_mapping(self.params["unk_mapping"])
    if self.params["unk_replace"]:
      self._unk_replace_fn = functools.partial(
          _unk_replace, mapping=self._unk_mapping)

    self._postproc_fn = None
    if self.params["postproc_fn"]:
      self._postproc_fn = locate(self.params["postproc_fn"])
      if self._postproc_fn is None:
        raise ValueError("postproc_fn not found: {}".format(
            self.params["postproc_fn"])) 
Example #20
Source File: pooling_encoder.py    From reaction_prediction_seq2seq with Apache License 2.0 5 votes vote down vote up
def __init__(self, params, mode, name="pooling_encoder"):
    super(PoolingEncoder, self).__init__(params, mode, name)
    self._pooling_fn = locate(self.params["pooling_fn"])
    self._combiner_fn = locate(self.params["position_embeddings.combiner_fn"]) 
Example #21
Source File: utils.py    From radish with MIT License 5 votes vote down vote up
def locate_python_object(name):
    """
    Locate the object for the given name
    """
    obj = pydoc.locate(name)
    if not obj:
        obj = globals().get(name, None)

    return obj 
Example #22
Source File: configurable.py    From natural-language-summary-generation-from-structured-data with MIT License 5 votes vote down vote up
def _create_from_dict(dict_, default_module, *args, **kwargs):
  """Creates a configurable class from a dictionary. The dictionary must have
  "class" and "params" properties. The class can be either fully qualified, or
  it is looked up in the modules passed via `default_module`.
  """
  class_ = locate(dict_["class"]) or getattr(default_module, dict_["class"])
  params = {}
  if "params" in dict_:
    params = dict_["params"]
  instance = class_(params, *args, **kwargs)
  return instance 
Example #23
Source File: example_config_test.py    From natural-language-summary-generation-from-structured-data with MIT License 5 votes vote down vote up
def _load_model_from_config(config_path, hparam_overrides, vocab_file, mode):
  """Loads model from a configuration file"""
  with gfile.GFile(config_path) as config_file:
    config = yaml.load(config_file)
  model_cls = locate(config["model"]) or getattr(models, config["model"])
  model_params = config["model_params"]
  if hparam_overrides:
    model_params.update(hparam_overrides)
  # Change the max decode length to make the test run faster
  model_params["decoder.params"]["max_decode_length"] = 5
  model_params["vocab_source"] = vocab_file
  model_params["vocab_target"] = vocab_file
  return model_cls(params=model_params, mode=mode) 
Example #24
Source File: utils.py    From natural-language-summary-generation-from-structured-data with MIT License 5 votes vote down vote up
def cell_from_spec(cell_classname, cell_params):
  """Create a RNN Cell instance from a JSON string.

  Args:
    cell_classname: Name of the cell class, e.g. "BasicLSTMCell".
    cell_params: A dictionary of parameters to pass to the cell constructor.

  Returns:
    A RNNCell instance.
  """

  cell_params = cell_params.copy()

  # Find the cell class
  cell_class = locate(cell_classname) or getattr(rnn_cell, cell_classname)

  # Make sure additional arguments are valid
  cell_args = set(inspect.getargspec(cell_class.__init__).args[1:])
  for key in cell_params.keys():
    if key not in cell_args:
      raise ValueError(
          """{} is not a valid argument for {} class. Available arguments
          are: {}""".format(key, cell_class.__name__, cell_args))

  # Create cell
  return cell_class(**cell_params) 
Example #25
Source File: basic_seq2seq.py    From natural-language-summary-generation-from-structured-data with MIT License 5 votes vote down vote up
def _create_bridge(self, encoder_outputs, decoder_state_size):
    """Creates the bridge to be used between encoder and decoder"""
    bridge_class = locate(self.params["bridge.class"]) or \
      getattr(bridges, self.params["bridge.class"])
    return bridge_class(
        encoder_outputs=encoder_outputs,
        decoder_state_size=decoder_state_size,
        params=self.params["bridge.params"],
        mode=self.mode) 
Example #26
Source File: basic_seq2seq.py    From natural-language-summary-generation-from-structured-data with MIT License 5 votes vote down vote up
def __init__(self, params, mode, name="basic_seq2seq"):
    super(BasicSeq2Seq, self).__init__(params, mode, name)
    self.encoder_class = locate(self.params["encoder.class"])
    self.decoder_class = locate(self.params["decoder.class"]) 
Example #27
Source File: conv_encoder.py    From natural-language-summary-generation-from-structured-data with MIT License 5 votes vote down vote up
def __init__(self, params, mode, name="conv_encoder"):
    super(ConvEncoder, self).__init__(params, mode, name)
    self._combiner_fn = locate(self.params["position_embeddings.combiner_fn"]) 
Example #28
Source File: pooling_encoder.py    From natural-language-summary-generation-from-structured-data with MIT License 5 votes vote down vote up
def __init__(self, params, mode, name="pooling_encoder"):
    super(PoolingEncoder, self).__init__(params, mode, name)
    self._pooling_fn = locate(self.params["pooling_fn"])
    self._combiner_fn = locate(self.params["position_embeddings.combiner_fn"]) 
Example #29
Source File: metric_specs.py    From natural-language-summary-generation-from-structured-data with MIT License 5 votes vote down vote up
def __init__(self, params, name):
    # We don't call the super constructor on purpose
    #pylint: disable=W0231
    """Initializer"""
    Configurable.__init__(self, params, tf.contrib.learn.ModeKeys.EVAL)
    self._name = name
    self._eos_token = self.params["eos_token"]
    self._sos_token = self.params["sos_token"]
    self._separator = self.params["separator"]
    self._postproc_fn = None
    if self.params["postproc_fn"]:
      self._postproc_fn = locate(self.params["postproc_fn"])
      if self._postproc_fn is None:
        raise ValueError("postproc_fn not found: {}".format(
            self.params["postproc_fn"])) 
Example #30
Source File: __init__.py    From jesse with MIT License 5 votes vote down vote up
def inject_local_config():
    """
    injects config from local config file
    """
    local_config = locate('config.config')
    from jesse.config import set_config
    set_config(local_config)