undo redo.py :  » Database » PyTables » tables-2.1.2 » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » PyTables 
PyTables » tables 2.1.2 » examples » undo-redo.py
"""Yet another couple of examples on do/undo feauture."""

import tables

def setUp(filename):
    # Create an HDF5 file
    fileh = tables.openFile(filename, mode = "w", title="Undo/Redo demo")
    # Create some nodes in there
    fileh.createGroup("/", "agroup", "Group 1")
    fileh.createGroup("/agroup", "agroup2", "Group 2")
    fileh.createArray("/", "anarray", [1,2], "Array 1")
    # Enable undo/redo.
    fileh.enableUndo()
    return fileh

def tearDown(fileh):
    # Disable undo/redo.
    fileh.disableUndo()
    # 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.createArray('/', 'otherarray1', [3,4], "Another array 1")
    fileh.createArray('/', 'otherarray2', [4,5], "Another array 2")
    # Put a mark
    fileh.mark()
    fileh.createArray('/', 'otherarray3', [5,6], "Another array 3")
    fileh.createArray('/', 'otherarray4', [6,7], "Another array 4")
    # Put a mark
    fileh.mark()
    fileh.createArray('/', 'otherarray5', [7,8], "Another array 5")
    fileh.createArray('/', '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
    array2 = fileh.createArray(fileh.root, 'anarray3',
                                    [3], "Array title 3")
    # Create a group
    array2 = fileh.createGroup(fileh.root, 'agroup3',
                                    "Group title 3")
    # /anarray => /agroup/agroup3/
    newNode = fileh.copyNode('/anarray3', '/agroup/agroup2')
    newNode = fileh.copyChildren('/agroup', '/agroup3', recursive=1)
    # rename anarray
    array4 = fileh.renameNode('/anarray', 'anarray4')
    # Move anarray
    newNode = fileh.copyNode('/anarray3', '/agroup')
    # Remove anarray4
    fileh.removeNode('/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 newNode
    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()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.