Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 8th, 2006, 6:33 PM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Pickle a Class

Does anybody have a good example of using a pickle dump and load on a class?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Nov 8th, 2006, 7: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 7:02 PM. Reason: addition
DaWei is offline   Reply With Quote
Old Nov 10th, 2006, 1:37 AM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Interesting! You pickle dumped and loaded a list of class instances.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Nov 14th, 2006, 12:13 PM   #4
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Can you pickle a class directly rather than its instance?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Nov 14th, 2006, 12:45 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Haven't tried it; should take you a couple minutes to find out. Why would you want to pickle a class definition?
__________________
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
DaWei is offline   Reply With Quote
Old Nov 14th, 2006, 2:36 PM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Quote:
Originally Posted by Dietrich View Post
Can you pickle a class directly rather than its instance?
No, at least not directly. There may be a way to do it indirectly, perhaps... Though, as DaWei says, why would you want to pickle a class in the first place?
Arevos is offline   Reply With Quote
Old Nov 16th, 2006, 6:39 PM   #7
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
I thought that if you pickle the instance of a class and load it into another program you have to supply the class.

Somewhere I found this note:
"use class methods __getstate__ and __setstate__ to pickle a class"

Pickle allows one to pickle any object, right?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Nov 16th, 2006, 6:42 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'm still not saying you can't do it (I haven't checked), but you have to ask yourself if a class definition is an object. A blueprint is not a house or a lawnmower.
__________________
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
DaWei is offline   Reply With Quote
Old Nov 17th, 2006, 2:43 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Well, in Python a class definition is an object. But class objects tend to have complications associated with them. Pickling a normal object instance is just a matter of looking at its __dict__ and then turning all its variables into strings. Pickling a class would involve including Python bytecode as well, which is somewhat harder.

Setting __getstate__ and __setstate__ on an object overrides Pickle's default serialization and deserialization routines, allowing you to define manually how your object is turned into a string. Other than that, you could play around with the code and code.compile modules, but they tend to be rather sparse on documentation.

Is there a reason you really need to pickle a class? Could it not conceivably be done another way?
Arevos is offline   Reply With Quote
Old Nov 17th, 2006, 1:38 PM   #10
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Thanks Arevos, I think the easier way would be to import the class as a module.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd compile Error. Need help please. Keiyentai Java 7 Aug 19th, 2006 1:35 AM
URL class Eric the Red Java 5 Jun 24th, 2006 9:01 PM
What is: "Oriented programming (OO)?" BrinyCode C++ 12 Nov 22nd, 2005 7:40 AM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 3:41 PM
MFC/OpenGL: problem with 'Document' class brenda C++ 11 May 23rd, 2005 8:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC