encommon 0.4.1__tar.gz → 0.6.0__tar.gz

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.
Files changed (71) hide show
  1. {encommon-0.4.1 → encommon-0.6.0}/LICENSE +1 -1
  2. {encommon-0.4.1/encommon.egg-info → encommon-0.6.0}/PKG-INFO +7 -6
  3. {encommon-0.4.1 → encommon-0.6.0}/README.md +6 -5
  4. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/config.py +2 -2
  5. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/params.py +10 -0
  6. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/test/test_config.py +21 -1
  7. {encommon-0.4.1/encommon/config/test → encommon-0.6.0/encommon}/conftest.py +2 -1
  8. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/common.py +1 -1
  9. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/test/test_timers.py +46 -3
  10. encommon-0.6.0/encommon/times/timers.py +287 -0
  11. {encommon-0.4.1 → encommon-0.6.0}/encommon/types/__init__.py +5 -1
  12. {encommon-0.4.1 → encommon-0.6.0}/encommon/types/dicts.py +26 -0
  13. encommon-0.6.0/encommon/types/strings.py +25 -0
  14. {encommon-0.4.1 → encommon-0.6.0}/encommon/types/test/test_dicts.py +33 -13
  15. encommon-0.6.0/encommon/types/test/test_strings.py +19 -0
  16. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/sample.py +1 -1
  17. encommon-0.6.0/encommon/version.txt +1 -0
  18. {encommon-0.4.1 → encommon-0.6.0/encommon.egg-info}/PKG-INFO +7 -6
  19. {encommon-0.4.1 → encommon-0.6.0}/encommon.egg-info/SOURCES.txt +3 -1
  20. {encommon-0.4.1 → encommon-0.6.0}/setup.cfg +1 -0
  21. encommon-0.4.1/encommon/times/timers.py +0 -144
  22. encommon-0.4.1/encommon/version.txt +0 -1
  23. {encommon-0.4.1 → encommon-0.6.0}/MANIFEST.in +0 -0
  24. {encommon-0.4.1 → encommon-0.6.0}/encommon/__init__.py +0 -0
  25. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/__init__.py +0 -0
  26. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/common.py +0 -0
  27. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/files.py +0 -0
  28. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/logger.py +0 -0
  29. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/paths.py +0 -0
  30. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/test/__init__.py +0 -0
  31. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/test/test_common.py +0 -0
  32. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/test/test_files.py +0 -0
  33. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/test/test_logger.py +0 -0
  34. {encommon-0.4.1 → encommon-0.6.0}/encommon/config/test/test_paths.py +0 -0
  35. {encommon-0.4.1 → encommon-0.6.0}/encommon/crypts/__init__.py +0 -0
  36. {encommon-0.4.1 → encommon-0.6.0}/encommon/crypts/crypts.py +0 -0
  37. {encommon-0.4.1 → encommon-0.6.0}/encommon/crypts/hashes.py +0 -0
  38. {encommon-0.4.1 → encommon-0.6.0}/encommon/crypts/params.py +0 -0
  39. {encommon-0.4.1 → encommon-0.6.0}/encommon/crypts/test/__init__.py +0 -0
  40. {encommon-0.4.1 → encommon-0.6.0}/encommon/crypts/test/test_crypts.py +0 -0
  41. {encommon-0.4.1 → encommon-0.6.0}/encommon/crypts/test/test_hashes.py +0 -0
  42. {encommon-0.4.1 → encommon-0.6.0}/encommon/py.typed +0 -0
  43. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/__init__.py +0 -0
  44. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/duration.py +0 -0
  45. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/parse.py +0 -0
  46. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/test/__init__.py +0 -0
  47. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/test/test_common.py +0 -0
  48. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/test/test_duration.py +0 -0
  49. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/test/test_parse.py +0 -0
  50. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/test/test_times.py +0 -0
  51. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/test/test_window.py +0 -0
  52. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/times.py +0 -0
  53. {encommon-0.4.1 → encommon-0.6.0}/encommon/times/window.py +0 -0
  54. {encommon-0.4.1 → encommon-0.6.0}/encommon/types/empty.py +0 -0
  55. {encommon-0.4.1 → encommon-0.6.0}/encommon/types/test/__init__.py +0 -0
  56. {encommon-0.4.1 → encommon-0.6.0}/encommon/types/test/test_empty.py +0 -0
  57. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/__init__.py +0 -0
  58. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/common.py +0 -0
  59. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/match.py +0 -0
  60. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/paths.py +0 -0
  61. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/stdout.py +0 -0
  62. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/test/__init__.py +0 -0
  63. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/test/test_match.py +0 -0
  64. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/test/test_paths.py +0 -0
  65. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/test/test_sample.py +0 -0
  66. {encommon-0.4.1 → encommon-0.6.0}/encommon/utils/test/test_stdout.py +0 -0
  67. {encommon-0.4.1 → encommon-0.6.0}/encommon.egg-info/dependency_links.txt +0 -0
  68. {encommon-0.4.1 → encommon-0.6.0}/encommon.egg-info/requires.txt +0 -0
  69. {encommon-0.4.1 → encommon-0.6.0}/encommon.egg-info/top_level.txt +0 -0
  70. {encommon-0.4.1 → encommon-0.6.0}/pyproject.toml +0 -0
  71. {encommon-0.4.1 → encommon-0.6.0}/reqs-install.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Enasis Network
