Confused - how to use List/Nested
See original GitHub issueCan you help me understand how to use the structure like below to achieve something like this:
{"config":{"factor_1":[{"comparator":"<=", "value":"20"},{"comparator":">=", "value":"10"}],
"factor_2":[{"comparator":"<=", "value": "PE"}],
"factor_3":[{"comparator":"exactly", "value": "AAA+"}]}
class ComparatorValue(Schema):
comparator = fields.Str(required=True)
value = fields.Str(required=True)
@validates('comparator')
def validate_comprator(self, comparator):
if comparator not in comparators:
raise ValidationError("Invalid comparator: {}. Allowed values: {}".format(comparator, comparators))
class Meta:
strict = True
class ConfigSchema(Schema):
"""
{"config":{"factor_1":[{"comparator":"<=", "value":"20"},{"comparator":">=", "value":"10"}],
"factor_2":[{"comparator":"<=", "value": "PE"}],
"factor_3":[{"comparator":"exactly", "value": "AAA+"}]}
"""
config = fields.Dict(keys=fields.Str(required=True),
values=fields.Nested(ComparatorValue), required=True)
# comparator = fields.Str()
# # value = fields.Str(required=True)
#
@validates('config')
def validate_config(self, config):
keys = list(config.keys())
vals = list(config.values())
for key in keys:
if key != '':
raise ValidationError("Factor name cannot be empty.")
# for val in vals:
# if
class Meta:
strict = True
def get_api():
class BondScreenApi(Resource):
@use_args(ConfigSchema())
def post(self, args):
# comparator = args['comparator']
# value = args['value']
print(args['config'])
print("Posted args: {}".format(args))
return {"Got": "Through"}, 201
return BondScreenApi
As soon as I add Nested/List, it stops working, as in it successfully goes through all of the validation, even though the args are not correct. For example, if for values we have fields.List(Nested(ComparatorValue))
(this is actually what I need, I’ve given above a minimal example), then any request where the data is like: {"config": {"yo":"got through"}}
goes through, even though the value of “yo” is supposed to be a List(Nested(...))
. I am clearly misunderstanding how to use it and I cannot find a lot of examples on the Internet.
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Confused with nested list - python - Stack Overflow
What it's really doing is first creating a list of False (and this is ok, as False is a value type), but then...
Read more >Confusion with Nested List : r/learnpython - Reddit
So I was going through the Udemy Python course by Angela Yu - Master Python by building 100 projects in 100 days. I...
Read more >A Helpful Illustrated Guide to Nested Lists in Python - Finxter
Solution: Use the np.array(list) function to convert a list of lists into a two-dimensional NumPy array. Here's the code:.
Read more >Python 3.7: Nested List In Python - YouTube
In this Python 3.7 tutorial, we will take a tour of the nested list in Python. We will also look at how to...
Read more >Create and Style Nested Lists - YouTube
Nest list items within other list items to create a nested list, or outline. The technique is also used for creating cascading navigation ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Depends on your project. Looks like you’re starting something so go for it. Just beware of the updates, pin your versions, read the changelog. We hope to release a 3.0 soonish.
Thanks for your help!