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

how to resolve values in custom constructors #33

Open
vielleicht opened this issue Jul 7, 2024 · 0 comments
Open

how to resolve values in custom constructors #33

vielleicht opened this issue Jul 7, 2024 · 0 comments

Comments

@vielleicht
Copy link

Dynamic resolution in values seems quite handy to me. I tried to add custom constructors, but failed quickly, as the values do not get resolved anymore - at least it doesn't work in my example:

#!/usr/bin/env python3
from typing import Any
import dynamic_yaml

class Command():
    def __init__(self, value):
        self.value = value

    def __call__(self, *args: Any, **kwds: Any) -> Any:
        return self.value

def command_constructor(loader, node):
    value = loader.construct_scalar(node)
    print(value)
    return Command(value)

dynamic_yaml.DynamicYamlLoader.add_constructor("!command", command_constructor)

yaml = '''
a: name
b: !command test/{a}
'''

cfg = dynamic_yaml.load(yaml)
print(cfg.b())

please give advice, how this could work.

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

1 participant