Skip to content

Instantly share code, notes, and snippets.

@bahostetterlewis
Last active December 14, 2015 11:19
Show Gist options
  • Save bahostetterlewis/5078269 to your computer and use it in GitHub Desktop.
Save bahostetterlewis/5078269 to your computer and use it in GitHub Desktop.
Proof of concept for an undo and redo function using python functions. This is for use in the pyrrhic-ree project. By using properties it is possible to build the undo stack just by setting the string. Once a stack is built calling the undo function builds a redo stack that is emptied once a person decides to type manually. I don't think there i…
class Proof:
def __init__(self):
self._redo = []
self._undo = []
self._mystr = ''
def mystr():
def fget(self):
return self._mystr
def fset(self, value):
self._undo.append(self.buildStrRevert())
self._redo = [] # empty redo ability when string is set
self._mystr = value
return locals()
mystr = property(**mystr())
def undo(self):
if self._undo:
self._redo.append(self.buildStrRevert())
self._undo.pop()()
def redo(self):
if self._redo:
self._undo.append(self.buildStrRevert())
self._redo.pop()()
def myostr():
def fget(self):
return self._myostr
def fset(self, value):
self._undo.append(self.buildStrRevert())
self._redo = [] # empty redo ability when string is set
self._myostr = value
return locals()
myostr = property(**myostr())
#builds a function that will use a closure
- #to save and revert the strings in one simple action
#The closure maintains the state before the change.
#It would be possible to build a more complex system using
#multiple function wrappers, but this allows for a uniform
#interface with only 2 stacks.
def buildStrRevert(self):
oldMyStr = self._mystr
oldMyOStr = self._myostr
def strRevert():
self._mystr = oldMyStr
self._myostr = oldMyOStr
return strRevert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment