-
Notifications
You must be signed in to change notification settings - Fork 1
/
zint.py
129 lines (106 loc) · 3.33 KB
/
zint.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
import os
import time
import sys
import atexit
import msgpack
from io import BytesIO
from tempfile import gettempdir
"""
Usage:
myint = Zint("mydict_unique_name", restart_time=60).load()
restart_time : If the app is not restarted before this time, the store
of the dict variable will be deleted.
If not specified, restart_time default value is 3600s
"""
__all__ = ['Zint']
def _save_var(self):
if not self._active:
return
self._zintfp = open(self._zintfile, "wb")
self._zintfp.write(msgpack.packb(self._obj, use_bin_type=True))
self._zintfp.flush()
self._zintfp.close()
# Zdb-backed int class
class Zint():
def __init__(self, objname, default=0, restart_time=60, namespace=''):
if not isinstance(default, int):
raise ValueError("Default value is must be int, "
f"{type(default)} given")
self._obj = default
self._objname = objname
self._active = 1
self._restart_time = restart_time
self._zintfile = gettempdir() + '/' + namespace + '_' + objname
try:
tm = os.stat(self._zintfile).st_atime
now = time.time()
if now - tm > restart_time:
# zint file is more than 1hr old...dump it
raise ValueError("Old zint file")
self._zintfp = open(self._zintfile, "rb")
except Exception:
self._zintfp = open(self._zintfile, "wb+")
if not self._zintfp:
raise ValueError("Unable to create backup file")
self._zintfp.close()
atexit.register(_save_var, self)
def load(self):
self._zintfp = open(self._zintfile, "rb")
myio = BytesIO(self._zintfp.read())
it = msgpack.Unpacker(myio, raw=False)
self._zintfp.close()
if not it:
return self
for obj in it:
self._obj = obj
return self
def __disable(self):
self._active = 0
self._zintfp.close()
def __set(self, val):
self._obj = val
def __str__(self):
return str(self._obj)
def __add__(self, b):
self._obj = self._obj + b
return self
def __iadd__(self, b):
self._obj = self._obj + b
return self
def __sub__(self, b):
self._obj = self._obj - b
return self
def __isub__(self, b):
self._obj = self._obj - b
return self
def __mul__(self, b):
self._obj = self._obj * b
return self
def __imul__(self, b):
self._obj = self._obj * b
return self
def __div__(self, b):
self._obj = self._obj / b
return self
def __idiv__(self, b):
self._obj = self._obj / b
return self
def __floordiv__(self, b):
self._obj = self._obj // b
return self
def __ifloordiv__(self, b):
self._obj = self._obj // b
return self
def __mod__(self, b):
self._obj = self._obj % b
return self
#self.__disable()
#n = Zint(self._objname, restart_time=self._restart_time).load()
#n.__set(self._obj % b)
#return n
def __imod__(self, b):
self._obj = self._obj % b
return self
def __repr__(self):
return f"Zint[intvalue: {self._obj}, file:{self._zintfile}]"