MCPcopy Create free account
hub / github.com/ActiveState/code / pickled

Function pickled

recipes/Python/511510_Pickle_databstractiusing/recipe-511510.py:5–20  ·  view source on GitHub ↗
(filename)

Source from the content-addressed store, hash-verified

3
4@contextmanager
5def pickled(filename):
6 if os.path.isfile(filename):
7 data = pickle.load(open(filename))
8 else:
9 data = {}
10
11 def getter(item, type):
12 if item in data:
13 return data[item]
14 else:
15 data[item] = type()
16 return data[item]
17
18 yield getter
19
20 pickle.dump(data, open(filename, "w"))
21
22# Here is an example usage:
23with pickled("foo.db") as p:

Callers 1

recipe-511510.pyFile · 0.85

Calls 4

isfileMethod · 0.80
openFunction · 0.50
loadMethod · 0.45
dumpMethod · 0.45

Tested by

no test coverage detected