def solve_results(): '''Assign default values for missing arguments.''' from xoutil.values import valid for par, ps in iteritems(self.scheme): if clean(par): default = ps['default'] if valid(default): kwargs[str(par)] = default else: msg = 'Missing required argument "{}"!' raise TypeError(msg.format(par))
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
def check_kwargs(): '''Check all formal keyword arguments.''' from xoutil.values import valid for key, arg in iteritems(kwargs): if key in self.scheme: checker = self.scheme[key]['checker'] value = checker(arg) if valid(value): kwargs[str(key)] = value else: msg = 'Invalid argument value "{}": "{}"!' raise ValueError(msg.format(key, arg)) elif self.strict: msg = 'Invalid keyword argument "{}": "{}"!' raise ValueError(msg.format(key, arg))
def get_duplicate(): '''Get a possible all not settled valid parameter names.''' from xoutil.values import valid res = None pos = last_pivot while not res and pos < len(positions): names = positions[pos] i = 0 while not res and i < len(names): name = names[i] if name not in settled: checker = self.scheme[name]['checker'] value = checker(arg) if valid(value): res = name i += 1 pos += 1 return res
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)
def get_valid(): '''Get the valid parameter name in current position pivot. Return a tuple (name, value) if valid. ''' from xoutil.values import valid names = positions[pivot] i, count = 0, len(names) res = () while not res and i < count: name = names[i] if clean(name): checker = self.scheme[name]['checker'] value = checker(arg) if valid(value): res = (name, value) i += 1 return res