Skip to content

Instantly share code, notes, and snippets.

@glyg
Created April 14, 2017 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glyg/5087c6f919eb0e2a411065ec64909f46 to your computer and use it in GitHub Desktop.
Save glyg/5087c6f919eb0e2a411065ec64909f46 to your computer and use it in GitHub Desktop.
def MyClass()
def __init__(self, data):
self._backups = deque()
self.data = data
def copy(self):
return MyClass(self.data)
def backup(self):
"""Creates a copy of self and keeps a reference to it
in the self.backups stack.
"""
print('backing up')
self._backups.append(self.copy())
def restore(self):
print('Restoring')
# This doesn't seem to work
self = self._backups.pop()
@glyg
Copy link
Author

glyg commented Apr 14, 2017

here is the function decorator that comes with it:

def do_undo(func):
    """Decorator that creates a copy of the first argument
    (usually an epithelium object) and restores it if the function fails.

    The first argument in `*args` should have a `backup()` method.
    """
    def with_bckup(*args, **kwargs):
        eptm = args[0]
        eptm.backup()
        try:
            print('Trying ...')
            return func(*args, **kwargs)
        except Exception as err:
            print('Something bad happened, reverting')
            eptm.restore()
            raise err
    return with_bckup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment