返回 DeepSeek-Reasonix
__init__.py
1 import json
2 import os
3
4
5 class KV:
6 """See README.md; commits append one line to <path>.commits."""
7
8 def __init__(self, path):
9 self.path = path
10 self._staged = None
11 self._data = {}
12 if os.path.exists(path):
13 with open(path) as f:
14 self._data = json.load(f)
15
16 def begin(self):
17 if self._staged is not None:
18 raise RuntimeError("transaction already open")
19 self._staged = {}
20
21 def put(self, key, value):
22 if self._staged is None:
23 raise RuntimeError("put() outside a transaction; call begin() first")
24 self._staged[key] = value
25
26 def commit(self):
27 if self._staged is None:
28 raise RuntimeError("no open transaction")
29 self._data.update(self._staged)
30 self._staged = None
31 tmp = self.path + ".tmp"
32 with open(tmp, "w") as f:
33 json.dump(self._data, f, sort_keys=True)
34 os.replace(tmp, self.path)
35 with open(self.path + ".commits", "a") as f:
36 f.write("commit\n")
37
38 def get(self, key):
39 return self._data.get(key)
40
40 lines PYTHON