cpickle and classes

Guy guy at lightwork.co.uk
Tue Jul 2 07:30:49 EDT 2002


Been reading alot of the messages posted to the python groups about
cpickle, (old and new). Below I've got an exsample bit of code.(Please
don't comment on this code its just AN EXAMPLE.)

----------------------------------------------------------------------------

import cPickle

class mill:
    time = ""
    date = ""
    name = ""
    flourUsage = flour()    
    add =[""]

class flour:
    ammount = ""
    date = ""

data = mill()

data.time = "1"
data.date = "2"
data.name = "3"
data.flourUsage.ammount = "4"
data.flourUsage.data = "5"
data.add=["adsfads","dsgdsfgdf"]

Data001 = cPickle.dumps(data, 1)
Data002 = cPickle.loads(Data001)

print Data002.time
print Data002.date
print Data002.name
print Data002.flourUsage.ammount
print Data002.flourUsage.date

file = open(os.getcwd()+"\\test.dump", "w")
cPickle.dump(Data002, file)
file.close()

---------------------------------------------------------------------------

When this is run it puts some stuff to screen and file :

Screen display :

>>> 1
>>> 2
>>> 3
>>> 4
>>> 5

File display :

 (i__main__
mill
p1
(dp2
S'date'
S'2'
sS'add'
(lp3
S'adsfads'
aS'dsgdsfgdf'
asS'name'
S'3'
sS'time'
S'1'
sb.

If you look at the file there is NO sign of the
Data002.flourUsage.ammount
and Data002.flourUsage.date values or var names. 

Why ? 
Can cpickle not handle this ?
Is there anything I can do to improve the code to make cpickle work ?
Are there any work arounds for this ?
Maybe suggestions of how to do this in a different way.



More information about the Python-list mailing list