Skip to main content

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
undoable-0.3.1.tar.gz 4.8 kB Details

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

Release history Release notifications | RSS feed

This release

0.3.1 This release

1 release file

0.3

1 release file

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