/usr/share/doc/python-tables/examples/undo-redo.py is in python-tables-doc 3.4.2-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | """Yet another couple of examples on do/undo feauture."""
import tables
def setUp(filename):
# Create an HDF5 file
fileh = tables.open_file(filename, mode="w", title="Undo/Redo demo")
# Create some nodes in there
fileh.create_group("/", "agroup", "Group 1")
fileh.create_group("/agroup", "agroup2", "Group 2")
fileh.create_array("/", "anarray", [1, 2], "Array 1")
# Enable undo/redo.
fileh.enable_undo()
return fileh
def tearDown(fileh):
# Disable undo/redo.
fileh.disable_undo()
# Close the file
fileh.close()
def demo_6times3marks():
"""Checking with six ops and three marks."""
# Initialize the data base with some nodes
fileh = setUp("undo-redo-6times3marks.h5")
# Create a new array
fileh.create_array('/', 'otherarray1', [3, 4], "Another array 1")
fileh.create_array('/', 'otherarray2', [4, 5], "Another array 2")
# Put a mark
fileh.mark()
fileh.create_array('/', 'otherarray3', [5, 6], "Another array 3")
fileh.create_array('/', 'otherarray4', [6, 7], "Another array 4")
# Put a mark
fileh.mark()
fileh.create_array('/', 'otherarray5', [7, 8], "Another array 5")
fileh.create_array('/', 'otherarray6', [8, 9], "Another array 6")
# Unwind just one mark
fileh.undo()
assert "/otherarray1" in fileh
assert "/otherarray2" in fileh
assert "/otherarray3" in fileh
assert "/otherarray4" in fileh
assert "/otherarray5" not in fileh
assert "/otherarray6" not in fileh
# Unwind another mark
fileh.undo()
assert "/otherarray1" in fileh
assert "/otherarray2" in fileh
assert "/otherarray3" not in fileh
assert "/otherarray4" not in fileh
assert "/otherarray5" not in fileh
assert "/otherarray6" not in fileh
# Unwind all marks
fileh.undo()
assert "/otherarray1" not in fileh
assert "/otherarray2" not in fileh
assert "/otherarray3" not in fileh
assert "/otherarray4" not in fileh
assert "/otherarray5" not in fileh
assert "/otherarray6" not in fileh
# Redo until the next mark
fileh.redo()
assert "/otherarray1" in fileh
assert "/otherarray2" in fileh
assert "/otherarray3" not in fileh
assert "/otherarray4" not in fileh
assert "/otherarray5" not in fileh
assert "/otherarray6" not in fileh
# Redo until the next mark
fileh.redo()
assert "/otherarray1" in fileh
assert "/otherarray2" in fileh
assert "/otherarray3" in fileh
assert "/otherarray4" in fileh
assert "/otherarray5" not in fileh
assert "/otherarray6" not in fileh
# Redo until the end
fileh.redo()
assert "/otherarray1" in fileh
assert "/otherarray2" in fileh
assert "/otherarray3" in fileh
assert "/otherarray4" in fileh
assert "/otherarray5" in fileh
assert "/otherarray6" in fileh
# Tear down the file
tearDown(fileh)
def demo_manyops():
"""Checking many operations together."""
# Initialize the data base with some nodes
fileh = setUp("undo-redo-manyops.h5")
# Create an array
fileh.create_array(fileh.root, 'anarray3', [3], "Array title 3")
# Create a group
fileh.create_group(fileh.root, 'agroup3', "Group title 3")
# /anarray => /agroup/agroup3/
new_node = fileh.copy_node('/anarray3', '/agroup/agroup2')
new_node = fileh.copy_children('/agroup', '/agroup3', recursive=1)
# rename anarray
fileh.rename_node('/anarray', 'anarray4')
# Move anarray
new_node = fileh.copy_node('/anarray3', '/agroup')
# Remove anarray4
fileh.remove_node('/anarray4')
# Undo the actions
fileh.undo()
assert '/anarray4' not in fileh
assert '/anarray3' not in fileh
assert '/agroup/agroup2/anarray3' not in fileh
assert '/agroup3' not in fileh
assert '/anarray4' not in fileh
assert '/anarray' in fileh
# Redo the actions
fileh.redo()
# Check that the copied node exists again in the object tree.
assert '/agroup/agroup2/anarray3' in fileh
assert '/agroup/anarray3' in fileh
assert '/agroup3/agroup2/anarray3' in fileh
assert '/agroup3/anarray3' not in fileh
assert fileh.root.agroup.anarray3 is new_node
assert '/anarray' not in fileh
assert '/anarray4' not in fileh
# Tear down the file
tearDown(fileh)
if __name__ == '__main__':
# run demos
demo_6times3marks()
demo_manyops()
|