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}
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.
+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.
+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'+
entry.pickle file.
+entry.pickle file. Now you’ve read the serialized data from that file and created a perfect replica of the original data structure.
+'tags' key is a tuple, and the value of the 'internal_id' key is a bytes object.
+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
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.
+pickle.dumps() function returns a bytes object.
+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.
+FIXME -
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
->>> 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 = 3TypeError: b'\xde\xd5\xb4\xf8' is not JSON serializable
FIXME