sample_data = { 'userNameFirst': 'Adam', 'userNameSecond': 'Smith', 'userPassword': 'supersecretpassword', 'userEmail': 'adam@smith.math.edu', 'userRoles': 'teacher, worker, admin', }
import hashlib desired_data = { 'name': 'Adam', 'second_name': 'Smith', 'password': hashlib.md5('supersecretpassword').hexdigest(), 'email': 'adam@smith.math.edu', 'roles': ['teacher', 'worker', 'admin'], }
new_data = { 'name': sample_data['userNameFirst'], 'second_name': sample_data['userNameSecond'], 'password': hashlib.md5(sample_data['userPassword']).hexdigest(), 'email': sample_data['userEmail'], 'roles': [s.strip() for s in sample_data['userRoles'].split(',')] } assert new_data == desired_data, 'Uh oh'
FIELDS = { 'userNameFirst': 'name', 'userNameSecond': 'second_name', 'userEmail': 'email', } new_data = dict((n2, sample_data[n1]) for n1, n2 in FIELDS.items()) new_data['roles'] = [s.strip() for s in sample_data['userRoles'].split(',')] new_data['password'] = hashlib.md5(sample_data['userPassword']).hexdigest() assert new_data == desired_data, 'Uh oh'
desired_data['title'] = 'Bachelor' # FIELDS = { 'userNameFirst': 'name', 'userNameSecond': 'second_name', 'userEmail': ('email', '__optional'), 'userTitle': ('title', 'Bachelor'), } new_data = {} for old, new in FIELDS.items(): if isinstance(new, tuple): new, default = new if old not in sample_data: if default == '__optional': continue new_data[new] = default else: new_data[new] = sample_data[old] new_data['roles'] = [s.strip() for s in sample_data['userRoles'].split(',')] new_data['password'] = hashlib.md5(sample_data['userPassword']).hexdigest() assert new_data == desired_data, 'Uh oh'
new_data = { 'name': sample_data['userNameFirst'], 'second_name': sample_data['userNameSecond'], 'password': hashlib.md5(sample_data['userPassword']).hexdigest(), 'roles': [s.strip() for s in sample_data['userRoles'].split(',')] } if 'userEmail' in sample_data: new_data['email'] = sample_data['userEmail'] new_data['title'] = sample_data.get('userTitle', 'Bachelor') assert new_data == desired_data, 'Uh oh'
import trafaret as t hash_md5 = lambda d: hashlib.md5(d).hexdigest() comma_to_list = lambda d: [s.strip() for s in d.split(',')] converter = t.Dict({ t.Key('userNameFirst') >> 'name': t.String, t.Key('userNameSecond') >> 'second_name': t.String, t.Key('userPassword') >> 'password': hash_md5, t.Key('userEmail', optional=True) >> 'email': t.Email, t.Key('userTitle', default='Bachelor') >> 'title': t.String, t.Key('userRoles') >> 'roles': comma_to_list, }) assert converter.check(sample_data) == desired_data
>>> import trafaret as t >>> c = t.Dict({'a': t.List(t.Int)}) >>> c.check({'a': [4, 5]}) {'a': [4, 5]} >>> c.check({'a': [4, 'a', 6]}) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "trafaret/__init__.py", line 110, in check return self._convert(self._check_val(value)) File "trafaret/__init__.py", line 804, in _check_val raise DataError(error=errors) trafaret.DataError: {'a': DataError({1: DataError(value cant be converted to int)})}
>>> t.extract_error(c, {'a': [4, 'a', 6]}) {'a': {1: 'value cant be converted to int'}}
>>> from trafaret.utils import unfold >>> unfold(t.extract_error(c, {'a': [4, 'a', 6]}), prefix='form') {'form__a__1': 'value cant be converted to int'}
>>> todt = lambda m: datetime(*[int(i) for i in m.groups()]) >>> (t.String(regex='^year=(\d+),month=(\d+),day=(\d+)$') >> todt).check('year=2011,month=07,day=23') datetime.datetime(2011, 7, 23, 0, 0)
Source: https://habr.com/ru/post/139927/
All Articles