def test_errors(self): with pytest.raises(ModuleNotFoundError): configure(u'allennlp.non_existent_module.SomeClass') with pytest.raises(AttributeError): configure( u'allennlp.data.dataset_readers.NonExistentDatasetReader')
def test_specific_subclass(self): config = configure('allennlp.data.dataset_readers.semantic_role_labeling.SrlReader') assert isinstance(config, Config) items = {item.name: item for item in config.items} assert len(items) == 4 assert 'token_indexers' in items token_indexers = items['token_indexers'] assert token_indexers.default_value is None assert 'domain_identifier' in items domain_identifier = items['domain_identifier'] assert domain_identifier.annotation == str assert domain_identifier.default_value is None assert 'bert_model_name' in items domain_identifier = items['bert_model_name'] assert domain_identifier.annotation == str assert domain_identifier.default_value is None assert 'lazy' in items lazy = items['lazy'] assert lazy.annotation == bool assert not lazy.default_value
def _configure(args: argparse.Namespace) -> None: cla55 = args.cla55 parts = cla55.split(".") module = ".".join(parts[:-1]) class_name = parts[-1] print() try: config = configure(cla55) if isinstance(config, Config): if cla55: print(f"configuration stub for {cla55}:\n") else: print(f"configuration stub for AllenNLP:\n") print(render_config(config)) else: print(f"{class_name} is an abstract base class, choose one of the following subclasses:\n") for subclass in config: print("\t", subclass) except ModuleNotFoundError: print(f"unable to load module {module}") except AttributeError: print(f"class {class_name} does not exist in module {module}") print()
def _configure(args: argparse.Namespace) -> None: cla55 = args.cla55 parts = cla55.split(".") module = ".".join(parts[:-1]) class_name = parts[-1] print() try: config = configure(cla55) if isinstance(config, Config): if cla55: print(f"configuration stub for {cla55}:\n") else: print(f"configuration stub for AllenNLP:\n") print(render_config(config)) else: print( f"{class_name} is an abstract base class, choose one of the following subclasses:\n" ) for subclass in config: print("\t", subclass) except ModuleNotFoundError: print(f"unable to load module {module}") except AttributeError: print(f"class {class_name} does not exist in module {module}") print()
def test_vocab_workaround(self): config = configure('allennlp.data.vocabulary.Vocabulary') assert isinstance(config, Config) items = {item.name: item for item in config.items} assert len(items) == 9 assert "directory_path" in items assert "max_vocab_size" in items
def api() -> Response: # pylint: disable=unused-variable class_name = request.args.get('class', '') config = configure(class_name) if isinstance(config, Config): return jsonify({ "className": class_name, "config": config.to_json() }) else: return jsonify({"className": class_name, "choices": config})
def api_config() -> Response: # pylint: disable=unused-variable """ There are basically two things that can happen here. If this method is called with a ``Registrable`` class (e.g. ``Model``), it should return the list of possible ``Model`` subclasses. If it is called with an instantiable subclass (e.g. ``CrfTagger``), is should return the config for that subclass. This is complicated by the fact that some Registrable base classes (e.g. Vocabulary, Trainer) are _themselves_ instantiable. We handle this in two ways: first, we insist that the first case include an extra ``get_choices`` parameter. That is, if you call this method for ``Trainer`` with get_choices=true, you get the list of Trainer subclasses. If you call it without that extra flag, you get the config for the class itself. There are basically two UX situations in which this API is called. The first is when you have a dropdown list of choices (e.g. Model types) and you select one. Such an API request is made *without* the get_choices flag, which means that the config is returned *even if the class in question is a Registrable class that has subclass choices*. The second is when you click a "Configure" button, which configures a class that may (e.g. ``Model``) or may not (e.g. ``FeedForward``) have registrable subclasses. In this case the API request is made with the "get_choices" flag, but will return the corresponding config object if no choices are available (e.g. in the ``FeedForward``) case. This is not elegant, but it works. """ class_name = request.args.get('class', '') get_choices = request.args.get('get_choices', None) # Get the configuration for this class name config = configure(class_name) try: # May not have choices choice5 = choices(class_name) except ValueError: choice5 = [] if get_choices and choice5: return jsonify({ "className": class_name, "choices": choice5 }) else: return jsonify({ "className": class_name, "config": config.to_json() })
def api_config() -> Response: """ There are basically two things that can happen here. If this method is called with a ``Registrable`` class (e.g. ``Model``), it should return the list of possible ``Model`` subclasses. If it is called with an instantiable subclass (e.g. ``CrfTagger``), is should return the config for that subclass. This is complicated by the fact that some Registrable base classes (e.g. Vocabulary, Trainer) are _themselves_ instantiable. We handle this in two ways: first, we insist that the first case include an extra ``get_choices`` parameter. That is, if you call this method for ``Trainer`` with get_choices=true, you get the list of Trainer subclasses. If you call it without that extra flag, you get the config for the class itself. There are basically two UX situations in which this API is called. The first is when you have a dropdown list of choices (e.g. Model types) and you select one. Such an API request is made *without* the get_choices flag, which means that the config is returned *even if the class in question is a Registrable class that has subclass choices*. The second is when you click a "Configure" button, which configures a class that may (e.g. ``Model``) or may not (e.g. ``FeedForward``) have registrable subclasses. In this case the API request is made with the "get_choices" flag, but will return the corresponding config object if no choices are available (e.g. in the ``FeedForward``) case. This is not elegant, but it works. """ class_name = request.args.get("class", "") get_choices = request.args.get("get_choices", None) # Get the configuration for this class name config = configure(class_name) try: # May not have choices choice5 = choices(class_name) except ValueError: choice5 = [] if get_choices and choice5: return jsonify({"className": class_name, "choices": choice5}) else: return jsonify({ "className": class_name, "config": config.to_json() })
def index() -> Response: # pylint: disable=unused-variable class_name = request.args.get('class', '') try: config = configure(class_name) except: # TODO(joelgrus): better error handling raise if isinstance(config, Config): html = config_html(class_name, config) else: html = choices_html(class_name, config) return Response(response=html, status=200)
def api() -> Response: # pylint: disable=unused-variable class_name = request.args.get('class', '') config = configure(class_name) if isinstance(config, Config): return jsonify({ "className": class_name, "config": config.to_json() }) else: return jsonify({ "className": class_name, "choices": config })
def test_specific_subclass(self): config = configure('allennlp.data.dataset_readers.semantic_role_labeling.SrlReader') assert isinstance(config, Config) items = {item.name: item for item in config.items} assert len(items) == 3 assert 'token_indexers' in items token_indexers = items['token_indexers'] assert token_indexers.default_value is None assert 'domain_identifier' in items domain_identifier = items['domain_identifier'] assert domain_identifier.annotation == str assert domain_identifier.default_value is None assert 'lazy' in items lazy = items['lazy'] assert lazy.annotation == bool assert not lazy.default_value
def test_specific_subclass(self): config = configure( u'allennlp.data.dataset_readers.semantic_role_labeling.SrlReader') assert isinstance(config, Config) items = dict((item.name, item) for item in config.items) assert len(items) == 3 assert u'token_indexers' in items token_indexers = items[u'token_indexers'] assert token_indexers.default_value is None assert u'domain_identifier' in items domain_identifier = items[u'domain_identifier'] assert domain_identifier.annotation == unicode assert domain_identifier.default_value is None assert u'lazy' in items lazy = items[u'lazy'] assert lazy.annotation == bool assert not lazy.default_value
def test_abstract_base_class(self): config = configure('allennlp.data.dataset_readers.dataset_reader.DatasetReader') assert isinstance(config, list) assert 'allennlp.data.dataset_readers.snli.SnliReader' in config
def test_errors(self): with pytest.raises(ModuleNotFoundError): configure('allennlp.non_existent_module.SomeClass') with pytest.raises(AttributeError): configure('allennlp.data.dataset_readers.NonExistentDatasetReader')
def test_abstract_base_class(self): config = configure( u'allennlp.data.dataset_readers.dataset_reader.DatasetReader') assert isinstance(config, list) assert u'allennlp.data.dataset_readers.snli.SnliReader' in config
def test_configure_top_level(self): config = configure() assert config == BASE_CONFIG
def test_configure_top_level(self): config = configure() assert config == BASE_CONFIG