Thread: Pickle a Class
View Single Post
Old Nov 8th, 2006, 8:01 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I used it to store the patterns tree for the TicTacToe game:
def makeSet ():
    '''
    Derive a set of Basic Patterns
    '''
    tree = [[Pattern ()]]
    count = 1
    for level in range (1, 10):
        ordinal = 0
        sym = (level % 2)
        if sym == 0: sym = 2
        tree.append ([])
        for i in range (len (tree [level-1])):
            # Each pattern in the previous set
            # Don't follow up on game end
            if tree [level-1][i].winner or tree [level-1][i].draw: continue
            for j in range (9):
                # Each possible square
                testpattern = tree [level-1][i].pattern [:]
                if testpattern [j] == 0: testpattern [j] = sym
                else: continue
                transform = ""

                for k in range (len (tree [level])):
                    # Each existing sibling
                    transform = equiv (testpattern, tree [level][k].pattern)
                    if transform != "": break

                if transform == "":
                    # New basic pattern
                    tree [level].append (Pattern (level, ordinal, [], []))
                    tree [level][ordinal].pattern = testpattern
                    tree [level][ordinal].parent = []
                    tree [level][ordinal].children = []
                    tree [level][ordinal].winner = checkWin (tree [level][ordinal])
                    tree [level][ordinal].draw = checkDraw (tree [level][ordinal])
                    tree [level-1][i].children.append ([ordinal, transform])
                    ordinal += 1
                else:
                    # Equivalent child exists with another parent
                    if tree [level-1][i].children.count (tree [level][k].ordinal) == 0:
                        # Record it with this parent, if not already present
                        tree [level-1][i].children.append ([tree [level][k].ordinal, transform])
    file = open ("patterns.dat", "wb")
    pickle.dump (tree, file)
    file.close ()
    return tree
...and to retrieve it:
...
file = open ("patterns.dat", "rb")
tree = pickle.load (file)
file.close ()
...
The class:
class Pattern:

    def __init__(self, level = 0, ordinal = 0, parent = [],
                 children = [], pattern = [0,0,0,0,0,0,0,0,0]):
        self.level = level
        self.ordinal = ordinal
        self.parent = parent
        self.children = children
        self.pattern = pattern
        self.wins = 0
        self.draws = 0
        self.losses = 0
        self.winner = 0
        self.draw = False
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers

Last edited by DaWei; Nov 8th, 2006 at 8:02 PM. Reason: addition
DaWei is offline   Reply With Quote