Ejemplo n.º 1
0
def scheme_coerce(arg):
    '''Coerce a scheme definition into a precise formalized dictionary.'''
    from xoutil.values import valid, Invalid
    names, coercers, defaults = _SCHEME_COERCER_CACHE
    if arg is Invalid:
        res = arg
    elif isinstance(arg, dict):
        res = arg
        i = 0
        keys = tuple(res)
        while valid(res) and i < len(keys):
            concept = keys[i]
            if concept in coercers:
                coercer = coercers[concept]
                value = coercer(res[concept])
                if valid(value):
                    res[concept] = value
                    i += 1
                else:
                    res = Invalid
            else:
                res = Invalid
    else:
        if not isinstance(arg, tuple):
            arg = (arg,)
        res = {}
        i = 0
        while valid(res) and i < len(arg):
            value = arg[i]
            j, found = 0, False
            while j < len(names) and not found:
                concept = names[j]
                if concept not in res:
                    coercer = coercers[concept]
                    v = coercer(value)
                    if valid(v):
                        found = True
                        res[concept] = v
                j += 1
            if found:
                i += 1
            else:
                res = Invalid
    if valid(res):
        # Complete and check default value
        for concept in defaults:
            if concept not in res:
                res[concept] = defaults[concept]
        concept = 'default'
        default = res[concept]
        if default is not defaults[concept]:
            coercer = res['checker']
            value = coercer(default)
            if valid(value):
                res[concept] = value
            else:
                res = Invalid
    return res
Ejemplo n.º 2
0
 def test_compound_coercers(self):
     from xoutil.eight import string_types
     from xoutil.values import (coercer, compose, some, combo, iterable,
                                check, valid, typecast, int_coerce,
                                float_coerce, identifier_coerce, Invalid)
     isstr = coercer(string_types)
     strcast = typecast(string_types)
     toint = compose(isstr, int_coerce)
     isint = some(isstr, int_coerce)
     hyphenjoin = coercer(lambda arg: '-'.join(arg))
     intjoin = compose(iterable(strcast), hyphenjoin)
     cb = combo(strcast, int_coerce, float_coerce)
     self.assertEqual(toint('10'), 10)
     self.assertIs(toint(10), Invalid)
     self.assertEqual(toint.scope, (10, isstr))
     self.assertEqual(isint('10'), '10')
     self.assertEqual(isint.scope, isstr)
     self.assertEqual(isint(10), 10)
     self.assertEqual(isint.scope, int_coerce)
     self.assertEqual(intjoin(2*i + 1 for i in range(5)), '1-3-5-7-9')
     self.assertEqual(cb([1, '2.0', 3, 4]), ['1', 2, 3.0])
Ejemplo n.º 3
0
 def test_basic_coercers(self):
     from xoutil.eight import string_types
     from xoutil.values import (identity_coerce, void_coerce, coercer,
                                coercer_name, check, valid, int_coerce,
                                float_coerce, create_int_range_coerce,
                                istype, typecast, iterable, mapping,
                                create_unique_member_coerce, Invalid)
     d = {'1': 2, 3.0: '4', 5.0+0j: 7.3+0j, 1: '2'}
     s = {1, '2', 3.0, '1'}
     l = [1, '2', 3.0, '1', 'x10']
     mc = mapping(int_coerce, float_coerce)
     uint_coerce = create_unique_member_coerce(int_coerce, d)
     mcu = mapping(uint_coerce, float_coerce)
     ic = iterable(int_coerce)
     age_coerce = create_int_range_coerce(0, 100)
     text_coerce = coercer(string_types)
     isnumber = istype((int, float, complex))
     numbercast = typecast((int, float, complex))
     self.assertEqual(all(isinstance(c, coercer)
                          for c in (mc, mcu, uint_coerce, ic,
                                    age_coerce, text_coerce,
                                    identity_coerce, void_coerce,
                                    int_coerce, float_coerce)), True)
     self.assertEqual(mc(dict(d)), {1: 2.0, 3: 4.0, 5: 7.3})
     self.assertIs(mcu(d), Invalid)
     self.assertEqual(mcu.scope, ({'1': 2}, uint_coerce))
     self.assertEqual(ic(s), {1, 2, 3})
     self.assertIs(ic(l), Invalid)
     self.assertIs(ic.scope, l[-1])
     self.assertEqual(l, [1, 2, 3, 1, 'x10'])
     self.assertIs(age_coerce(80), 80)
     self.assertFalse(valid(age_coerce(120)))
     self.assertIs(check(age_coerce, 80), 80)
     with self.assertRaises(TypeError):
         check(age_coerce, 120)
     self.assertIs(isnumber(5), 5)
     self.assertIs(isnumber(5.1), 5.1)
     with self.assertRaises(TypeError):
         check(isnumber, '5.1')
     self.assertIs(numbercast(5), 5)
     self.assertIs(numbercast(5.1), 5.1)
     self.assertEqual(numbercast('5.1'), 5.1)
     self.assertIs(numbercast.scope, float)