cpickle and classes

Steve Holden sholden at holdenweb.com
Tue Jul 2 08:55:02 EDT 2002


"Guy" <guy at lightwork.co.uk> wrote in message
news:c07d0edc.0207020330.5f0d9ec5 at posting.google.com...
> 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 :

No it doesn't.


Sorry, I have to comment on the code, because it just doesn't run as
presented. When the interpreter processes the definition of class mill it
calls class flour, which is undefined, so you get a NameError exception.
Given this failure it casts some doubt on the veracity of the other problems
you compain about...

However, let's rearrange the code so the definition of flour comes first,
then see what happens.

interesting. Now we see that "os" isn't defined due to a missing import.
Sorry, try again. See further notes below..
>
> Screen display :
>
> >>> 1
> >>> 2
> >>> 3
> >>> 4
> >>> 5

Now, this last line of output just doesn't appear. Please note that you are
setting data.flourUsage.date in the class definition, but setting
data.flourUsage.data in the main code.
>
> 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.

Perhaps if you were to present a correct example we could set you stright.
Suffice it, at this stage, to say that the class definitions are extracted
from the defining module by executing the class definition. Let's see what
happens when you show us the code you actually ran.

regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list