Skip to main content

Undoable

A simple Python list (observed_list) and dict (observed_dict) with callbacks when they are changed and undo/redo using these callbacks.

Also includes observed_tree, a labelled tree with ordered children implemented as Python list with parent pointers and a bit of consistency check.

Implementation

Implemented using the command pattern. See my blog post discussing this.

Example

pip install undoable

Callbacks

>>> from undoable import observed_dict, observed_list
>>> def printargs(*args):
...     print args
...
>>> l = observed_list([1, 2, 3])
>>> l.callbacks.append(printargs)
>>> l.append(4)
([1, 2, 3, 4], 'append', 4)
>>> l.extend([5, 6, 7])
([1, 2, 3, 4, 5, 6, 7], 'extend', [5, 6, 7])
>>> d = observed_dict({1: "one", 2: "two"})
>>> d.callbacks.append(printargs)
>>> d[3] = "three"
({1: 'one', 2: 'two', 3: 'three'}, '__setitem__', 3, 'three')
>>> d2 = observed_dict()
>>> d2.undocallbacks.append(printargs)
>>> d2[1] = "one"
({1: 'one'}, ('__delitem__', 1), ('__setitem__', 1, 'one'))

Undo/redo

>>> from undoable import UndoLog, observed_dict, observed_list
>>> u = UndoLog()
>>> d = observed_dict({1: "one", 2: "two"})
>>> l = observed_list([1, 2, 3])
>>> u.add(d)
>>> u.add(l)
>>> l.append(1)
>>> d[3] = "Hello"
>>> l
[1, 2, 3, 1]
>>> d
{1: 'one', 2: 'two', 3: 'Hello'}
>>> u.undo()
>>> d
{1: 'one', 2: 'two'}
>>> u.undo()
>>> l
[1, 2, 3]
>>> u.redo()
>>> u.redo()
>>> l
[1, 2, 3, 1]
>>> d
{1: 'one', 2: 'two', 3: 'Hello'}
>>> u.start_group("foo")
True
>>> d[53] = "user"
>>> del d[1]
>>> u.end_group("foo")
>>> d
{2: 'two', 3: 'Hello', 53: 'user'}
>>> u.undo()
>>> d
{1: 'one', 2: 'two', 3: 'Hello'}

Metadata

Release files for undoable 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for undoable 0.3.1
File Size Uploaded Provenance Actions
undoable-0.3.1.tar.gz 4.8 kB Without provenance Details for undoable-0.3.1.tar.gz

Release files / undoable-0.3.1.tar.gz

Download URL undoable-0.3.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
57db8770cf5f27fb816784b7bbd03a1dfcd54730807a724dfe2ea4e452e09988
BLAKE2b-256 checksum
How to use checksums
5447939631e14bec61f1cc44c7084b92f0555381d9b2930f15528709822879c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for undoable 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page