0

In an existing python script I use vivification for work with dictionary of dictionaries.

class AutoVivification(dict):
        """Implementation of perl's autovivification feature."""

        def __init__(self, *args, **kwargs):
                super(AutoVivification, self).__init__(*args, **kwargs)
                self.vivify = True
                self.root = self

        def __getitem__(self, item):

                try:
                        return dict.__getitem__(self, item)

                except KeyError:
                        if not self.root.vivify:
                                raise
                        value = self[item] = type(self)()
                        value.root = self.root
                        return value

        def unvivify(self):
                self.vivify = False

        def revivify(self):
                self.vivify = True

        def upgrade(self):
                self.root = self

I would like apply this class for each dictionary in a dictionary of dictionaries. I thought to something like that:

def upvivification(dico):
        """Upgrade to the last autovivification version."""

        if isinstance(dico, dict):
                dico = AutoVivification(dico)

                for k in dico:

                        if isinstance(dico[k], dict):
                                upvivification(dico[k])

But, indeed it is not working because the scope of this change is in the function and not global ... I do not see how to recursively make this change ...

servoz
  • 606
  • 9
  • 22

2 Answers2

1

Just return dico in the end and reassign the return value in your recursive call:

def upvivification(dico):
    if isinstance(dico, dict):
        dico = AutoVivification(dico)
            for k in dico:
                if isinstance(dico[k], dict):
                    dico[k] = upvivification(dico[k])
        return dico

Then use like:

some_dict = upvivification(some_dict)

It is only possible to change the type of an existing object mutation-style with great pain and care. You can, however, reassign the old variable to the new object.

Community
  • 1
  • 1
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • Oh great !!!! Exactly what I looked for ... and I think I need to sleep ... thanks a lot .... – servoz Jan 17 '17 at 16:49
1
def upvivification(dico):
        """Upgrade to the last autovivification version."""

        if isinstance(dico, dict):
                dico = AutoVivification(dico)

                for k in dico:

                        if isinstance(dico[k], dict):
                                dico[k] = upvivification(dico[k])
        return deco

And use it like

new_dico = upvivification(dico)
gipsy
  • 3,859
  • 1
  • 13
  • 21
  • Oh great !!!! Exactly what I looked for ... and I think I need to sleep ... thanks a lot .... – servoz Jan 17 '17 at 16:49