Exemplo n.º 1
0
def test_cast_str():
    assert Option(name='str_option', type=str).cast_value("abc") == "abc"
    assert Option(name='str_option', type=str).cast_value(1.5) == "1.5"
    assert Option(name='str_option', type=str).cast_value('123') == "123"
    assert Option(name='str_option', type=str).cast_value('off') == 'off'
    with pytest.raises(VergeMLError):
        Option(name='str_option', type=str).cast_value({}) == {}
Exemplo n.º 2
0
def test_command_usage5():
    cmd = Command('train',
                  options=[
                      Option(name='optimizer', type=str),
                      Option(name='learning-rate', default=0.0001, short='l')
                  ])
    assert cmd.usage() == USAGE_5
Exemplo n.º 3
0
def test_command_usage6():
    cmd = Command('train', options=[
        Option(name='a', type=str, default="A"),
        Option(name='b', type=str, default="B"),
        Option(name='c', type=str, default="C"),
    ])
    assert cmd.usage() == USAGE_6
Exemplo n.º 4
0
def test_cast_none():
    assert Option(name='none_option', type=type(None)).cast_value(None) == None
    assert Option(name='none_option',
                  type=type(None)).cast_value("NULL") == None
    with pytest.raises(VergeMLError):
        assert Option(name='none_option',
                      type=type(None)).cast_value("Nichts") == None
Exemplo n.º 5
0
def test_command_2():
    cmd = Command('run', options=[Option('<args>', type=list), Option('@AIs', type=list)])
    assert cmd.parse(["run", "tensorboard"]) == {'@AIs': [], '<args>': ["tensorboard"]}
    assert cmd.parse(["@funky-terminator", "run", "tensorboard"]) == \
        {'@AIs': ['funky-terminator'], '<args>': ["tensorboard"]}
    assert cmd.parse(["@funky-terminator", "@touchy-brobot", "run", "tensorboard", "--port=2204"]) == \
                    {'@AIs': ['funky-terminator', 'touchy-brobot'], '<args>': ["tensorboard", "--port=2204"]}
Exemplo n.º 6
0
def test_cast_float():
    assert Option(name='float_option', type=float).cast_value(1) == 1.
    assert Option(name='float_option', type=float).cast_value(1.5) == 1.5
    assert Option(name='float_option', type=float).cast_value('123') == 123.
    assert Option(name='float_option', type=float).cast_value('123.5') == 123.5
    with pytest.raises(VergeMLError):
        Option(name='float_option', type=float).cast_value('x123.5') == 123.5
Exemplo n.º 7
0
def test_command_usage9():
    cmd = Command('predict', options=[
        Option(name='<file>', type='Optional[str]', descr="The file to use when predicting."),
        Option(name='a', type=str, default="A"),
        Option(name='b', type=str, default="B"),
        Option(name='c', type=str, default="C"),
    ])
    assert cmd.usage() == USAGE_9
Exemplo n.º 8
0
def test_command_usage8():
    cmd = Command('predict', options=[
        Option(name='<file>', type='Optional[str]'),
        Option(name='a', type=str, default="A"),
        Option(name='b', type=str, default="B"),
        Option(name='c', type=str, default="C"),
    ])
    assert cmd.usage() == USAGE_8
Exemplo n.º 9
0
def test_command_7():
    cmd = Command('help', options=[Option(name='<topic>'),
                                   Option(name="@AI", type='Optional[@]')], free_form=True)
    assert cmd.parse(["@funky-robot", "help", "--option=xyz", "something"]) == \
           ('funky-robot', ["--option=xyz", "something"])

    assert cmd.parse(["help", "--option=xyz", "something"]) == \
           (None, ["--option=xyz", "something"])
Exemplo n.º 10
0
def test_validate_in():
    Option(name='in_option', type=int, validate=(1,2,3,4,5)).validate_value(5)

    with pytest.raises(VergeMLError, match=r'Invalid value for option in_option\.'):
        Option(name='in_option', type=int, validate=(1,2,3,4,5)).validate_value(6)

    Option(name='in_option', type=int, validate=("adam", "sgd")).validate_value("adam")
    with pytest.raises(VergeMLError, match=r'Invalid value for option in_option\.'):
        Option(name='in_option', type=int, validate=("adam", "sgd")).validate_value("sgf")
Exemplo n.º 11
0
def test_cast_list_int():
    assert Option(name='list_option',
                  type='List[int]').cast_value(["1", "2", "3"]) == [1, 2, 3]
    assert Option(name='list_option', type='List[int]').cast_value([]) == []
    assert Option(name='list_option',
                  type='List[int]').cast_value([1, 2, 3]) == [1, 2, 3]
    with pytest.raises(VergeMLError):
        assert Option(name='list_option',
                      type='List[int]').cast_value(["1.0", "2.0"]) == [1, 2]
Exemplo n.º 12
0
def test_command_usage4():
    cmd = Command('predict',
                  options=[
                      Option(name='@AI'),
                      Option(name='threshold',
                             default=0.2,
                             descr="Prediction Threshold.")
                  ])
    assert cmd.usage() == USAGE_4
Exemplo n.º 13
0
def test_command_usage11():
    cmd = Command('predict',
                  options=[
                      Option(name='@AIs', type="List[AI]"),
                      Option(name='threshold',
                             default=0.2,
                             descr="Prediction Threshold.")
                  ])
    assert cmd.usage() == USAGE_11
Exemplo n.º 14
0
def test_cast_union():
    assert Option(name='union_option',
                  type='Union[int,str]').cast_value("abc") == "abc"
    assert Option(name='union_option',
                  type='Union[int,str]').cast_value(1) == 1
    assert Option(name='union_option',
                  type='Union[int,str]').cast_value("1") == 1
    with pytest.raises(VergeMLError):
        assert Option(name='union_option',
                      type='Union[int,str]').cast_value(True) == True
Exemplo n.º 15
0
def test_command_5():
    options = [
        Option('threshold', type=float, validate=">0", short='t'),
        Option('id', default=False, type=bool, flag=True, short='i')
    ]
    cmd = Command('predict', options=options)
    assert cmd.parse(["predict", "--threshold=0.2"]) == {'threshold': 0.2, 'id': False}
    assert cmd.parse(["predict", "-t0.2"]) == {'threshold': 0.2, 'id': False}
    assert cmd.parse(["predict", "-t0.2", "--id"]) == {'threshold': 0.2, 'id': True}
    assert cmd.parse(["predict", "-t0.2", "-i"]) == {'threshold': 0.2, 'id': True}
Exemplo n.º 16
0
def test_command_usage13():
    cmd = Command('predict',
                  long_descr="Make a prediction.",
                  examples="ml @skynet predict",
                  options=[
                      Option(name='@AIs', type="List[AI]"),
                      Option(name='threshold',
                             default=0.2,
                             descr="Prediction Threshold.")
                  ])
    assert cmd.usage() == USAGE_13
Exemplo n.º 17
0
def test_cast_list_str():
    assert Option(name='list_option',
                  type='List[str]').cast_value(["a", "b",
                                                "c"]) == ["a", "b", "c"]
    assert Option(name='list_option', type='List[str]').cast_value([]) == []
    assert Option(name='list_option',
                  type='List[str]').cast_value([1, 2, 3]) == ["1", "2", "3"]
    with pytest.raises(VergeMLError):
        assert Option(name='list_option',
                      type='List[str]').cast_value([True, False
                                                    ]) == ["True", "False"]
Exemplo n.º 18
0
def test_command_3():
    cmd = Command('predict', options=[Option(name="@AI")])
    assert cmd.parse(["@stubborn-dishwasher", "predict"]) == {
        '@AI': 'stubborn-dishwasher'
    }
    with pytest.raises(VergeMLError):
        cmd.parse(["predict"])
Exemplo n.º 19
0
    def decorator(o):
        assert getattr(o, _MODEL_META_KEY, None) is None

        options = Option.discover(o)
        cmd = Model(name, descr=descr, long_descr=long_descr, options=options)
        setattr(o, _MODEL_META_KEY, cmd)
        return o
Exemplo n.º 20
0
    def decorator(obj):
        # make sure that a command is not defined more than once
        assert getattr(obj, _CMD_META_KEY, None) is None

        # name defaults to the functions name
        _name = name or getattr(obj, '__name__', None)

        # construct the command with options and attach it to obj
        options = list(reversed(Option.discover(obj)))
        cmd = Command(_name,
                      descr=descr,
                      long_descr=long_descr,
                      examples=examples,
                      options=options,
                      free_form=free_form,
                      type=type)

        if inspect.isclass(obj):
            setattr(obj, _CMD_META_KEY, cmd)
            _wrapper = _CommandCallProxy.class_wrapper(obj, cmd)
        else:
            _wrapper = _CommandCallProxy.function_wrapper(obj, cmd)

        setattr(_wrapper, _CMD_META_KEY, cmd)

        return _wrapper
Exemplo n.º 21
0
 def __init__(self, name=None, plugins=PLUGINS):
     self._values = {}
     self._options = Option.discover(self, plugins=plugins)
     self.plugins = plugins
     self.name = name
     for option in self._options:
         if option.alias is None:
             dict_set_path(self._values, option.name, option.default)
