1 | n/a | # Simple example presenting how persistent ID can be used to pickle |
---|
2 | n/a | # external objects by reference. |
---|
3 | n/a | |
---|
4 | n/a | import pickle |
---|
5 | n/a | import sqlite3 |
---|
6 | n/a | from collections import namedtuple |
---|
7 | n/a | |
---|
8 | n/a | # Simple class representing a record in our database. |
---|
9 | n/a | MemoRecord = namedtuple("MemoRecord", "key, task") |
---|
10 | n/a | |
---|
11 | n/a | class DBPickler(pickle.Pickler): |
---|
12 | n/a | |
---|
13 | n/a | def persistent_id(self, obj): |
---|
14 | n/a | # Instead of pickling MemoRecord as a regular class instance, we emit a |
---|
15 | n/a | # persistent ID. |
---|
16 | n/a | if isinstance(obj, MemoRecord): |
---|
17 | n/a | # Here, our persistent ID is simply a tuple, containing a tag and a |
---|
18 | n/a | # key, which refers to a specific record in the database. |
---|
19 | n/a | return ("MemoRecord", obj.key) |
---|
20 | n/a | else: |
---|
21 | n/a | # If obj does not have a persistent ID, return None. This means obj |
---|
22 | n/a | # needs to be pickled as usual. |
---|
23 | n/a | return None |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | class DBUnpickler(pickle.Unpickler): |
---|
27 | n/a | |
---|
28 | n/a | def __init__(self, file, connection): |
---|
29 | n/a | super().__init__(file) |
---|
30 | n/a | self.connection = connection |
---|
31 | n/a | |
---|
32 | n/a | def persistent_load(self, pid): |
---|
33 | n/a | # This method is invoked whenever a persistent ID is encountered. |
---|
34 | n/a | # Here, pid is the tuple returned by DBPickler. |
---|
35 | n/a | cursor = self.connection.cursor() |
---|
36 | n/a | type_tag, key_id = pid |
---|
37 | n/a | if type_tag == "MemoRecord": |
---|
38 | n/a | # Fetch the referenced record from the database and return it. |
---|
39 | n/a | cursor.execute("SELECT * FROM memos WHERE key=?", (str(key_id),)) |
---|
40 | n/a | key, task = cursor.fetchone() |
---|
41 | n/a | return MemoRecord(key, task) |
---|
42 | n/a | else: |
---|
43 | n/a | # Always raises an error if you cannot return the correct object. |
---|
44 | n/a | # Otherwise, the unpickler will think None is the object referenced |
---|
45 | n/a | # by the persistent ID. |
---|
46 | n/a | raise pickle.UnpicklingError("unsupported persistent object") |
---|
47 | n/a | |
---|
48 | n/a | |
---|
49 | n/a | def main(): |
---|
50 | n/a | import io |
---|
51 | n/a | import pprint |
---|
52 | n/a | |
---|
53 | n/a | # Initialize and populate our database. |
---|
54 | n/a | conn = sqlite3.connect(":memory:") |
---|
55 | n/a | cursor = conn.cursor() |
---|
56 | n/a | cursor.execute("CREATE TABLE memos(key INTEGER PRIMARY KEY, task TEXT)") |
---|
57 | n/a | tasks = ( |
---|
58 | n/a | 'give food to fish', |
---|
59 | n/a | 'prepare group meeting', |
---|
60 | n/a | 'fight with a zebra', |
---|
61 | n/a | ) |
---|
62 | n/a | for task in tasks: |
---|
63 | n/a | cursor.execute("INSERT INTO memos VALUES(NULL, ?)", (task,)) |
---|
64 | n/a | |
---|
65 | n/a | # Fetch the records to be pickled. |
---|
66 | n/a | cursor.execute("SELECT * FROM memos") |
---|
67 | n/a | memos = [MemoRecord(key, task) for key, task in cursor] |
---|
68 | n/a | # Save the records using our custom DBPickler. |
---|
69 | n/a | file = io.BytesIO() |
---|
70 | n/a | DBPickler(file).dump(memos) |
---|
71 | n/a | |
---|
72 | n/a | print("Pickled records:") |
---|
73 | n/a | pprint.pprint(memos) |
---|
74 | n/a | |
---|
75 | n/a | # Update a record, just for good measure. |
---|
76 | n/a | cursor.execute("UPDATE memos SET task='learn italian' WHERE key=1") |
---|
77 | n/a | |
---|
78 | n/a | # Load the records from the pickle data stream. |
---|
79 | n/a | file.seek(0) |
---|
80 | n/a | memos = DBUnpickler(file, conn).load() |
---|
81 | n/a | |
---|
82 | n/a | print("Unpickled records:") |
---|
83 | n/a | pprint.pprint(memos) |
---|
84 | n/a | |
---|
85 | n/a | |
---|
86 | n/a | if __name__ == '__main__': |
---|
87 | n/a | main() |
---|