-
Notifications
You must be signed in to change notification settings - Fork 47
/
common_abc.py
42 lines (35 loc) · 1.09 KB
/
common_abc.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
"""
Common abstract base classes (or mixins..if we get krazy) that will be shared
across modules.
"""
import cPickle
from common import smart_open
class SaveLoad(object):
"""
Objects which inherit from this class have load/ methods, which [un]pickle
them to disk.
Uses cPickle, so objects cannot have an attribute set to either a lambda
function, or any function that is not defined until some class is
initialized (classmethod).
"""
def save(self, savefile, protocol=-1):
"""
Pickle self to outfile.
Parameters
----------
savefile : filepath or buffer
protocol : 0, 1, 2, -1
0 < 1 < 2 in terms of performance. -1 means use highest available.
"""
with smart_open(savefile, 'w') as f:
cPickle.dump(self, f, protocol=protocol)
@classmethod
def load(cls, loadfile):
"""
Pickle SFileFilter from disk.
Parameters
----------
loadfile : filepath or buffer
"""
with smart_open(loadfile, 'rb') as f:
return cPickle.load(f)