Page 168 -
P. 168

in a pickle


           Pickle your data


           Python ships with a standard library called pickle, which can save and load
           almost any Python data object, including lists.

           Once you pickle your data to a file, it is persistent and ready to be read into
           another program at some later date/time:

            Your data as it appears
            in Python’s memory                             The pickle engine




                 ['Is this the right room for an                                                  Your
                 argument?', "No you haven't!",
                 'When?', "No you didn't!", "You                                                  pickled
                 didn't!", 'You did not!', 'Ah!                                                    data
                 (taking out his wallet and paying)                                        [‘Is this the right room
                                                                                           for an argument?’, “No
                                                                                           you haven’t!”, ‘When?’,
                                                                                          “No you didn’t!”, “You
                                                                                           didn’t!”, ‘You did not!’,
                 Just the five minutes.', 'You                                            ‘Ah! (taking out his wallet
                                                                                           and paying) Just the five
                                                                                           minutes.’, ‘You most
                                                                                           certainly did not!’, “Oh
                  most certainly did not!', "Oh                                           “No it isn’t!”, “It’s
                                                                                           no you didn’t!”, “Oh no
                                                                                           you didn’t!”, “Oh look,
                                                                                           this isn’t an argument!”,
                                                                                           just contradiction!”,
                 no you didn't!", "Oh no you                                              ‘It IS!’, ‘You just
                                                                                           contradicted me!’, ‘You
                                                                                           DID!’, ‘You did just
                                                                                           then!’, ‘(exasperated)
                                                                                           Oh, this is futile!!’,
                 didn't!", "Oh look, this isn't                                           ‘Yes it is!’]
                 an argument!", "No it isn't!",
                 "It's just contradiction!", 'It
                 IS!', 'You just contradicted
                  me!', 'You DID!', 'You did just                               Out comes the
                 then!', '(exasperated) Oh, this   Feed your Python             pickled version of
                 is futile!!', 'Yes it is!']
                                                 data to pickle.                your data.
          You can, for example, store your pickled data on disk, put it in a database,
           or transfer it over a network to another computer.
           When you are ready, reversing this process unpickles your persistent pickled
           data and recreates your data in its original form within Python’s memory:  Your data is recreated
                                                                                    in Python’s memory,
                                   The same pickle engine                           exactly as before.
           Your
           pickled                                                      ['Is this the right room for an
                                                                        argument?', "No you haven't!",
           data                                                        'When?', "No you didn't!", "You
                                                                        didn't!", 'You did not!', 'Ah!
                                                                        (taking out his wallet and paying)
                   [‘Is this the right room
                   for an argument?’, “No
                   you haven’t!”, ‘When?’,
                   “No you didn’t!”, “You
                   didn’t!”, ‘You did not!’,                            Just the five minutes.', 'You
                   ‘Ah! (taking out his wallet
                   and paying) Just the five
                    minutes.’, ‘You most
                   certainly did not!’, “Oh                             most certainly did not!', "Oh
                   no you didn’t!”, “Oh no
                   you didn’t!”, “Oh look,
                   this isn’t an argument!”,
                   “No it isn’t!”, “It’s
                   just contradiction!”,                                no you didn't!", "Oh no you
                   ‘It IS!’, ‘You just
                   contradicted me!’, ‘You
                    DID!’, ‘You did just
                   then!’, ‘(exasperated)
                   Oh, this is futile!!’,                               didn't!", "Oh look, this isn't
                   ‘Yes it is!’]
                                                                        an argument!", "No it isn't!",
                                                                        "It's just contradiction!", 'It
                       Feed your pickled             Out comes the      IS!', 'You just contradicted
                                                                        me!', 'You DID!', 'You did just
                       data to pickle.               Python version of   then!', '(exasperated) Oh, this
                                                     your pickled data.  is futile!!', 'Yes it is!']
           132    Chapter 4
   163   164   165   166   167   168   169   170   171   172   173