General undo/redo framework for Python
Project description
collections-undo
General undo/redo implementation in Python.
Installation
pip install -U collections-undo
Basic usage
You'll have to create reversible functions using UndoManager
.
from collections_undo import UndoManager
class A:
mgr = UndoManager()
@mgr.undoable # create a reversible function
def f(self, x):
print("do", x)
@f.undo_def # define undo
def f(self, x):
print("undo", x)
a = A()
a.f(0) # Out: "do" 0
a.mgr.undo() # Out: "undo" 0
a.mgr.redo() # Out: "do" 0
ABC-like undo implementation
To avoid careless errors, ABC-like interface will be useful.
from collections_undo.abc import UndoableABC, undoablemethod, undo_def
class A(UndoableABC):
@undoablemethod
def f(self, x):
print("do", x)
@undo_def(f)
def f(self, x):
print("undo", x)
a = A() # OK
a.f(0) # Out: "do" 0
a.undo() # Out: "undo" 0
a.redo() # Out: "do" 0
If undo is not defined, construction fails.
class A(UndoableABC):
@undoablemethod
def f(self, x):
...
a = A() # TypeError
class B(A):
@undo_def(A.f)
def f(self, x):
...
b = B() # OK
Builtin undoable objects
These mutable classes have undo
and redo
method to handle operations that mutate the object.
-
Ready-to-use classes
UndoableList
...insert
,__setitem__
,extend
etc. are undoable.UndoableDict
...__setitem__
,update
etc. are undoable.UndoableSet
...add
,discard
etc. are undoable.
-
Abstract classes
AbstractUndoableList
AbstractUndoableDict
AbstractUndoableSet
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
collections-undo-0.0.2.tar.gz
(14.2 kB
view hashes)
Built Distribution
Close
Hashes for collections_undo-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 102ad59ecd02965a1fdbbaf90445d89eac4c299bb03a4eb5aaf764da4a649bd3 |
|
MD5 | 4a0185e0199f582123ce95ea9549a3d0 |
|
BLAKE2b-256 | a2c07d2cdf4a6bc321bcbbd0e6d7dc89e7deec2ba19c9336c3d4f25c4ac34281 |