Python trafaret.Dict() Examples
The following are 30
code examples of trafaret.Dict().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
trafaret
, or try the search function
.
Example #1
Source File: vfolder.py From backend.ai-manager with GNU Lesser General Public License v3.0 | 6 votes |
def delete_files(request: web.Request, params: Any, row: VFolderRow) -> web.Response: folder_name = request.match_info['name'] access_key = request['keypair']['access_key'] recursive = params['recursive'] log.info('VFOLDER.DELETE_FILES (ak:{}, vf:{}, path:{}, recursive:{})', access_key, folder_name, folder_name, recursive) folder_path = get_folder_hostpath(row, request.app) ops = [] for file in params['files']: try: file_path = (folder_path / file).resolve() file_path.relative_to(folder_path) except ValueError: # path is out of folder continue if file_path.is_dir(): if recursive: ops.append(functools.partial(shutil.rmtree, file_path)) else: raise InvalidAPIParameters( f'"{file_path}" is a directory. ' 'Set recursive option to remove it.') elif file_path.is_file(): ops.append(functools.partial(os.unlink, file_path)) def _do_ops(): for op in ops: op() loop = current_loop() await loop.run_in_executor(None, _do_ops) resp: Dict[str, Any] = {} return web.json_response(resp, status=200)
Example #2
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_base3(self): trafaret = t.Dict({t.Key('bar', default=u'nyanya') >> 'baz': t.String}, foo=t.ToInt) res = trafaret.check({'foo': 4}) assert res == {'baz': u'nyanya', 'foo': 4} trafaret = trafaret.allow_extra('*') res = extract_error(trafaret, {'baz': u'spam', 'foo': 4}) assert res == {'baz': 'baz key was shadowed'} trafaret = trafaret.allow_extra('*', trafaret=t.String) res = extract_error(trafaret, {'baaz': 5, 'foo': 4}) assert res == {'baaz': 'value is not a string'} res = trafaret({'baaz': u'strstr', 'foo':4}) assert res == {'baaz': u'strstr', 'foo':4, 'baz': u'nyanya'} trafaret = trafaret.ignore_extra('fooz') res = trafaret.check({'foo': 4, 'fooz': 5}) assert res == {'baz': u'nyanya', 'foo': 4} trafaret = trafaret.ignore_extra('*') res = trafaret.check({'foo': 4, 'foor': 5}) assert res == {'baz': u'nyanya', 'foo': 4}
Example #3
Source File: ql.py From flask-graphql-example with MIT License | 6 votes |
def mutate(cls, _, info, __): logger.debug("agrs %s", info) post_schema = t.Dict({ 'title': t.String(min_length=2), 'user_id': t.String(min_length=2), 'content': t.String(min_length=2), t.Key('tags', optional=True): t.List(t.String, min_length=1), }) post_data = post_schema.check(info) user_id = post_data.pop('user_id') user = User.objects.get_or_404(id=user_id) post = Post(author=user, **post_data) post.save() return cls(post=construct(PostField, post))
Example #4
Source File: layout_utils.py From aiohttp_admin with Apache License 2.0 | 6 votes |
def build_field(key, value, relations=None): extra = None name = key if isinstance(value, t.ToInt): v = "number" elif isinstance(value, (t.String, t.URL)): v = "string" elif isinstance(value, t.Email): v = "email" elif isinstance(value, t.ToFloat): v = "float" elif isinstance(value, t.Enum): v = "choice" elif isinstance(value, (t.Dict, t.List)): v = "json" elif isinstance(value, (t.Bool, t.StrBool)): v = "boolean" elif isinstance(value, DateTime): v = "datetime" else: v = "string" return name, v, extra
Example #5
Source File: db_fixtures.py From aiohttp_admin with Apache License 2.0 | 6 votes |
def document_schema(): choices = ['a', 'b', 'c'] schema = t.Dict({ t.Key('_id'): MongoId, t.Key('title'): t.String(max_length=200), t.Key('category'): t.String(max_length=200), t.Key('body'): t.String, t.Key('views'): t.ToInt, t.Key('average_note'): t.ToFloat, # t.Key('pictures'): t.Dict({}).allow_extra('*'), t.Key('published_at'): DateTime, # t.Key('tags'): t.List(t.Int), t.Key('status'): t.Enum(*choices), t.Key('visible'): t.ToBool, }) return schema
Example #6
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_add_kwargs_extra(self): first = t.Dict( t.Key('bar', trafaret=t.Int()), allow_extra=['eggs'] ) second = t.Dict(t.Key('bar1', trafaret=t.Int())) third = first + second third.check({"bar": 1, "bar1": 41, "eggs": None}) third.check({"bar": 1, "bar1": 41}) with pytest.raises(t.DataError): third.check({"bar": 2, "bar1": 1, "marmalade": 5}) first = t.Dict(t.Key('bar', trafaret=t.Int())) second = t.Dict(t.Key('bar1', trafaret=t.Int()), allow_extra=['eggs']) third = first + second third.check({"bar": 1, "bar1": 41, "eggs": None}) third.check({"bar": 1, "bar1": 41}) with pytest.raises(t.DataError): third.check({"bar": 2, "bar1": 1, "marmalade": 5})
Example #7
Source File: test_async.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_dict(): trafaret = t.Dict({ t.Key('b'): t.ToInt & check_int, }) res = await trafaret.async_check({'b': '5'}) assert res == {'b': 5} # bad value with pytest.raises(t.DataError) as res: await trafaret.async_check({'b': 'qwe'}) assert res.value.as_dict() == {'b': "value can't be converted to int"} # is not a dict with pytest.raises(t.DataError) as res: await trafaret.async_check(None) assert res.value.as_dict() == "value is not a dict" # missed key with pytest.raises(t.DataError) as res: await trafaret.async_check({}) assert res.value.as_dict() == {'b': 'is required'}
Example #8
Source File: test_keys.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_xor_key(self): trafaret = t.Dict(xor_key('name', 'nick', t.String())) res = trafaret({'name': u'Nickolay'}) assert res == {'name': u'Nickolay'} res = catch_error(trafaret, {'name': u'Nickolay', 'nick': u'Sveta'}) assert res.as_dict() == { 'name': 'correct only if nick is not defined', 'nick': 'correct only if name is not defined', } res = catch_error(trafaret, {}) assert res.as_dict() == { 'name': 'is required if nick is not defined', 'nick': 'is required if name is not defined', }
Example #9
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 6 votes |
def test_add_kwargs_ignore(self): first = t.Dict( t.Key('bar', trafaret=t.Int()), ignore_extra=['eggs'] ) second = t.Dict( t.Key('bar1', trafaret=t.Int()) ) third = first + second third.check({'bar': 4, 'bar1': 41}) third.check({'bar': 4, 'bar1': 41, 'eggs': 'blabla'}) first = t.Dict( t.Key('bar', trafaret=t.Int()), ) second = t.Dict( t.Key('bar1', trafaret=t.Int()), ignore_extra=['eggs'] ) third = first + second third.check({'bar': 4, 'bar1': 41}) third.check({'bar': 4, 'bar1': 41, 'eggs': 'blabla'})
Example #10
Source File: test_keys.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_subdict_sample(self): def check_passwords_equal(data): if data['password'] != data['password_confirm']: return t.DataError('Passwords are not equal', code='are_no_equal') return data['password'] check_password = t.String() passwords_key = subdict( 'password', t.Key('password', trafaret=check_password), t.Key('password_confirm', trafaret=check_password), trafaret=check_passwords_equal, ) signup_trafaret = t.Dict( t.Key('email', trafaret=t.Email), passwords_key, ) res = signup_trafaret({'email': u'me@gmail.com', 'password': u'qwerty', 'password_confirm': u'qwerty'}) assert res == {'email': u'me@gmail.com', 'password': u'qwerty'} res = catch_error(signup_trafaret, {'email': u'me@gmail.com', 'password': u'qwerty', 'password_confirm': u'not qwerty'}) assert res.as_dict() == {'password': 'Passwords are not equal'} res = catch_error(signup_trafaret, {'email': u'me@gmail.com', 'password': u'qwerty'}) assert res.as_dict() == {'password_confirm': 'is required'}
Example #11
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_add_to_names_list_of_keys(self): dct = t.Dict(key1=t.String) dct + [t.Key('a', trafaret=t.String())]
Example #12
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_add_to_names_dict_of_keys(self): dct = t.Dict(key1=t.String) dct + {'a': t.String}
Example #13
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_bad_args_add(self): dct = t.Dict(key1=t.String) with pytest.raises(TypeError): dct + 8
Example #14
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_mapping_interface(self): trafaret = t.Dict({t.Key("foo"): t.String, t.Key("bar"): t.ToFloat}) # class with mapping interface but not subclass of dict class Map(AbcMapping): def __init__(self, data, *a, **kw): super(Map, self).__init__(*a, **kw) self._data = data def __getitem__(self, key): return self._data[key] def __iter__(self): for x in self._data: yield x def __len__(self): return len(self._data) trafaret.check(Map({"foo": u"xxx", "bar": 0.1})) res = extract_error(trafaret, object()) assert res == "value is not a dict" res = extract_error(trafaret, Map({"foo": u"xxx"})) assert res == {'bar': 'is required'} res = extract_error(trafaret, Map({"foo": u"xxx", "bar": u'str'})) assert res == {'bar': "value can't be converted to float"}
Example #15
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_clone(self): d = t.Dict(t.Key('a', t.Int), ignore_extra='*') newd = d.ignore_extra('a') assert newd.ignore_any is True assert newd.ignore == ['a']
Example #16
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_forward(self): node = t.Forward() res = extract_error(node, 'something') assert res == 'trafaret not set yet' node << t.Dict(name=t.String, children=t.List[node]) assert node.check({"name": u"foo", "children": []}) == {'children': [], 'name': u'foo'} res = extract_error(node, {"name": u"foo", "children": [1]}) assert res == {'children': {0: 'value is not a dict'}} res = node.check({"name": u"foo", "children": [{"name": u"bar", "children": []}]}) assert res == {'children': [{'children': [], 'name': u'bar'}], 'name': u'foo'} with pytest.raises(RuntimeError): # __rshift__ is not overridden node << t.Int()
Example #17
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_repr(self): node = t.Forward() assert repr(node) == '<Forward(None)>' node << t.Dict(name=t.String, children=t.List[node]) assert repr(node) == '<Forward(<Dict(<Key "children" <List(<recur>)>>, <Key "name" <String>>)>)>'
Example #18
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_2_0_regression(self): t_request = t.Dict({ t.Key('params', optional=True): t.Or(t.List(t.Any()), t.Mapping(t.AnyString(), t.Any())), }) assert t_request.check({'params': {'aaa': 123}}) == {'params': {'aaa': 123}}
Example #19
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_args_checks(self): with pytest.raises(RuntimeError) as exc_info: @guard(123) def fn(**kw): return kw assert exc_info.value.args[0] == 'trafaret should be instance of Dict or Forward' with pytest.raises(RuntimeError) as exc_info: @guard(t.Dict(t.Key('a', trafaret=t.Bytes)), a=t.ToInt) def fn(**kw): return kw assert exc_info.value.args[0] == 'choose one way of initialization, trafaret or kwargs'
Example #20
Source File: test_base.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_base2(self): trafaret = t.Dict({t.Key('bar', optional=True): t.String}, foo=t.ToInt) trafaret = trafaret.allow_extra('*') res = trafaret.check({"foo": 1, "ham": 100, "baz": None}) assert res == {'baz': None, 'foo': 1, 'ham': 100} res = extract_error(trafaret, {"bar": 1, "ham": 100, "baz": None}) assert res == {'bar': 'value is not a string', 'foo': 'is required'} res = extract_error(trafaret, {"foo": 1, "bar": 1, "ham": 100, "baz": None}) assert res == {'bar': 'value is not a string'}
Example #21
Source File: test_keys.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_confirm_key(self): trafaret = t.Dict(confirm_key('password', 'password_confirm', t.String())) res = trafaret({'password': u'qwerty', 'password_confirm': u'qwerty'}) assert res == {'password': u'qwerty', 'password_confirm': u'qwerty'} res = catch_error(trafaret, {'password_confirm': u'qwerty'}) assert res.as_dict() == {'password': 'is required'} res = catch_error(trafaret, {'password': u'qwerty'}) assert res.as_dict() == {'password_confirm': 'is required'} res = catch_error(trafaret, {'password': u'qwerty', 'password_confirm': u'not qwerty'}) assert res.as_dict() == {'password_confirm': 'must be equal to password'}
Example #22
Source File: constructor.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
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.ToInt() elif arg is float: return t.ToFloat() 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 #23
Source File: ql.py From flask-graphql-example with MIT License | 5 votes |
def mutate(cls, _, info, __): logger.debug("agrs %s", info) user_schema = t.Dict({ 'email': t.String(min_length=2), 'first_name': t.String(min_length=2), 'last_name': t.String(min_length=2), }) user_data = user_schema.check(info) user = User.objects.create(**user_data) user.save() return cls(user=construct(UserField, user))
Example #24
Source File: ql.py From flask-graphql-example with MIT License | 5 votes |
def mutate(cls, _, info, __): logger.debug("agrs %s", info) comment_schema = t.Dict({ 'name': t.String(min_length=2, max_length=30), 'post_id': t.String(min_length=2, max_length=30), 'content': t.String(min_length=2), }) comment_data = comment_schema.check(info) post_id = comment_data.pop('post_id') post = Post.objects.get_or_404(id=post_id) comment = Comment(**comment_data) post.comments.append(comment) post.save() return cls(post=construct(PostField, post), comment=construct(CommentField, comment))
Example #25
Source File: test_trafaret.py From pydantic with MIT License | 5 votes |
def __init__(self, allow_extra): self.schema = t.Dict({ 'id': t.Int(), 'client_name': t.String(max_length=255), 'sort_index': t.Float, # t.Key('client_email', optional=True): t.Or(t.Null | t.Email()), t.Key('client_phone', optional=True): t.Or(t.Null | t.String(max_length=255)), t.Key('location', optional=True): t.Or(t.Null | t.Dict({ 'latitude': t.Or(t.Float | t.Null), 'longitude': t.Or(t.Float | t.Null), })), t.Key('contractor', optional=True): t.Or(t.Null | t.Int(gt=0)), t.Key('upstream_http_referrer', optional=True): t.Or(t.Null | t.String(max_length=1023)), t.Key('grecaptcha_response'): t.String(min_length=20, max_length=1000), t.Key('last_updated', optional=True): t.Or(t.Null | t.String >> parse), t.Key('skills', default=[]): t.List(t.Dict({ 'subject': t.String, 'subject_id': t.Int, 'category': t.String, 'qual_level': t.String, 'qual_level_id': t.Int, t.Key('qual_level_ranking', default=0): t.Float, })), }) if allow_extra: self.schema.allow_extra('*')
Example #26
Source File: test_async.py From trafaret with BSD 2-Clause "Simplified" License | 5 votes |
def test_key_with_callable_default(): trafaret = t.Dict( t.Key('a', default=lambda: 123, trafaret=t.ToInt), ) res = await trafaret.async_check({}) assert res == {'a': 123}
Example #27
Source File: vfolder.py From backend.ai-manager with GNU Lesser General Public License v3.0 | 5 votes |
def rename_file(request: web.Request, params: Any, row: VFolderRow) -> web.Response: folder_name = request.match_info['name'] access_key = request['keypair']['access_key'] log.info('VFOLDER.RENAME_FILE (ak:{}, vf:{}, target_path:{}, new_name:{})', access_key, folder_name, params['target_path'], params['new_name']) folder_path = get_folder_hostpath(row, request.app) ops = [] try: target_path = (folder_path / params['target_path']).resolve(strict=True) target_path.relative_to(folder_path) new_path = target_path.parent / params['new_name'] # Ensure new file is in the same directory. if len(params['new_name'].split('/')) > 1: raise InvalidAPIParameters('New name should not be a path: ' + params['new_name']) if new_path.exists(): raise InvalidAPIParameters('File already exists: ' + params['new_name']) except FileNotFoundError: raise InvalidAPIParameters('No such target file: ' + params['target_path']) except ValueError: raise InvalidAPIParameters('The requested path is out of the folder') ops.append(functools.partial(target_path.rename, new_path)) def _do_ops(): for op in ops: op() loop = current_loop() await loop.run_in_executor(None, _do_ops) resp: Dict[str, Any] = {} return web.json_response(resp, status=200)
Example #28
Source File: mongo_utils.py From aiohttp_admin with Apache License 2.0 | 5 votes |
def create_validator(schema, primary_key): # create validator without primary key, used for update queries # where pk supplied in url and rest in body keys = [s for s in schema.keys if s.get_name() != primary_key] return t.Dict().merge(keys).ignore_extra(primary_key)
Example #29
Source File: sa_utils.py From aiohttp_admin with Apache License 2.0 | 5 votes |
def table_to_trafaret(table, primary_key, skip_pk=False): trafaret = {} for name, column in table.c.items(): if skip_pk and column.name == primary_key: continue default = column.server_default key = build_key(name, default) traf_field = build_field(column) trafaret[key] = traf_field return t.Dict(trafaret).ignore_extra(primary_key)
Example #30
Source File: test_utils.py From aiohttp_admin with Apache License 2.0 | 5 votes |
def test_validate_payload(): raw_data = b'{"foo": "bar"}' schema = t.Dict({ t.Key('foo'): t.Atom('bar') }) data = validate_payload(raw_data, schema) assert data == {'foo': 'bar'}