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

Added RecursiveMunch object. #58

Merged
merged 3 commits into from Dec 16, 2019
Merged

Added RecursiveMunch object. #58

merged 3 commits into from Dec 16, 2019

Conversation

GuillaumeRochette
Copy link

Hi,

I felt the need to have a RecursiveMunch class, which is similar to DefaultFactoryMunch, except that it creates an instance of itself to generate values for missing keys, instead of a user-specified function.

Regards,

Guillaume Rochette

@ayalash
Copy link

ayalash commented Nov 28, 2019

@GuillaumeRochette Thanks for contributing.

Copy link

@ayalash ayalash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RecursiveMunch code is duplication of DefaultFactoryMunch code, except for the value of default_factory member which is automatically set In RecursiveMunch.

Code duplication is a bad practice, please reuse the common code.

@ayalash ayalash merged commit 93f179e into Infinidat:master Dec 16, 2019
@ayalash
Copy link

ayalash commented Dec 16, 2019

Thanks

ayalash pushed a commit that referenced this pull request Dec 16, 2019
PR #58 was merged to master instead of develop. Deleting it from master and adding to develop
@GuillaumeRochette
Copy link
Author

Hi,
Sorry, I've realised that I did not overload the copy() method, which causes an error.
Here is a corrected version:

class RecursiveMunch(DefaultFactoryMunch):
    """A Munch that calls an instance of itself to generate values for
        missing keys.

        >>> b = RecursiveMunch({'hello': 'world!'})
        >>> b.hello
        'world!'
        >>> b.foo
        RecursiveMunch(RecursiveMunch, {})
        >>> b.bar.okay = 'hello'
        >>> b.bar
        RecursiveMunch(RecursiveMunch, {'okay': 'hello'})
        >>> b
        RecursiveMunch(RecursiveMunch, {'hello': 'world!', 'foo': RecursiveMunch(RecursiveMunch, {}),
        'bar': RecursiveMunch(RecursiveMunch, {'okay': 'hello'})})
    """

    def __init__(self, *args, **kwargs):
        super(RecursiveMunch, self).__init__(RecursiveMunch, *args, **kwargs)

    @classmethod
    def fromDict(cls, d):
        # pylint: disable=arguments-differ
        return munchify(d, factory=cls)

    def copy(self):
        return type(self).fromDict(self)

@ayalash
Copy link

ayalash commented Dec 19, 2019

@GuillaumeRochette Fixed
Thanks again

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

Successfully merging this pull request may close these issues.

None yet

2 participants