3
+ Copyright (c) 2024 Enasis Network
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: encommon
3
- Version: 0.4.1
3
+ Version: 0.6.0
4
4
  Summary: Enasis Network Common Library
5
5
  License: MIT
6
6
  Classifier: Programming Language :: Python :: 3
@@ -18,14 +18,15 @@ Requires-Dist: snaptime
18
18
 
19
19
  # Enasis Network Common Library
20
20
 
21
- [![](https://img.shields.io/github/actions/workflow/status/enasisnetwork/encommon/build.yml?style=flat-square&label=GitHub%20actions)](https://github.com/enasisnetwork/encommon/actions)
22
- [![](https://img.shields.io/readthedocs/encommon?style=flat-square&label=Read%20the%20Docs)](https://encommon.readthedocs.io/en/stable)
23
- [![](https://img.shields.io/pypi/v/encommon.svg?style=flat-square&label=PyPi%20version)](https://pypi.org/project/encommon)
24
- [![](https://img.shields.io/pypi/dm/encommon?style=flat-square&label=PyPi%20downloads)](https://pypi.org/project/encommon)
25
-
26
21
  Common classes and functions used in various public and private projects for
27
22
  [Enasis Network](https://github.com/enasisnetwork).
28
23
 
24
+ [![](https://img.shields.io/github/actions/workflow/status/enasisnetwork/encommon/build.yml?style=flat-square&label=GitHub%20actions)](https://github.com/enasisnetwork/encommon/actions)<br>
25
+ [![codecov](https://codecov.io/gh/enasisnetwork/encommon/graph/badge.svg?token=7PGOXKJU0E)](https://codecov.io/gh/enasisnetwork/encommon)<br>
26
+ [![](https://img.shields.io/readthedocs/encommon?style=flat-square&label=Read%20the%20Docs)](https://encommon.readthedocs.io/en/stable)<br>
27
+ [![](https://img.shields.io/pypi/v/encommon.svg?style=flat-square&label=PyPi%20version)](https://pypi.org/project/encommon)<br>
28
+ [![](https://img.shields.io/pypi/dm/encommon?style=flat-square&label=PyPi%20downloads)](https://pypi.org/project/encommon)
29
+
29
30
  ## Installing the package
30
31
  Installing stable from the PyPi repository
31
32
  ```
@@ -1,13 +1,14 @@
1
1
  # Enasis Network Common Library
2
2
 
3
- [![](https://img.shields.io/github/actions/workflow/status/enasisnetwork/encommon/build.yml?style=flat-square&label=GitHub%20actions)](https://github.com/enasisnetwork/encommon/actions)
4
- [![](https://img.shields.io/readthedocs/encommon?style=flat-square&label=Read%20the%20Docs)](https://encommon.readthedocs.io/en/stable)
5
- [![](https://img.shields.io/pypi/v/encommon.svg?style=flat-square&label=PyPi%20version)](https://pypi.org/project/encommon)
6
- [![](https://img.shields.io/pypi/dm/encommon?style=flat-square&label=PyPi%20downloads)](https://pypi.org/project/encommon)
7
-
8
3
  Common classes and functions used in various public and private projects for
9
4
  [Enasis Network](https://github.com/enasisnetwork).
10
5
 
6
+ [![](https://img.shields.io/github/actions/workflow/status/enasisnetwork/encommon/build.yml?style=flat-square&label=GitHub%20actions)](https://github.com/enasisnetwork/encommon/actions)<br>
7
+ [![codecov](https://codecov.io/gh/enasisnetwork/encommon/graph/badge.svg?token=7PGOXKJU0E)](https://codecov.io/gh/enasisnetwork/encommon)<br>
8
+ [![](https://img.shields.io/readthedocs/encommon?style=flat-square&label=Read%20the%20Docs)](https://encommon.readthedocs.io/en/stable)<br>
9
+ [![](https://img.shields.io/pypi/v/encommon.svg?style=flat-square&label=PyPi%20version)](https://pypi.org/project/encommon)<br>
10
+ [![](https://img.shields.io/pypi/dm/encommon?style=flat-square&label=PyPi%20downloads)](https://pypi.org/project/encommon)
11
+
11
12
  ## Installing the package
12
13
  Installing stable from the PyPi repository
13
14
  ```
@@ -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:
@@ -20,7 +20,9 @@ class ConfigParams(BaseModel, extra='forbid'):
20
20
  """
21
21
  Process and validate the common configuration parameters.
22
22
 
23
+ :param paths: Complete or relative path to config paths.
23
24
  :param data: Keyword arguments passed to Pydantic model.
25
+ This parameter is picked up by autodoc, please ignore.
24
26
  """
25
27
 
26
28
  paths: Optional[list[str]] = None
@@ -31,7 +33,11 @@ class LoggerParams(BaseModel, extra='forbid'):
31
33
  """
32
34
  Process and validate the common configuration parameters.
33
35
 
36
+ :param stdo_level: Minimum log message severity level.
37
+ :param file_level: Minimum log message severity level.
38
+ :param file_path: Enables writing to the filesystem path.
34
39
  :param data: Keyword arguments passed to Pydantic model.
40
+ This parameter is picked up by autodoc, please ignore.
35
41
  """
36
42
 
37
43
  stdo_level: Optional[LOGLEVELS] = None
@@ -44,7 +50,11 @@ class Params(BaseModel, extra='forbid'):
44
50
  """
45
51
  Process and validate the common configuration parameters.
46
52
 
53
+ :param enconfig: Configuration for the `.Config` object.
54
+ :param enlogger: Configuration for the `.Logger` object.
55
+ :param encrypts: Configuration for the `.Crypts` object.
47
56
  :param data: Keyword arguments passed to Pydantic model.
57
+ This parameter is picked up by autodoc, please ignore.
48
58
  """
49
59
 
50
60
  enconfig: Optional[ConfigParams] = None
@@ -25,7 +25,7 @@ SAMPLES = (
25
25
 
26
26
 
27
27
 
28
- def test_Config(
28
+ def test_Config( # noqa: CFQ001
29
29
  config_path: Path,
30
30
  ) -> None:
31
31
  """
@@ -108,6 +108,26 @@ def test_Config(
108
108
  assert sample == expect
109
109
 
110
110
 
111
+ _params1 = config.params
112
+ _params2 = config.params
113
+
114
+ assert _params1 is _params2
115
+
116
+ sample = load_sample(
117
+ path=SAMPLES.joinpath('params.json'),
118
+ update=ENPYRWS,
119
+ content=_params1.model_dump(),
120
+ replace={
121
+ 'config_path': str(config_path)})
122
+
123
+ expect = prep_sample(
124
+ content=_params2.model_dump(),
125
+ replace={
126
+ 'config_path': str(config_path)})
127
+
128
+ assert sample == expect
129
+
130
+
111
131
  logger = config.logger
112
132
 
113
133
  assert isinstance(logger, Logger)
@@ -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()
@@ -20,7 +20,7 @@ if TYPE_CHECKING:
20
20
 
21
21
 
22
22
  NUMERISH = compile(r'^\-?\d+(\.\d+)?$')
23
- SNAPABLE = compile(r'^\-|\+[\d\@a-z]+$')
23
+ SNAPABLE = compile(r'^(\-|\+)[\d\@a-z\-\+]+$')
24
24
  STRINGNOW = {'None', 'null', 'now'}
25
25
 
26
26
  NUMERIC = Union[int, float]
@@ -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.
@@ -0,0 +1,287 @@
1
+ """
2
+ Functions and routines associated with Enasis Network Common Library.
3
+
4
+ This file is part of Enasis Network software eco-system. Distribution
5
+ is permitted, for more information consult the project license file.
6
+ """
7
+
8
+
9
+
10
+ from sqlite3 import connect as SQLite
11
+ from typing import Optional
12
+ from typing import TYPE_CHECKING
13
+
14
+ from .common import NUMERIC
15
+ from .common import PARSABLE
16
+ from .times import Times
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
+
32
+
33
+
34
+ class Timers:
35
+ """
36
+ Track timers on unique key and determine when to proceed.
37
+
38
+ .. warning::
39
+ This class will use an in-memory database for cache,
40
+ unless a cache file is explicity defined.
41
+
42
+ .. testsetup::
43
+ >>> from time import sleep
44
+
45
+ Example
46
+ -------
47
+ >>> timers = Timers({'one': 1})
48
+ >>> timers.ready('one')
49
+ False
50
+ >>> sleep(1)
51
+ >>> timers.ready('one')
52
+ True
53
+
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.
58
+ """
59
+
60
+ __timers: dict[str, float]
61
+ __cache_file: 'Connection'
62
+ __cache_name: str
63
+ __cache_dict: dict[str, Times]
64
+
65
+
66
+ def __init__(
67
+ self,
68
+ timers: Optional[dict[str, NUMERIC]] = None,
69
+ cache_file: str = ':memory:',
70
+ cache_name: str = 'encommon_timers',
71
+ ) -> None:
72
+ """
73
+ Initialize instance for class using provided parameters.
74
+ """
75
+
76
+ timers = timers or {}
77
+
78
+
79
+ self.__timers = {
80
+ k: float(v)
81
+ for k, v in
82
+ timers.items()}
83
+
84
+
85
+ cached = SQLite(cache_file)
86
+
87
+ cached.execute(
88
+ TABLE.format(cache_name))
89
+
90
+ cached.commit()
91
+
92
+ self.__cache_file = cached
93
+ self.__cache_name = cache_name
94
+
95
+
96
+ self.__cache_dict = {
97
+ x: Times()
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()
155
+
156
+
157
+ @property
158
+ def timers(
159
+ self,
160
+ ) -> dict[str, float]:
161
+ """
162
+ Return the property for attribute from the class instance.
163
+
164
+ :returns: Property for attribute from the class instance.
165
+ """
166
+
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
207
+
208
+
209
+ def ready(
210
+ self,
211
+ unique: str,
212
+ update: bool = True,
213
+ ) -> bool:
214
+ """
215
+ Determine whether or not the appropriate time has passed.
216
+
217
+ .. note::
218
+ For performance reasons, this method will not notice
219
+ changes within the database unless refreshed first.
220
+
221
+ :param unique: Unique identifier for the timer in mapping.
222
+ :param update: Determines whether or not time is updated.
223
+ """
224
+
225
+ timers = self.__timers
226
+ caches = self.__cache_dict
227
+
228
+ if unique not in caches:
229
+ raise ValueError('unique')
230
+
231
+ cache = caches[unique]
232
+ timer = timers[unique]
233
+
234
+ ready = cache.elapsed >= timer
235
+
236
+ if ready and update:
237
+ self.update(unique)
238
+
239
+ return ready
240
+
241
+
242
+ def update(
243
+ self,
244
+ unique: str,
245
+ started: Optional[PARSABLE] = None,
246
+ ) -> None:
247
+ """
248
+ Update the existing timer from mapping within the cache.
249
+
250
+ :param unique: Unique identifier for the timer in mapping.
251
+ :param started: Override the start time for timer value.
252
+ """
253
+
254
+ caches = self.__cache_dict
255
+
256
+ if unique not in caches:
257
+ raise ValueError('unique')
258
+
259
+ caches[unique] = Times(started)
260
+
261
+ self.save_cache()
262
+
263
+
264
+ def create(
265
+ self,
266
+ unique: str,
267
+ minimum: int | float,
268
+ started: Optional[PARSABLE] = None,
269
+ ) -> None:
270
+ """
271
+ Update the existing timer from mapping within the cache.
272
+
273
+ :param unique: Unique identifier for the timer in mapping.
274
+ :param minimum: Determines minimum seconds that must pass.
275
+ :param started: Determines when the time starts for timer.
276
+ """
277
+
278
+ timers = self.__timers
279
+ caches = self.__cache_dict
280
+
281
+ if unique in caches:
282
+ raise ValueError('unique')
283
+
284
+ timers[unique] = float(minimum)
285
+ caches[unique] = Times(started)
286
+
287
+ self.save_cache()
@@ -8,10 +8,14 @@ is permitted, for more information consult the project license file.
8
8
 
9
9
 
10
10
  from .dicts import merge_dicts
11
+ from .dicts import sort_dict
11
12
  from .empty import Empty
13
+ from .strings import striplower
12
14
 
13
15
 
14
16
 
15
17
  __all__ = [
16
18
  'Empty',
17
- 'merge_dicts']
19
+ 'merge_dicts',
20
+ 'sort_dict',
21
+ 'striplower']
@@ -58,3 +58,29 @@ def merge_dicts(
58
58
 
59
59
  elif force is True:
60
60
  dict1[key] = value
61
+
62
+
63
+
64
+ def sort_dict(
65
+ value: dict[Any, Any],
66
+ reverse: bool = False,
67
+ ) -> dict[Any, Any]:
68
+ """
69
+ Sort the keys within the dictionary and return new one.
70
+
71
+ Example
72
+ -------
73
+ >>> foo = {'b': 'be', 'a': 'ey'}
74
+ >>> sort_dict(foo)
75
+ {'a': 'ey', 'b': 'be'}
76
+ >>> sort_dict(foo, True)
77
+ {'b': 'be', 'a': 'ey'}
78
+
79
+ :param value: Dictionary whose keys are sorted into new.
80
+ :param reverse: Optionally reverse the sort direction.
81
+ :returns: New dictionary with keys sorted alphabetical.
82
+ """
83
+
84
+ return dict(sorted(
85
+ value.items(),
86
+ reverse=reverse))
@@ -0,0 +1,25 @@
1
+ """
2
+ Functions and routines associated with Enasis Network Common Library.
3
+
4
+ This file is part of Enasis Network software eco-system. Distribution
5
+ is permitted, for more information consult the project license file.
6
+ """
7
+
8
+
9
+
10
+ def striplower(
11
+ value: str,
12
+ ) -> str:
13
+ """
14
+ Return the provided string but stripped and lower cased.
15
+
16
+ Example
17
+ -------
18
+ >>> striplower(' Foo ')
19
+ 'foo'
20
+
21
+ :param value: String which will be stripped and lowered.
22
+ :returns: Provided string but stripped and lower cased.
23
+ """
24
+
25
+ return value.strip().lower()
@@ -10,6 +10,23 @@ is permitted, for more information consult the project license file.
10
10
  from copy import deepcopy
11
11
 
12
12
  from ..dicts import merge_dicts
13
+ from ..dicts import sort_dict
14
+
15
+
16
+
17
+ DICT1 = {
18
+ 'dict1': 'dict1',
19
+ 'str': 'd1string',
20
+ 'list': ['d1list'],
21
+ 'dict': {'key': 'd1value'},
22
+ 'bool': False}
23
+
24
+ DICT2 = {
25
+ 'dict2': 'dict2',
26
+ 'str': 'd2string',
27
+ 'list': ['d2list'],
28
+ 'dict': {'key': 'd2value'},
29
+ 'bool': True}
13
30
 
14
31
 
15
32
 
@@ -18,19 +35,8 @@ def test_merge_dicts() -> None:
18
35
  Perform various tests associated with relevant routines.
19
36
  """
20
37
 
21
- dict1 = {
22
- 'dict1': 'dict1',
23
- 'str': 'd1string',
24
- 'list': ['d1list'],
25
- 'dict': {'key': 'd1value'},
26
- 'bool': False}
27
-
28
- dict2 = {
29
- 'dict2': 'dict2',
30
- 'str': 'd2string',
31
- 'list': ['d2list'],
32
- 'dict': {'key': 'd2value'},
33
- 'bool': True}
38
+ dict1 = deepcopy(DICT1)
39
+ dict2 = deepcopy(DICT2)
34
40
 
35
41
  dict1['recurse'] = deepcopy(dict1)
36
42
  dict2['recurse'] = deepcopy(dict2)
@@ -100,3 +106,17 @@ def test_merge_dicts() -> None:
100
106
  'list': ['d1list'],
101
107
  'dict': {'key': 'd1value'},
102
108
  'bool': False}}
109
+
110
+
111
+
112
+ def test_sort_dict() -> None:
113
+ """
114
+ Perform various tests associated with relevant routines.
115
+ """
116
+
117
+ assert sort_dict(DICT1) == {
118
+ 'bool': False,
119
+ 'dict': {'key': 'd1value'},
120
+ 'dict1': 'dict1',
121
+ 'list': ['d1list'],
122
+ 'str': 'd1string'}
@@ -0,0 +1,19 @@
1
+ """
2
+ Functions and routines associated with Enasis Network Common Library.
3
+
4
+ This file is part of Enasis Network software eco-system. Distribution
5
+ is permitted, for more information consult the project license file.
6
+ """
7
+
8
+
9
+
10
+ from ..strings import striplower
11
+
12
+
13
+
14
+ def test_striplower() -> None:
15
+ """
16
+ Perform various tests associated with relevant routines.
17
+ """
18
+
19
+ assert striplower(' Foo ') == 'foo'
@@ -98,7 +98,7 @@ def load_sample(
98
98
  :param update: Determine whether the sample is updated.
99
99
  :param content: Content which will be processed for JSON.
100
100
  :param default: Callable used when stringifying values.
101
- :param replace: Optional string values to replace in path.
101
+ :param replace: Optional string values to replace in file.
102
102
  :returns: Content after processing using JSON functions.
103
103
  """
104
104
 
@@ -0,0 +1 @@
1
+ 0.6.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: encommon
3
- Version: 0.4.1
3
+ Version: 0.6.0
4
4
  Summary: Enasis Network Common Library
5
5
  License: MIT
6
6
  Classifier: Programming Language :: Python :: 3
@@ -18,14 +18,15 @@ Requires-Dist: snaptime
18
18
 
19
19
  # Enasis Network Common Library
20
20
 
21
- [![](https://img.shields.io/github/actions/workflow/status/enasisnetwork/encommon/build.yml?style=flat-square&label=GitHub%20actions)](https://github.com/enasisnetwork/encommon/actions)
22
- [![](https://img.shields.io/readthedocs/encommon?style=flat-square&label=Read%20the%20Docs)](https://encommon.readthedocs.io/en/stable)
23
- [![](https://img.shields.io/pypi/v/encommon.svg?style=flat-square&label=PyPi%20version)](https://pypi.org/project/encommon)
24
- [![](https://img.shields.io/pypi/dm/encommon?style=flat-square&label=PyPi%20downloads)](https://pypi.org/project/encommon)
25
-
26
21
  Common classes and functions used in various public and private projects for
27
22
  [Enasis Network](https://github.com/enasisnetwork).
28
23
 
24
+ [![](https://img.shields.io/github/actions/workflow/status/enasisnetwork/encommon/build.yml?style=flat-square&label=GitHub%20actions)](https://github.com/enasisnetwork/encommon/actions)<br>
25
+ [![codecov](https://codecov.io/gh/enasisnetwork/encommon/graph/badge.svg?token=7PGOXKJU0E)](https://codecov.io/gh/enasisnetwork/encommon)<br>
26
+ [![](https://img.shields.io/readthedocs/encommon?style=flat-square&label=Read%20the%20Docs)](https://encommon.readthedocs.io/en/stable)<br>
27
+ [![](https://img.shields.io/pypi/v/encommon.svg?style=flat-square&label=PyPi%20version)](https://pypi.org/project/encommon)<br>
28
+ [![](https://img.shields.io/pypi/dm/encommon?style=flat-square&label=PyPi%20downloads)](https://pypi.org/project/encommon)
29
+
29
30
  ## Installing the package
30
31
  Installing stable from the PyPi repository
31
32
  ```
@@ -5,6 +5,7 @@ pyproject.toml
5
5
  reqs-install.txt
6
6
  setup.cfg
7
7
  encommon/__init__.py
8
+ encommon/conftest.py
8
9
  encommon/py.typed
9
10
  encommon/version.txt
10
11
  encommon.egg-info/PKG-INFO
@@ -20,7 +21,6 @@ encommon/config/logger.py
20
21
  encommon/config/params.py
21
22
  encommon/config/paths.py
22
23
  encommon/config/test/__init__.py
23
- encommon/config/test/conftest.py
24
24
  encommon/config/test/test_common.py
25
25
  encommon/config/test/test_config.py
26
26
  encommon/config/test/test_files.py
@@ -50,9 +50,11 @@ encommon/times/test/test_window.py
50
50
  encommon/types/__init__.py
51
51
  encommon/types/dicts.py
52
52
  encommon/types/empty.py
53
+ encommon/types/strings.py
53
54
  encommon/types/test/__init__.py
54
55
  encommon/types/test/test_dicts.py
55
56
  encommon/types/test/test_empty.py
57
+ encommon/types/test/test_strings.py
56
58
  encommon/utils/__init__.py
57
59
  encommon/utils/common.py
58
60
  encommon/utils/match.py
@@ -11,6 +11,7 @@ ignore =
11
11
  W503,
12
12
  per-file-ignores =
13
13
  encommon/crypts/hashes.py:S324,
14
+ encommon/times/timers.py:S608,
14
15
 
15
16
  [mypy]
16
17
  mypy_path = stubs
@@ -1,144 +0,0 @@
1
- """
2
- Functions and routines associated with Enasis Network Common Library.
3
-
4
- This file is part of Enasis Network software eco-system. Distribution
5
- is permitted, for more information consult the project license file.
6
- """
7
-
8
-
9
-
10
- from typing import Optional
11
-
12
- from .common import NUMERIC
13
- from .common import PARSABLE
14
- from .times import Times
15
-
16
-
17
-
18
- class Timers:
19
- """
20
- Track timers on unique key and determine when to proceed.
21
-
22
- .. testsetup::
23
- >>> from time import sleep
24
-
25
- Example
26
- -------
27
- >>> timing = {'test': 1}
28
- >>> timers = Timers(timing)
29
- >>> timers.ready('test')
30
- False
31
- >>> sleep(1)
32
- >>> timers.ready('test')
33
- True
34
-
35
- :param timing: Seconds that are used for each of timers.
36
- """
37
-
38
- __timing: dict[str, float]
39
- __caches: dict[str, Times]
40
-
41
-
42
- def __init__(
43
- self,
44
- timing: Optional[dict[str, NUMERIC]] = None,
45
- ) -> None:
46
- """
47
- Initialize instance for class using provided parameters.
48
- """
49
-
50
- timing = timing or {}
51
-
52
- self.__timing = {
53
- k: float(v)
54
- for k, v in timing.items()}
55
-
56
- self.__caches = {
57
- x: Times()
58
- for x in self.__timing}
59
-
60
-
61
- @property
62
- def timing(
63
- self,
64
- ) -> dict[str, float]:
65
- """
66
- Return the property for attribute from the class instance.
67
-
68
- :returns: Property for attribute from the class instance.
69
- """
70
-
71
- return dict(self.__timing)
72
-
73
-
74
- def ready(
75
- self,
76
- unique: str,
77
- update: bool = True,
78
- ) -> bool:
79
- """
80
- Determine whether or not the appropriate time has passed.
81
-
82
- :param unique: Unique identifier for the timer in mapping.
83
- :param update: Determines whether or not time is updated.
84
- """
85
-
86
- timer = self.__timing
87
- cache = self.__caches
88
-
89
- if unique not in cache:
90
- raise ValueError('unique')
91
-
92
- _cache = cache[unique]
93
- _timer = timer[unique]
94
-
95
- ready = _cache.elapsed >= _timer
96
-
97
- if ready and update:
98
- self.update(unique)
99
-
100
- return ready
101
-
102
-
103
- def update(
104
- self,
105
- unique: str,
106
- started: Optional[PARSABLE] = None,
107
- ) -> None:
108
- """
109
- Update the existing timer from mapping within the cache.
110
-
111
- :param unique: Unique identifier for the timer in mapping.
112
- :param started: Override the start time for timer value.
113
- """
114
-
115
- cache = self.__caches
116
-
117
- if unique not in cache:
118
- raise ValueError('unique')
119
-
120
- cache[unique] = Times(started)
121
-
122
-
123
- def create(
124
- self,
125
- unique: str,
126
- minimum: int | float,
127
- started: Optional[PARSABLE] = None,
128
- ) -> None:
129
- """
130
- Update the existing timer from mapping within the cache.
131
-
132
- :param unique: Unique identifier for the timer in mapping.
133
- :param started: Determines when the time starts for timer.
134
- :param minimum: Determines minimum seconds that must pass.
135
- """
136
-
137
- timer = self.__timing
138
- cache = self.__caches
139
-
140
- if unique in cache:
141
- raise ValueError('unique')
142
-
143
- timer[unique] = float(minimum)
144
- cache[unique] = Times(started)
@@ -1 +0,0 @@
1
- 0.4.1
File without changes
File without changes
File without changes
File without changes
File without changes