A general purpose library to help implement undo.
Project description
jundo
A general purpose library to help implement undo for existing objects.
The main idea is that if one limits oneself to objects that can be viewed as JSON-like data structures (ie. are composed of strings, numbers, lists and dictionaries) it is possible to record changes in a completely generic way, and use these recordings to reconstruct objects or to roll back changes, for example to implement undo.
A key requirement was that the model objects must be completely decoupled from the recording mechanism, and will not need any awareness of it.
The way this is implemented here is through proxy objects: instead of modifying the original model objects directly, one uses proxy objects that mimic the model object, allowing the proxy to pass on change information (deltas) to an undo manager.
The undo manager collects change deltas (and their inverses), and can apply them to the original model object when undo or redo is requested.
The model object is passed to the undo manager with the
undoManager.setModel(model)
, which returns a proxy for the model. Client code
must use the proxy object instead of the model to modify the model. Here is an
example:
>>> model = [1, 2, 3, {"a": 123}]
>>> um = UndoManager()
>>> proxy = um.setModel(model)
>>> # Modifications must be done within a change set context:
>>> with um.changeSet(title="replace list item"):
... proxy[1] = 2000
...
>>> model[1]
2000
>>> um.undo()
>>> model[1]
2
>>> um.redo()
>>> model[1]
2000
>>> with um.changeSet(title="replace nested dict item"):
... proxy[3]["a"] = 456
...
>>> model[3]["a"]
456
>>> um.undo()
>>> model[3]["a"]
123
In this example, only Python list and dict objects are used as containers, but any type of Mapping or Sequence (in the collections.abc-sense) can be used, or any object type that uses attribute access to modify its model data. See the Examples folder for more elaborate examples.
Sets are also supported.
To support model objects that are not JSON-like, custom proxy classes can be
registered via the registerUndoProxy()
function.
Acknowledgments
The approach implemented here is inspired by Raph Levien's ideas, as he wrote them down here. It also draws inspiration from jsonpatch.
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
Built Distribution
File details
Details for the file jundo-0.1.2.tar.gz
.
File metadata
- Download URL: jundo-0.1.2.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0507d0b6692e94de03d49efe1d14b9375dbcaa05b8cb8a3d172126dc3c160929 |
|
MD5 | 8390533d31d0e2470fe4e4da18f1bf54 |
|
BLAKE2b-256 | b41e2adf78991d2167f87f26c37ecc9cffe4363d19f5af85a0508ee892077967 |
File details
Details for the file jundo-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: jundo-0.1.2-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a253206e834a8d61361f89fee685c8597e3737266dc6df9b471cd3263a95b42 |
|
MD5 | 778a0b980ecc651b967763ac1c97a82f |
|
BLAKE2b-256 | 9edf81f85aa42fdd444fda682dfa8a409678a4bfaf6f2d305636de7b13811f8c |