[Tutor] Re: newbie intro to pickle

Lee Harr missive at hotmail.com
Sat Apr 16 16:57:44 CEST 2005


>Pickling simply converts an object to a string (unpickling does the 
>opposite,
>convert a string to an object). The dump() function writes the generated 
>string
>directly to a file, but you can use the dumps() function to get the 
>generated
>string as such and do something else with it (e.g. put it in a database, 
>send it
>by mail, whatever).
>
>>>>mylist = [1, 2, 34, 4.2, 'abc']
>>>>s = pickle.dumps(mylist)
>>>>s # display the string
>"(lp0\nI1\naI2\naI34\naF4.2000000000000002\naS'abc'\np1\na."
>>>>pickle.loads(s)
>[1, 2, 34, 4.2000000000000002, 'abc']
>


... and just to complete the connection of dumps to dump,
you could write the string out to a file yourself ...

>>>import pickle
>>>mylist = [1, 2, 34, 4.2, 'abc']
>>>s = pickle.dumps(mylist)
>>>file_object = file('somefile.pickle', 'w') # I don't think there is a 
>>>"standard" suffix
>>>file_object.write(s)
>>>file_object.close()
>>>_file_object = file('somefile.pickle')
>>>_s = file_object.read()
>>>_mylist = pickle.loads(still_s)
>>>_mylist
[1, 2, 34, 4.2000000000000002, 'abc']


Now my question is how do you keep people from just loading
the high score file with whatever scores they want?

This is not to disparage a simple (and probably very useful)
high score file. It is just something that I have been thinking
about doing myself for quite a while, but I can never quite get
a grasp on how I would go about it.

Any thoughts?

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/



More information about the Tutor mailing list