encommon 0.5.0__py3-none-any.whl → 0.6.0__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.
encommon/config/config.py CHANGED
@@ -155,9 +155,9 @@ class Config:
155
155
  self,
156
156
  ) -> Params:
157
157
  """
158
- Return the configuration in the object format for files.
158
+ Return the Pydantic model containing the configuration.
159
159
 
160
- :returns: Configuration in the object format for files.
160
+ :returns: Pydantic model containing the configuration.
161
161
  """
162
162
 
163
163
  if self.__params is not None:
encommon/conftest.py CHANGED
@@ -21,6 +21,7 @@ def config_path(
21
21
  Construct the directory and files needed for the tests.
22
22
 
23
23
  :param tmp_path: pytest object for temporal filesystem.
24
+ :returns: New resolved filesystem path object instance.
24
25
  """
25
26
 
26
27
 
@@ -40,4 +41,4 @@ def config_path(
40
41
  .write_text('name: Tony Stark'))
41
42
 
42
43
 
43
- return tmp_path
44
+ return tmp_path.resolve()
@@ -7,6 +7,7 @@ is permitted, for more information consult the project license file.
7
7
 
8
8
 
9
9
 
10
+ from pathlib import Path
10
11
  from time import sleep
11
12
 
12
13
  from pytest import raises
@@ -25,8 +26,10 @@ def test_Timers() -> None:
25
26
  attrs = list(timers.__dict__)
26
27
 
27
28
  assert attrs == [
28
- '_Timers__timing',
29
- '_Timers__caches']
29
+ '_Timers__timers',
30
+ '_Timers__cache_file',
31
+ '_Timers__cache_name',
32
+ '_Timers__cache_dict']
30
33
 
31
34
 
32
35
  assert repr(timers).startswith(
@@ -36,7 +39,11 @@ def test_Timers() -> None:
36
39
  '<encommon.times.timers.Timers')
37
40
 
38
41
 
39
- assert timers.timing == {'one': 1}
42
+ assert timers.timers == {'one': 1}
43
+ assert timers.cache_file is not None
44
+ assert len(timers.cache_dict) == 1
45
+ assert timers.cache_name is not None
46
+
40
47
 
41
48
  assert not timers.ready('one')
42
49
  sleep(1.1)
@@ -50,6 +57,42 @@ def test_Timers() -> None:
50
57
 
51
58
 
52
59
 
60
+ def test_Timers_cache(
61
+ tmp_path: Path,
62
+ ) -> None:
63
+ """
64
+ Perform various tests associated with relevant routines.
65
+
66
+ :param tmp_path: pytest object for temporal filesystem.
67
+ """
68
+
69
+ cache_file = (
70
+ f'{tmp_path}/timers.db')
71
+
72
+ timers1 = Timers(
73
+ timers={'one': 1},
74
+ cache_file=cache_file)
75
+
76
+ assert not timers1.ready('one')
77
+
78
+ sleep(0.75)
79
+
80
+ timers2 = Timers(
81
+ timers={'one': 1},
82
+ cache_file=cache_file)
83
+
84
+ assert not timers1.ready('one')
85
+ assert not timers2.ready('one')
86
+
87
+ sleep(0.25)
88
+
89
+ timers2.load_cache()
90
+
91
+ assert timers1.ready('one')
92
+ assert timers2.ready('one')
93
+
94
+
95
+
53
96
  def test_Timers_raises() -> None:
54
97
  """
55
98
  Perform various tests associated with relevant routines.
encommon/times/timers.py CHANGED
@@ -7,59 +7,155 @@ is permitted, for more information consult the project license file.
7
7
 
8
8
 
9
9
 
10
+ from sqlite3 import connect as SQLite
10
11
  from typing import Optional
12
+ from typing import TYPE_CHECKING
11
13
 
12
14
  from .common import NUMERIC
13
15
  from .common import PARSABLE
14
16
  from .times import Times
15
17
 
18
+ if TYPE_CHECKING:
19
+ from sqlite3 import Connection
20
+
21
+
22
+ TABLE = (
23
+ """
24
+ create table
25
+ if not exists
26
+ {0} (
27
+ "unique" text not null,
28
+ "update" text not null,
29
+ primary key ("unique"));
30
+ """) # noqa: LIT003
31
+
16
32
 
17
33
 
18
34
  class Timers:
19
35
  """
20
36
  Track timers on unique key and determine when to proceed.
21
37
 
38
+ .. warning::
39
+ This class will use an in-memory database for cache,
40
+ unless a cache file is explicity defined.
41
+
22
42
  .. testsetup::
23
43
  >>> from time import sleep
24
44
 
25
45
  Example
26
46
  -------
27
- >>> timing = {'test': 1}
28
- >>> timers = Timers(timing)
29
- >>> timers.ready('test')
47
+ >>> timers = Timers({'one': 1})
48
+ >>> timers.ready('one')
30
49
  False
31
50
  >>> sleep(1)
32
- >>> timers.ready('test')
51
+ >>> timers.ready('one')
33
52
  True
34
53
 
35
- :param timing: Seconds that are used for each of timers.
54
+ :param timers: Seconds that are used for each of timers.
55
+ :param cache_file: Optional path to SQLite database for
56
+ cache. This will allow for use between executions.
57
+ :param cache_name: Optional override default table name.
36
58
  """
37
59
 
38
- __timing: dict[str, float]
39
- __caches: dict[str, Times]
60
+ __timers: dict[str, float]
61
+ __cache_file: 'Connection'
62
+ __cache_name: str
63
+ __cache_dict: dict[str, Times]
40
64
 
41
65
 
42
66
  def __init__(
43
67
  self,
44
- timing: Optional[dict[str, NUMERIC]] = None,
68
+ timers: Optional[dict[str, NUMERIC]] = None,
69
+ cache_file: str = ':memory:',
70
+ cache_name: str = 'encommon_timers',
45
71
  ) -> None:
46
72
  """
47
73
  Initialize instance for class using provided parameters.
48
74
  """
49
75
 
50
- timing = timing or {}
76
+ timers = timers or {}
51
77
 
52
- self.__timing = {
78
+
79
+ self.__timers = {
53
80
  k: float(v)
54
- for k, v in timing.items()}
81
+ for k, v in
82
+ timers.items()}
83
+
84
+
85
+ cached = SQLite(cache_file)
86
+
87
+ cached.execute(
88
+ TABLE.format(cache_name))
55
89
 
56
- self.__caches = {
90
+ cached.commit()
91
+
92
+ self.__cache_file = cached
93
+ self.__cache_name = cache_name
94
+
95
+
96
+ self.__cache_dict = {
57
97
  x: Times()
58
- for x in self.__timing}
98
+ for x in
99
+ self.__timers}
100
+
101
+
102
+ self.load_cache()
103
+ self.save_cache()
104
+
105
+
106
+ def load_cache(
107
+ self,
108
+ ) -> None:
109
+ """
110
+ Load the timers cache from the database into attribute.
111
+ """
112
+
113
+ cached = self.__cache_file
114
+ table = self.__cache_name
115
+ cachem = self.__cache_dict
116
+
117
+ cursor = cached.execute(
118
+ f'select * from {table}'
119
+ ' order by "unique" asc')
120
+
121
+ records = cursor.fetchall()
122
+
123
+ for record in records:
124
+
125
+ unique = record[0]
126
+ update = record[1]
127
+
128
+ times = Times(update)
129
+
130
+ cachem[unique] = times
131
+
132
+
133
+ def save_cache(
134
+ self,
135
+ ) -> None:
136
+ """
137
+ Save the timers cache from the attribute into database.
138
+ """
139
+
140
+ cached = self.__cache_file
141
+ table = self.__cache_name
142
+ cachem = self.__cache_dict
143
+
144
+ insert = [
145
+ (k, str(v)) for k, v
146
+ in cachem.items()]
147
+
148
+ cached.executemany(
149
+ (f'replace into {table}'
150
+ ' ("unique", "update")'
151
+ ' values (?, ?)'),
152
+ tuple(insert))
153
+
154
+ cached.commit()
59
155
 
60
156
 
61
157
  @property
62
- def timing(
158
+ def timers(
63
159
  self,
64
160
  ) -> dict[str, float]:
65
161
  """
@@ -68,7 +164,46 @@ class Timers:
68
164
  :returns: Property for attribute from the class instance.
69
165
  """
70
166
 
71
- return dict(self.__timing)
167
+ return dict(self.__timers)
168
+
169
+
170
+ @property
171
+ def cache_file(
172
+ self,
173
+ ) -> 'Connection':
174
+ """
175
+ Return the property for attribute from the class instance.
176
+
177
+ :returns: Property for attribute from the class instance.
178
+ """
179
+
180
+ return self.__cache_file
181
+
182
+
183
+ @property
184
+ def cache_dict(
185
+ self,
186
+ ) -> dict[str, Times]:
187
+ """
188
+ Return the property for attribute from the class instance.
189
+
190
+ :returns: Property for attribute from the class instance.
191
+ """
192
+
193
+ return dict(self.__cache_dict)
194
+
195
+
196
+ @property
197
+ def cache_name(
198
+ self,
199
+ ) -> str:
200
+ """
201
+ Return the property for attribute from the class instance.
202
+
203
+ :returns: Property for attribute from the class instance.
204
+ """
205
+
206
+ return self.__cache_name
72
207
 
73
208
 
74
209
  def ready(
@@ -79,20 +214,24 @@ class Timers:
79
214
  """
80
215
  Determine whether or not the appropriate time has passed.
81
216
 
217
+ .. note::
218
+ For performance reasons, this method will not notice
219
+ changes within the database unless refreshed first.
220
+
82
221
  :param unique: Unique identifier for the timer in mapping.
83
222
  :param update: Determines whether or not time is updated.
84
223
  """
85
224
 
86
- timer = self.__timing
87
- cache = self.__caches
225
+ timers = self.__timers
226
+ caches = self.__cache_dict
88
227
 
89
- if unique not in cache:
228
+ if unique not in caches:
90
229
  raise ValueError('unique')
91
230
 
92
- _cache = cache[unique]
93
- _timer = timer[unique]
231
+ cache = caches[unique]
232
+ timer = timers[unique]
94
233
 
95
- ready = _cache.elapsed >= _timer
234
+ ready = cache.elapsed >= timer
96
235
 
97
236
  if ready and update:
98
237
  self.update(unique)
@@ -112,12 +251,14 @@ class Timers:
112
251
  :param started: Override the start time for timer value.
113
252
  """
114
253
 
115
- cache = self.__caches
254
+ caches = self.__cache_dict
116
255
 
117
- if unique not in cache:
256
+ if unique not in caches:
118
257
  raise ValueError('unique')
119
258
 
120
- cache[unique] = Times(started)
259
+ caches[unique] = Times(started)
260
+
261
+ self.save_cache()
121
262
 
122
263
 
123
264
  def create(
@@ -134,11 +275,13 @@ class Timers:
134
275
  :param started: Determines when the time starts for timer.
135
276
  """
136
277
 
137
- timer = self.__timing
138
- cache = self.__caches
278
+ timers = self.__timers
279
+ caches = self.__cache_dict
139
280
 
140
- if unique in cache:
281
+ if unique in caches:
141
282
  raise ValueError('unique')
142
283
 
143
- timer[unique] = float(minimum)
144
- cache[unique] = Times(started)
284
+ timers[unique] = float(minimum)
285
+ caches[unique] = Times(started)
286
+
287
+ self.save_cache()
encommon/version.txt CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: encommon
3
- Version: 0.5.0
3
+ Version: 0.6.0
4
4
  Summary: Enasis Network Common Library
5
5
  License: MIT
6
6
  Classifier: Programming Language :: Python :: 3
@@ -1,10 +1,10 @@
1
1
  encommon/__init__.py,sha256=VoXUcphq-gcXCraaU47EtXBftF6UVuQPMGr0fuCTt9A,525
2
- encommon/conftest.py,sha256=TNLox448WJSUkG5Lx2PM7LWvLHiaaOvxm6tx6o02pBc,810
2
+ encommon/conftest.py,sha256=z5BMi6KjNuactDRgbh8J8w81V08Ptf6QK2o_bJANRs4,880
3
3
  encommon/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- encommon/version.txt,sha256=oK1QZAE5pST4ZZEVcUW_HUZ06pwGW_6iFVjw97BEMMg,6
4
+ encommon/version.txt,sha256=l6XW5UCmEg0Jw53bZn4Ojiusf8wv_vgTuC4I_WA2W84,6
5
5
  encommon/config/__init__.py,sha256=2ic7tK2lOQvqWmmQnMozqxCJcbyQ_sSEHmOUDUFan2U,710
6
6
  encommon/config/common.py,sha256=gaKBgkF7b7UIAhd0ESio0KpG8JfdRpz5BxNgKRptd6A,2041
7
- encommon/config/config.py,sha256=pvvz5lzqaXJV-9-8v8XPXVP8vpHiZ9lzGLx05pjW5gk,4855
7
+ encommon/config/config.py,sha256=-wIkyA6YQGAly0WuXLaOYXENjZYRYgWk_VdUN8mMe8Q,4853
8
8
  encommon/config/files.py,sha256=2DHz7GKOjF8FC26tY0V-E9svNMrlkSUshb2gfxqGgSo,2313
9
9
  encommon/config/logger.py,sha256=2y-VFzgM2pZzEYq48XfvrDPrtUcxyP9QjbH5OG7fv5o,13449
10
10
  encommon/config/params.py,sha256=IWQ-UzR8x6VxkcUddC3qhI612IFMfLW44jEjRePXK0c,1825
@@ -26,14 +26,14 @@ encommon/times/__init__.py,sha256=w6LeTybgdsi0oM1EL9ARbAqevX7Tn46JYhbXNAaWzB0,59
26
26
  encommon/times/common.py,sha256=xLB_GLahYKT-YrVq90RUzmiodJQ4__1YihgZZTnadgg,2506
27
27
  encommon/times/duration.py,sha256=8p-L9OItO80nnd35FYrAXbiUS8RJzavI21lXBrtLaH0,9872
28
28
  encommon/times/parse.py,sha256=ZzranxftlB6bZJ1ivkoUxD53TUfXDDUdpq9jsSJN-88,6411
29
- encommon/times/timers.py,sha256=4p2zJIieZZ8pMaKAv5CQdxUOPjVDd2f9QCpARB8gGnQ,3212
29
+ encommon/times/timers.py,sha256=yOVr6deGE5iatNWUwNQCzaXjMlLeYlolx4MmKT0MSHc,6312
30
30
  encommon/times/times.py,sha256=Dfu6ztUsV4X4vHzOC7ds8jtYy0AVj4_R5xB6H-yzemE,9432
31
31
  encommon/times/window.py,sha256=Fx_OJfU9B0IctCyaJ5XH_HxzhGlzeRATwuhI-HlsTPA,8023
32
32
  encommon/times/test/__init__.py,sha256=PjrnBYT0efyvbaGeNx94dm3tP3EVHUHSVs-VGeLEv5g,218
33
33
  encommon/times/test/test_common.py,sha256=nizxgB1scLDEm2o66waK1Ar_Dl5BBMTC-GRV7iDk0_U,1477
34
34
  encommon/times/test/test_duration.py,sha256=rmAhW2otHzQDHF0mBSETHnnL2OSbeAFYEHpDlaUSRnI,3901
35
35
  encommon/times/test/test_parse.py,sha256=TuZG_UbIEez4Ww35vdKIie_yaKp-PiUhsNmr1ZUoGvo,4308
36
- encommon/times/test/test_timers.py,sha256=F8asiUbMcIXkSstrOhQ9wrmdM2oTFAAM4sL50eQB4IA,1456
36
+ encommon/times/test/test_timers.py,sha256=z-ONyKtkAZ1A7w8qgpgx07r-Vsl5jY-pe2oneSF7hyo,2314
37
37
  encommon/times/test/test_times.py,sha256=DJBmRHCPkMLL9-bmc2DMhhf8zcr2rsTVAAQPavD_UmE,1986
38
38
  encommon/times/test/test_window.py,sha256=EWjXR8XREYwKrH-221CBAUwLF9GpvHtuF_CSMdQtUXI,4735
39
39
  encommon/types/__init__.py,sha256=l2WKtDs6uC9I3dIGnrzJvz-4JLEETUJOfT509XyMQWs,420
@@ -55,8 +55,8 @@ encommon/utils/test/test_match.py,sha256=QagKpTFdRo23-Y55fSaJrSMpt5jIebScKbz0h8t
55
55
  encommon/utils/test/test_paths.py,sha256=0ls9gWJ2B487Dr1fHDDFCZPA7gxtv56nFEYHrTkNX-U,1892
56
56
  encommon/utils/test/test_sample.py,sha256=iNV9IxXmA5KJat3jKRiZH3iutHrT6bsibwti60AhICk,1464
57
57
  encommon/utils/test/test_stdout.py,sha256=a060uA8oEHFGoVqdwB5iYxdl4R2TRSgr6Z7pMEbxibs,1675
58
- encommon-0.5.0.dist-info/LICENSE,sha256=otnXKCtMjPlbHs0wgZ_BWULrp3g_2dWQJ6icRk9nkgg,1071
59
- encommon-0.5.0.dist-info/METADATA,sha256=NJL3BwQ7cUm7Azf7NS1-ttm5v0ST4LW6G9tYD0NlIXA,2671
60
- encommon-0.5.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
61
- encommon-0.5.0.dist-info/top_level.txt,sha256=bP8q7-5tLDNm-3XPlqn_bDENfYNug5801H_xfz3BEAM,9
62
- encommon-0.5.0.dist-info/RECORD,,
58
+ encommon-0.6.0.dist-info/LICENSE,sha256=otnXKCtMjPlbHs0wgZ_BWULrp3g_2dWQJ6icRk9nkgg,1071
59
+ encommon-0.6.0.dist-info/METADATA,sha256=OJ2Y0-tAC9HmFKQmrcyR5oQHFAVU1rxD9Rt2ssceXAs,2671
60
+ encommon-0.6.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
61
+ encommon-0.6.0.dist-info/top_level.txt,sha256=bP8q7-5tLDNm-3XPlqn_bDENfYNug5801H_xfz3BEAM,9
62
+ encommon-0.6.0.dist-info/RECORD,,