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

Help regarding custom yaml #44

Open
Fuhrmann opened this issue Apr 22, 2020 · 1 comment
Open

Help regarding custom yaml #44

Fuhrmann opened this issue Apr 22, 2020 · 1 comment

Comments

@Fuhrmann
Copy link

Hi! I'm trying to use your library with a custom yaml file I have:

devices:
    left-keyboard:  000123456789
    right-keyboard: 000987654321

How could I represent this yaml in a class? I've tried this:

@related.immutable
class Device(object):
    name = related.StringField()

@related.immutable
class Settings(object):
    devices = related.MappingField(Device, "name")

But it gives me an error: TypeError: 000123456789 is not an instance of <class '__main__.Device'>

I've tried using SetField, SequenceField and others in this case but to no success.

@M0r13n
Copy link

M0r13n commented Oct 26, 2021

@Fuhrmann

You could use the following model:

import related


@related.immutable
class Settings(object):
    devices = related.MappingField(str, "name")

Explanation:

Your YAML file is syntactically equivalent to the following Dictionary:

{
    'devices': {
        'left-keyboard': '000123456789',
        'right-keyboard': '000987654321',
    }
}

So

{
        'left-keyboard': '000123456789',
        'right-keyboard': '000987654321',
 }

is passed to the MappingField. So 'left-keyboard' will be used as the name and 000123456789 to whatever class is passed to the cls parameter of MappingField - in our example this is str.

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

2 participants