Example #1
0
async def test_tuple():
    trafaret = t.Tuple(t.Null, t.ToInt & check_int)
    res = await (trafaret.async_check([None, '5']))
    assert res == (None, 5)
    with pytest.raises(t.DataError) as res:
        await (trafaret.async_check((None, '5qwe')))
    assert res.value.as_dict() == {1: "value can't be converted to int"}
Example #2
0
 def test_tuple(self):
     tup = t.Tuple(t.Int, t.Int, t.String)
     self.assertEqual(repr(tup), '<Tuple(<Int>, <Int>, <String>)>')
     res = tup.check([3, 4, '5'])
     self.assertEqual(res, (3, 4, '5'))
     res = extract_error(tup, [3, 4, 5])
     self.assertEqual(res, {2: 'value is not a string'})
Example #3
0
 def test_tuple(self):
     tup = t.Tuple(t.ToInt, t.ToInt, t.String)
     res = tup.check([3, 4, u'5'])
     assert res == (3, 4, u'5')
     res = extract_error(tup, [3, 4, 5])
     assert res == {2: 'value is not a string'}
     res = extract_error(tup, 5)
     assert res == 'value must be convertable to tuple'
     res = extract_error(tup, [5])
     assert res == 'value must contain 3 items'
class TestTuple(unittest.TestCase):

    TRAFARET = T.Dict({
        T.Key("tuple", optional=True):
        T.Tuple(T.String(), T.Int()),
    })

    def test_items(self):
        self.assertEqual(
            get_err(
                self.TRAFARET, u"""
            tuple:
            - "hello"
            - "world"
        """),
            dedent(u"""\
            config.yaml:4: tuple[1]: value can't be converted to int
        """))
Example #5
0
def construct(arg):
    '''
    Shortcut syntax to define trafarets.

    - int, str, float and bool will return t.Int, t.String, t.Float and t.Bool
    - one element list will return t.List
    - tuple or list with several args will return t.Tuple
    - dict will return t.Dict. If key has '?' at the and it will be optional and '?' will be removed
    - any callable will be t.Call
    - otherwise it will be returned as is

    construct is recursive and will try construct all lists, tuples and dicts args
    '''
    if isinstance(arg, t.Trafaret):
        return arg
    elif isinstance(arg, tuple) or (isinstance(arg, list) and len(arg) > 1):
        return t.Tuple(*(construct(a) for a in arg))
    elif isinstance(arg, list):
        # if len(arg) == 1
        return t.List(construct(arg[0]))
    elif isinstance(arg, dict):
        return t.Dict({
            construct_key(key): construct(value)
            for key, value in arg.items()
        })
    elif isinstance(arg, str):
        return t.Atom(arg)
    elif isinstance(arg, type):
        if arg is int:
            return t.Int()
        elif arg is float:
            return t.Float()
        elif arg is str:
            return t.String()
        elif arg is bool:
            return t.Bool()
        else:
            return t.Type(arg)
    elif callable(arg):
        return t.Call(arg)
    else:
        return arg
Example #6
0
 def test_tuple(self):
     trafaret = t.Tuple(CONTEXT_TRAFARET)
     assert trafaret([123], context=123) == (123, )
Example #7
0
 def test_repr(self):
     tup = t.Tuple(t.ToInt, t.ToInt, t.String)
     assert repr(tup) == '<Tuple(<ToInt>, <ToInt>, <String>)>'
Example #8
0
    if image_path == '':
        image_source = 'url'

    else:
        image_source = 'directory'

    return image_source, csv_path


@t.guard(image_column_header=t.String(allow_blank=False),
         model_str=t.String(allow_blank=False),
         list_of_images=t.List(t.String(allow_blank=True)),
         image_path=t.String(allow_blank=True),
         csv_path=t.String(allow_blank=True),
         new_csv_name=t.String(allow_blank=True),
         target_size=t.Tuple(t.Int, t.Int),
         grayscale=t.Bool)
def preprocess_data(image_column_header,
                    model_str,
                    list_of_images,
                    image_path='',
                    csv_path='',
                    new_csv_name='~/Downloads/featurized_images.csv',
                    target_size=(299, 299),
                    grayscale=False):
    """
    Receive the data (some combination of image directory + csv), find
    the list of valid images, and then convert each to an array and adds
    them to the full batch.

    Parameters:
Example #9
0
 def test_tuple(self):
     trafaret = t.Tuple(t.Null, t.Int & check_int)
     res = self.loop.run_until_complete(trafaret.async_check([None, '5']))
     self.assertEqual(res, (None, 5))
Example #10
0
 def test_tuple(self):
     trafaret = t.Tuple(CONTEXT_TRAFARET)
     self.assertEqual(trafaret([123], context=123), (123, ))