vnai 2.0.2__py3-none-any.whl → 2.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
vnai/flow/queue.py CHANGED
@@ -1,58 +1,145 @@
1
- _C='category'
2
- _B=True
3
- _A=None
4
- import time,threading,json
1
+ ##
2
+
3
+ ##
4
+
5
+
6
+ import time
7
+ import threading
8
+ import json
5
9
  from datetime import datetime
6
10
  from pathlib import Path
11
+
7
12
  class Buffer:
8
- _instance=_A;_lock=threading.Lock()
9
- def __new__(A):
10
- with A._lock:
11
- if A._instance is _A:A._instance=super(Buffer,A).__new__(A);A._instance._initialize()
12
- return A._instance
13
- def _initialize(A):A.data=[];A.lock=threading.Lock();A.max_size=1000;A.backup_interval=300;A.home_dir=Path.home();A.project_dir=A.home_dir/'.vnstock';A.project_dir.mkdir(exist_ok=_B);A.data_dir=A.project_dir/'data';A.data_dir.mkdir(exist_ok=_B);A.backup_path=A.data_dir/'buffer_backup.json';A._load_from_backup();A._start_backup_thread()
14
- def _load_from_backup(A):
15
- if A.backup_path.exists():
16
- try:
17
- with open(A.backup_path,'r')as B:C=json.load(B)
18
- with A.lock:A.data=C
19
- except:pass
20
- def _save_to_backup(A):
21
- with A.lock:
22
- if not A.data:return
23
- try:
24
- with open(A.backup_path,'w')as B:json.dump(A.data,B)
25
- except:pass
26
- def _start_backup_thread(A):
27
- def B():
28
- while _B:time.sleep(A.backup_interval);A._save_to_backup()
29
- C=threading.Thread(target=B,daemon=_B);C.start()
30
- def add(A,item,category=_A):
31
- D='timestamp';C=category;B=item
32
- with A.lock:
33
- if isinstance(B,dict):
34
- if D not in B:B[D]=datetime.now().isoformat()
35
- if C:B[_C]=C
36
- A.data.append(B)
37
- if len(A.data)>A.max_size:A.data=A.data[-A.max_size:]
38
- if len(A.data)%100==0:A._save_to_backup()
39
- return len(A.data)
40
- def get(A,count=_A,category=_A):
41
- D=category;C=count
42
- with A.lock:
43
- if D:B=[A for A in A.data if A.get(_C)==D]
44
- else:B=A.data.copy()
45
- if C:return B[:C]
46
- else:return B
47
- def clear(A,category=_A):
48
- B=category
49
- with A.lock:
50
- if B:A.data=[A for A in A.data if A.get(_C)!=B]
51
- else:A.data=[]
52
- A._save_to_backup();return len(A.data)
53
- def size(A,category=_A):
54
- B=category
55
- with A.lock:
56
- if B:return len([A for A in A.data if A.get(_C)==B])
57
- else:return len(A.data)
58
- buffer=Buffer()
13
+ #--
14
+
15
+ _instance = None
16
+ _lock = threading.Lock()
17
+
18
+ def __new__(cls):
19
+ with cls._lock:
20
+ if cls._instance is None:
21
+ cls._instance = super(Buffer, cls).__new__(cls)
22
+ cls._instance._initialize()
23
+ return cls._instance
24
+
25
+ def _initialize(self):
26
+ #--
27
+ self.data = []
28
+ self.lock = threading.Lock()
29
+ self.max_size = 1000
30
+ self.backup_interval = 300 ##
31
+
32
+
33
+ ##
34
+
35
+ self.home_dir = Path.home()
36
+ self.project_dir = self.home_dir / ".vnstock"
37
+ self.project_dir.mkdir(exist_ok=True)
38
+ self.data_dir = self.project_dir / 'data'
39
+ self.data_dir.mkdir(exist_ok=True)
40
+ self.backup_path = self.data_dir / "buffer_backup.json"
41
+
42
+ ##
43
+
44
+ self._load_from_backup()
45
+
46
+ ##
47
+
48
+ self._start_backup_thread()
49
+
50
+ def _load_from_backup(self):
51
+ #--
52
+ if self.backup_path.exists():
53
+ try:
54
+ with open(self.backup_path, 'r') as f:
55
+ backup_data = json.load(f)
56
+
57
+ with self.lock:
58
+ self.data = backup_data
59
+ except:
60
+ pass
61
+
62
+ def _save_to_backup(self):
63
+ #--
64
+ with self.lock:
65
+ if not self.data:
66
+ return
67
+
68
+ try:
69
+ with open(self.backup_path, 'w') as f:
70
+ json.dump(self.data, f)
71
+ except:
72
+ pass
73
+
74
+ def _start_backup_thread(self):
75
+ #--
76
+ def backup_task():
77
+ while True:
78
+ time.sleep(self.backup_interval)
79
+ self._save_to_backup()
80
+
81
+ backup_thread = threading.Thread(target=backup_task, daemon=True)
82
+ backup_thread.start()
83
+
84
+ def add(self, item, category=None):
85
+ #--
86
+ with self.lock:
87
+ ##
88
+
89
+ if isinstance(item, dict):
90
+ if "timestamp" not in item:
91
+ item["timestamp"] = datetime.now().isoformat()
92
+ if category:
93
+ item["category"] = category
94
+
95
+ ##
96
+
97
+ self.data.append(item)
98
+
99
+ ##
100
+
101
+ if len(self.data) > self.max_size:
102
+ self.data = self.data[-self.max_size:]
103
+
104
+ ##
105
+
106
+ if len(self.data) % 100 == 0:
107
+ self._save_to_backup()
108
+
109
+ return len(self.data)
110
+
111
+ def get(self, count=None, category=None):
112
+ #--
113
+ with self.lock:
114
+ if category:
115
+ filtered_data = [item for item in self.data if item.get("category") == category]
116
+ else:
117
+ filtered_data = self.data.copy()
118
+
119
+ if count:
120
+ return filtered_data[:count]
121
+ else:
122
+ return filtered_data
123
+
124
+ def clear(self, category=None):
125
+ #--
126
+ with self.lock:
127
+ if category:
128
+ self.data = [item for item in self.data if item.get("category") != category]
129
+ else:
130
+ self.data = []
131
+
132
+ self._save_to_backup()
133
+ return len(self.data)
134
+
135
+ def size(self, category=None):
136
+ #--
137
+ with self.lock:
138
+ if category:
139
+ return len([item for item in self.data if item.get("category") == category])
140
+ else:
141
+ return len(self.data)
142
+
143
+ ##
144
+
145
+ buffer = Buffer()