question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Confused - how to use List/Nested

See original GitHub issue

Can 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:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lafrechcommented, Oct 9, 2018

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.

0reactions
nazariyvcommented, Oct 9, 2018

Thanks for your help!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found