![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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...
file = open ("patterns.dat", "rb")
tree = pickle.load (file)
file.close ()
...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 |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Interesting! You pickle dumped and loaded a list of class instances.
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Can you pickle a class directly rather than its instance?
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
|
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Thanks Arevos, I think the easier way would be to import the class as a module.
__________________
I looked it up on the Intergnats! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |