Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created March 2, 2018 14:00
Show Gist options
  • Save kezabelle/c62ca930bc95c7ddc003fe53193048e5 to your computer and use it in GitHub Desktop.
Save kezabelle/c62ca930bc95c7ddc003fe53193048e5 to your computer and use it in GitHub Desktop.
Extremely hacky, just about seems to work for logging changes ... can't figure out the way to go with accesses though
class MutationObserver(wrapt.ObjectProxy):
def __init__(self, *args, **kwargs):
super(MutationObserver, self).__init__(*args, **kwargs)
self.__dict__['_self_changed'] = []
self.__dict__['_self_used'] = []
def __setattr__(self, key, value):
if key.startswith('_self_'):
self.__dict__[key] = value
else:
self.__dict__['_self_changed'].append((key, value))
super(MutationObserver, self).__setattr__(key, value)
def __repr__(self):
return '<MutationObserver wrapping {0!r}; seen mutations are: {1!r}>'.format(
self.__wrapped__,
self._self_changed,
)
def changed_keys(self):
return sorted({x[0] for x in self._self_changed})
#
# def used_keys(self):
# return sorted({x[0] for x in self._self_used})
#
# def __getattribute__(self, item):
# if item.startswith('_self_'):
# return super(MutationObserver, self).__getattribute__(item)
# elif item == "__dict__":
# return super(MutationObserver, self).__getattribute__(item)
# elif item == "__wrapped__":
# return super(MutationObserver, self).__getattribute__(item)
# else:
# self.__dict__['_self_used'].append(item)
# return super(MutationObserver, self).__getattribute__(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment