From 3ae2729afd547a9e0eff38356aee34327d4e96e6 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Tue, 18 Aug 2009 00:43:49 -0400 Subject: [PATCH] finished #load and #dumps sections --- serializing.html | 69 ++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/serializing.html b/serializing.html index 2826a65..049fb77 100644 --- a/serializing.html +++ b/serializing.html @@ -116,39 +116,55 @@ NameError: name 'entry' is not defined 'published_date': time.struct_time(tm_year=2009, tm_mon=3, tm_mday=27, tm_hour=22, tm_min=20, tm_sec=42, tm_wday=4, tm_yday=86, tm_isdst=-1), 'published': True}
    -
  1. FIXME -
  2. FIXME -
  3. FIXME -
  4. FIXME -
  5. FIXME +
  6. This is Python Shell #2. +
  7. There is no entry variable defined here. You defined an entry variable in Python Shell #1, but that’s a completely different environment with its own state. +
  8. Open the entry.pickle file you created in Python Shell #1. The pickle module uses a binary data format, so you should always open pickle files in binary mode. +
  9. The pickle.load() function takes a stream object, reads the serialized data from the stream, creates a new Python object, recreates the serialized data in the new Python object, and returns the new Python object. +
  10. Now the entry variable is a dictionary with familiar-looking keys and values.
-

FIXME +

The pickle.dump() / pickle.load() cycle results in an identical copy of the original data structure. + +

+>>> shell                                    
+1
+>>> with open('entry.pickle', 'rb') as f:    
+...     entry2 = pickle.load(f)              
+... 
+>>> entry2 == entry                          
+True
+>>> entry2['tags']                           
+('diveintopython', 'docbook', 'html')
+>>> entry2['internal_id']
+b'\xde\xd5\xb4\xf8'
+
    +
  1. Switch back to Python Shell #1. +
  2. Open the entry.pickle file. +
  3. Load the serialized data into a new variable, entry2. +
  4. Python confirms that the two dictionaries, entry and entry2, are identical. In this shell, you built entry from the ground up, starting with an empty dictionary and manually assigning values to specific keys. You serialized this dictionary and stored it in the entry.pickle file. Now you’ve read the serialized data from that file and created a perfect replica of the original data structure. +
  5. For reasons that will become clear later in this chapter, I want to point out that the value of the 'tags' key is a tuple, and the value of the 'internal_id' key is a bytes object. +
+ +

Pickling Without a File

+ +

The examples in the previous section showed how to serialize a Python object directly to a file on disk. But what if you don’t want or need a file? You can also serialize to a bytes object in memory.

 >>> shell
 1
->>> with open('entry.pickle', 'rb') as f:    
-...     entry2 = pickle.load(f)              
-... 
->>> entry2 == entry                          
-True
->>> entry2['tags']                           
-('diveintopython', 'docbook', 'html')
->>> entry2['internal_id']                    
-b'\xde\xd5\xb4\xf8'
+>>> b = pickle.dumps(entry) +>>> type(b) +<class 'bytes'> +>>> entry3 = pickle.loads(b) +>>> entry3 == entry +True
    -
  1. FIXME -
  2. -
  3. -
  4. -
  5. +
  6. The pickle.dumps() function (note the 's' at the end of the function name) performs the same serialization as the pickle.dump() function. Instead of taking a stream object and writing the serialized data to a file on disk, it simply returns the serialized data. +
  7. Since the pickle protocol uses a binary data format, the pickle.dumps() function returns a bytes object. +
  8. The pickle.loads() function (again, note the 's' at the end of the function name) performs the same deserialization as the pickle.load() function. Instead of taking a stream object and reading the serialized data from a file, it takes a bytes object containing serialized data, such as the one returned by the pickle.dumps() function. +
  9. The end result is the same: a perfect replica of the original dictionary.
-

Saving to (and Loading from) an Object in Memory

- -

FIXME -

Bytes and Strings Rear Their Ugly Heads (Again!)

FIXME - discussion of pickle protocol versions, backward incompatibility of protocol version 3 due to bytes/strings separation in Python 3, link to http://docs.python.org/3.1/library/pickle.html#data-stream-format @@ -293,12 +309,12 @@ highest protocol among opcodes = 3

Serializing Datatypes Unsupported by JSON

->>> shell
+>>> shell                                                 
 1
 >>> entry
 FIXME
 >>> import json
->>> with open('entry.json', 'w', encoding='utf-8') as f:   
+>>> with open('entry.json', 'w', encoding='utf-8') as f:  
 ...     json.dump(entry, f)
 ... 
 Traceback (most recent call last):
@@ -316,6 +332,7 @@ highest protocol among opcodes = 3
TypeError: b'\xde\xd5\xb4\xf8' is not JSON serializable
  1. FIXME +
  2. FIXME

FIXME