Exemplo n.º 22
0
    def decorator(o):
        # if getattr(o, _SOURCE_META_KEY, None):
        #     print(getattr(o, _SOURCE_META_KEY, None).name, name)
        # assert getattr(o, _SOURCE_META_KEY, None) is None
        
        options = Option.discover(o)
        if input_patterns:
            assert isinstance(input_patterns, (str, list))
            input_patterns_option = Option('input-patterns', input_patterns, type='Union[str, List[str]]', 
                                            descr="Controls which files are loaded.",
                                            transform=lambda v: v if isinstance(v, list) else v.split(","))
            options.append(input_patterns_option)

        cmd = Source(name, 
                    descr=descr, 
                    long_descr=long_descr,
                    options=options)
        setattr(o, _SOURCE_META_KEY, cmd)
        return o
Exemplo n.º 23
0
def test_validate_float():
    Option(name='float_option_1', validate='>=0').validate_value(0.1)
    Option(name='float_option_2', validate='>=0').validate_value(0.0)
    Option(name='float_option_3', validate='<=0').validate_value(0.0)
    Option(name='float_option_4', validate='<=0').validate_value(-0.)
    Option(name='float_option_5', validate='<0').validate_value(-0.1)
    Option(name='float_option_6', validate='>0').validate_value(0.1)

    with pytest.raises(VergeMLError):
        Option(name='float_option_7', validate='>=0').validate_value(-0.1)

    with pytest.raises(VergeMLError):
        Option(name='float_option_8', validate='<=0').validate_value(0.1)

    with pytest.raises(VergeMLError):
        Option(name='float_option_9', validate='<0').validate_value(0.1)

    with pytest.raises(VergeMLError):
        Option(name='float_option_10', validate='>0').validate_value(-0.1)
Exemplo n.º 24
0
    def decorator(o):
        assert getattr(o, _OPERATION_META_KEY, None) is None

        options = Option.discover(o)
        cmd = Operation(name,
                        descr=descr,
                        long_descr=long_descr,
                        apply=apply,
                        options=options,
                        topic=topic)
        setattr(o, _OPERATION_META_KEY, cmd)
        return o
Exemplo n.º 25
0
def test_command_1():
    cmd = Command('train', options=[Option('epochs', 20, int, validate='>=1')])
    assert cmd.parse(["train", "--epochs=14"]) == {'epochs': 14}

    with pytest.raises(VergeMLError):
        cmd.parse(["train", "--epochs=abc"])

    with pytest.raises(VergeMLError):
        cmd.parse(["train", "--epochz=14"])

    with pytest.raises(VergeMLError):
        cmd.parse(["train", "--epochs=-1"])
Exemplo n.º 26
0
 def decorator(o):
     assert(getattr(o, _CMD_META_KEY, None) is None)
     _name = name or getattr(o, '__name__', None)
     options = list(reversed(Option.discover(o)))
     cmd = Command(_name, 
                   descr=descr, 
                   long_descr=long_descr, 
                   examples=examples, 
                   options=options,
                   free_form=free_form,
                   kind=kind)
     setattr(o, _CMD_META_KEY, cmd)
     return o
Exemplo n.º 27
0
def test_cast_list_float():
    assert Option(name='list_option', type='List[float]').cast_value(["1", "2", "3"]) == [1., 2., 3.]
    assert Option(name='list_option', type='List[float]').cast_value([]) == []
    assert Option(name='list_option', type='List[float]').cast_value([1., 2., 3.]) == [1., 2., 3.]
    assert Option(name='list_option', type='List[float]').cast_value([1, 2, 3]) == [1., 2., 3.]
    assert Option(name='list_option', type='List[float]').cast_value(["1.0", "2.0"]) == [1, 2]
    with pytest.raises(VergeMLError):
        assert Option(name='list_option', type='List[float]').cast_value(["one", "two"]) == [1., 2.]
Exemplo n.º 28
0
def test_cast_bool():
    assert Option(name='bool_option', type=bool).cast_value(True) == True
    assert Option(name='bool_option', type=bool).cast_value(False) == False
    assert Option(name='bool_option', type=bool).cast_value('on') == True
    assert Option(name='bool_option', type=bool).cast_value('off') == False

    with pytest.raises(VergeMLError):
        Option(name='bool_option', type=bool).cast_value('falsch') == False

    with pytest.raises(VergeMLError):
        Option(name='bool_option', type=bool).cast_value(1) == True
Exemplo n.º 29
0
def _normalize(raw, validators):
    raw = deepcopy(raw)
    res = {}

    for _, conf in validators.items():
        options = Option.discover(conf)
        aliases = [opt for opt in options if opt.alias]
        for alias in aliases:
            if dict_has_path(raw, alias.name):
                v = dict_get_path(raw, alias.name)
                if not alias.type or isinstance(v, alias.type):
                    dict_set_path(res, alias.alias, v)
                    dict_del_path(raw, alias.name)

    for k, v in deepcopy(raw).items():
        if "." in k:
            dict_set_path(res, k, v)
            del raw[k]

    return dict_merge(res, raw)
Exemplo n.º 30
0
 def options(self):
     return Option.discover(self)