Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to define schema for recursive structure? #119

Closed
AlexandreDecan opened this issue Sep 6, 2016 · 7 comments
Closed

Question: how to define schema for recursive structure? #119

AlexandreDecan opened this issue Sep 6, 2016 · 7 comments

Comments

@AlexandreDecan
Copy link

AlexandreDecan commented Sep 6, 2016

Hello,

I've a question about the definition of a schema for a recursive structure. Say for example I want to encode a person which has a firstname, a lastname, and a list of children which are person too.
How can I do that?

Basically, I would like to do:

person = {'firstname': str, 'lastname': str, Optional('children'): [person]}

Obviously, this does not work. Is there a solution?

Currently, I do something like:

person = {'firstname': str, 'lastname': str}
person[Optional('children')] = [person]

... but I find it not really readable...

@skorokithakis
Copy link
Collaborator

I was just going to post that but you updated the post. Does the recursion you tried work well? I think that is the most reasonable way to do it, currently. I agree that it's not very readable, but that's a Python issue, not a schema issue...

@AlexandreDecan
Copy link
Author

Yes, it works. But in case of a validation error, the error message is not very useful/accurate, but that's another issue ;-)

Btw, thanks for your answer!

@miso-belica
Copy link

@keleshev Would be nice to have this in documentation. I used library voluptuous just because I thought this is not possible with Schema.

@AlexandreDecan
Copy link
Author

Indeed. I also looked at voluptuous, but I didn't like much its "non pythonic" approach to declare schema.

@RagingRoosevelt
Copy link

Wait, so does this actually work? I tried doing (for example)

[...]

Person = Schema(
    {
        'name': str
        ,'gender': str
    }
    , name='Person', as_reference=True
)
person['children'] = [person]

and I got an error message of TypeError: 'Schema' object does not support item assignment. Am I missing something, or was this just a proposed way to do this?

@AlexandreDecan
Copy link
Author

Create and store the inner dict separately, following this idea:

person = dict()
person.update({'name': str, 'gender': str, 'children': [person])

@RagingRoosevelt
Copy link

Ah, ok, that makes sense.

For anyone finding this in the future:

>>>from schema import Schema
>>>person = dict()
>>>Person = Schema(person)
>>>person.update({'name': str, 'children': [Person]})

>>>print(Person)
Schema({'name': <class 'str'>, 'children': [Schema({...})]})

>>>print(Person.schema.get('children'))
[Schema({'name': <class 'str'>, 'children': [Schema({...})]})]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants