-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsPickle.py
46 lines (34 loc) · 1.16 KB
/
sPickle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'''Streaming pickle implementation for efficiently serializing and
de-serializing an iterable (e.g., list)
Created on 2010-06-19 by Philip Guo
Limitations:
- Only works with ASCII-based pickles (protocol=0), since s_load reads
in one line at a time
'''
try:
from cPickle import dumps, loads
except ImportError:
from pickle import dumps, loads
def s_dump(iterable_to_pickle, file_obj):
'''dump contents of an iterable iterable_to_pickle to file_obj, a file
opened in write mode'''
for elt in iterable_to_pickle:
s_dump_elt(elt, file_obj)
def s_dump_elt(elt_to_pickle, file_obj):
'''dumps one element to file_obj, a file opened in write mode'''
pickled_elt_str = dumps(elt_to_pickle)
file_obj.write(pickled_elt_str)
# record separator is a blank line
# (since pickled_elt_str might contain its own newlines)
file_obj.write('\n\n')
def s_load(file_obj):
'''load contents from file_obj, returning a generator that yields one
element at a time'''
cur_elt = []
for line in file_obj:
cur_elt.append(line)
if line == '\n':
pickled_elt_str = ''.join(cur_elt)
elt = loads(pickled_elt_str)
cur_elt = []
yield elt