rkt-config-lib 1.3.0__tar.gz → 1.4.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.
@@ -1,12 +1,9 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: rkt_config_lib
3
- Version: 1.3.0
3
+ Version: 1.4.0
4
4
  Summary: RootKit custom PyYaml Lib
5
- Home-page: UNKNOWN
6
5
  Author: RootKit
7
6
  Author-email: rootkit@rootkit-lab.org
8
- License: UNKNOWN
9
- Platform: UNKNOWN
10
7
  Classifier: Development Status :: 5 - Production/Stable
11
8
  Classifier: Intended Audience :: Developers
12
9
  Classifier: Intended Audience :: End Users/Desktop
@@ -20,6 +17,17 @@ Classifier: Topic :: Utilities
20
17
  Requires-Python: >=3.7
21
18
  Description-Content-Type: text/markdown
22
19
  License-File: LICENSE
20
+ Requires-Dist: PyYaml
21
+ Requires-Dist: rkt_tool_lib
22
+ Requires-Dist: rkt_logger_lib
23
+ Dynamic: author
24
+ Dynamic: author-email
25
+ Dynamic: classifier
26
+ Dynamic: description
27
+ Dynamic: description-content-type
28
+ Dynamic: requires-dist
29
+ Dynamic: requires-python
30
+ Dynamic: summary
23
31
 
24
32
  # rkt_config_lib - Python library
25
33
 
@@ -94,5 +102,3 @@ If you find this library useful here's how you can help:
94
102
  - Send a merge request with your kickass new features and bug fixes
95
103
  - Help new users with [issues](https://gitlab.tprc.ovh/python/rkt_lib_toolkit/-/issues) they may encounter
96
104
  - Support the development of this library and star this repo!
97
-
98
-
@@ -1,9 +1,15 @@
1
1
  import os
2
2
  import yaml
3
3
 
4
- from rkt_lib_toolkit.tool.Tool import Tool, Singleton
4
+ try:
5
+ from rkt_logger_lib.Logger import Logger
6
+ except ImportError:
7
+ from rkt_lib_toolkit.rkt_logger_lib.Logger import Logger
5
8
 
6
- from rkt_lib_toolkit.logger.Logger import Logger
9
+ try:
10
+ from rkt_tool_lib.Tool import Tool, Singleton
11
+ except ImportError:
12
+ from rkt_lib_toolkit.rkt_tool_lib.Tool import Tool, Singleton
7
13
 
8
14
 
9
15
  class Config(metaclass=Singleton):
@@ -21,14 +27,14 @@ class Config(metaclass=Singleton):
21
27
  self._tool = Tool()
22
28
  self.data = {}
23
29
 
24
- def get_data(self, needed_file: str = "", config_dir: str = "config", create_if_not_exist: bool = False) -> None:
30
+ def get_data(self, needed_file: str = "", _config_dir: str = "config", create_if_not_exist: bool = False) -> None:
25
31
  """
26
32
  Load all file in 'config_dir' and get data in dict formatted as : {"basename_1": <VALUE_1>, ...}
27
33
  """
28
- config_dir = self._tool.get_dir(config_dir)
34
+ config_dir = self._tool.get_dir(_config_dir)
29
35
 
30
- if create_if_not_exist and not os.path.exists(config_dir):
31
- os.makedirs(config_dir, exist_ok=True)
36
+ if (not config_dir or not os.path.exists(config_dir)) and create_if_not_exist:
37
+ os.makedirs(_config_dir, exist_ok=True)
32
38
 
33
39
  if needed_file:
34
40
  filename = os.path.basename(needed_file).split(".")[0]
@@ -1,12 +1,9 @@
1
- Metadata-Version: 2.1
2
- Name: rkt-config-lib
3
- Version: 1.3.0
1
+ Metadata-Version: 2.2
2
+ Name: rkt_config_lib
3
+ Version: 1.4.0
4
4
  Summary: RootKit custom PyYaml Lib
5
- Home-page: UNKNOWN
6
5
  Author: RootKit
7
6
  Author-email: rootkit@rootkit-lab.org
8
- License: UNKNOWN
9
- Platform: UNKNOWN
10
7
  Classifier: Development Status :: 5 - Production/Stable
11
8
  Classifier: Intended Audience :: Developers
12
9
  Classifier: Intended Audience :: End Users/Desktop
@@ -20,6 +17,17 @@ Classifier: Topic :: Utilities
20
17
  Requires-Python: >=3.7
21
18
  Description-Content-Type: text/markdown
22
19
  License-File: LICENSE
20
+ Requires-Dist: PyYaml
21
+ Requires-Dist: rkt_tool_lib
22
+ Requires-Dist: rkt_logger_lib
23
+ Dynamic: author
24
+ Dynamic: author-email
25
+ Dynamic: classifier
26
+ Dynamic: description
27
+ Dynamic: description-content-type
28
+ Dynamic: requires-dist
29
+ Dynamic: requires-python
30
+ Dynamic: summary
23
31
 
24
32
  # rkt_config_lib - Python library
25
33
 
@@ -94,5 +102,3 @@ If you find this library useful here's how you can help:
94
102
  - Send a merge request with your kickass new features and bug fixes
95
103
  - Help new users with [issues](https://gitlab.tprc.ovh/python/rkt_lib_toolkit/-/issues) they may encounter
96
104
  - Support the development of this library and star this repo!
97
-
98
-
@@ -1,9 +1,13 @@
1
1
  LICENSE
2
2
  README
3
- rkt_lib_toolkit/config/Config.py
4
- rkt_lib_toolkit/config/__init__.py
3
+ rkt_lib_toolkit/rkt_config_lib/Config.py
4
+ rkt_lib_toolkit/rkt_config_lib/__init__.py
5
5
  rkt_lib_toolkit/rkt_config_lib.egg-info/PKG-INFO
6
6
  rkt_lib_toolkit/rkt_config_lib.egg-info/SOURCES.txt
7
7
  rkt_lib_toolkit/rkt_config_lib.egg-info/dependency_links.txt
8
8
  rkt_lib_toolkit/rkt_config_lib.egg-info/requires.txt
9
- rkt_lib_toolkit/rkt_config_lib.egg-info/top_level.txt
9
+ rkt_lib_toolkit/rkt_config_lib.egg-info/top_level.txt
10
+ tests/test_AI.py
11
+ tests/test_Config.py
12
+ tests/test_Logger.py
13
+ tests/test_Tool.py
@@ -0,0 +1,189 @@
1
+ import os.path
2
+ import time
3
+ from unittest import TestCase
4
+
5
+ from pandas import DataFrame
6
+
7
+ from rkt_lib_toolkit import Logger, Config
8
+ from rkt_lib_toolkit.rkt_ai_lib import QLearning
9
+
10
+ MODEL = "tests/resources/qlearning/load/load_coverage.gzip"
11
+
12
+
13
+ class TestQLearning(TestCase):
14
+ def test_me_setter(self):
15
+ ql = QLearning(actions=['a', 'b', 'c'])
16
+ ql.me = 'me'
17
+ assert ql.me == 'me'
18
+
19
+ def test_me_getter(self):
20
+ ql = QLearning(actions=['a', 'b', 'c'])
21
+ assert ql.me == 'QLearning'
22
+
23
+ def test_logger_setter(self):
24
+ ql = QLearning(actions=['a', 'b', 'c'])
25
+ ql.logger = Logger(caller_class=ql.me)
26
+ assert isinstance(ql.logger, Logger)
27
+
28
+ def test_logger_getter(self):
29
+ ql = QLearning(actions=['a', 'b', 'c'])
30
+ logger = ql.logger
31
+ assert isinstance(logger, Logger)
32
+
33
+ def test_config_setter(self):
34
+ ql = QLearning(actions=['a', 'b', 'c'])
35
+ ql.config = Config()
36
+ assert isinstance(ql.config, Config)
37
+
38
+ def test_config_getter(self):
39
+ ql = QLearning(actions=['a', 'b', 'c'])
40
+ config = ql.config
41
+ assert isinstance(config, Config)
42
+
43
+ def test_learning_rate_setter(self):
44
+ ql = QLearning(actions=['a', 'b', 'c'])
45
+ original_lr = ql.learning_rate
46
+ ql.learning_rate = 1.0
47
+ assert ql.learning_rate != original_lr
48
+
49
+ def test_learning_rate_getter(self):
50
+ ql = QLearning(actions=['a', 'b', 'c'])
51
+ original_lr = ql.learning_rate
52
+ assert ql.learning_rate == original_lr
53
+
54
+ def test_discount_factor_setter(self):
55
+ ql = QLearning(actions=['a', 'b', 'c'])
56
+ original_df = ql.discount_factor
57
+ ql.discount_factor = 1.0
58
+ assert ql.discount_factor != original_df
59
+
60
+ def test_discount_factor_getter(self):
61
+ ql = QLearning(actions=['a', 'b', 'c'])
62
+ original_df = ql.discount_factor
63
+ assert ql.discount_factor == original_df
64
+
65
+ def test_qtable_setter(self):
66
+ ql = QLearning(actions=['a', 'b', 'c'])
67
+ assert isinstance(ql.qtable, DataFrame)
68
+
69
+ def test_qtable_getter(self):
70
+ ql = QLearning(actions=['a', 'b', 'c'])
71
+ qtable = ql.qtable
72
+ assert isinstance(qtable, DataFrame)
73
+
74
+ def test_previous_state_setter(self):
75
+ ql = QLearning(actions=['a', 'b', 'c'])
76
+ ql.previous_state = 's'
77
+ assert ql.previous_state == 's'
78
+
79
+ def test_previous_state_getter(self):
80
+ ql = QLearning(actions=['a', 'b', 'c'])
81
+ assert ql.previous_state == 'start'
82
+
83
+ def test_previous_action_setter(self):
84
+ ql = QLearning(actions=['a', 'b', 'c'])
85
+ ql.previous_action = 'a'
86
+ assert ql.previous_action == 'a'
87
+
88
+ def test_previous_action_getter(self):
89
+ ql = QLearning(actions=['a', 'b', 'c'])
90
+ assert ql.previous_action == 'do-nothing'
91
+
92
+ def test_available_actions_setter(self):
93
+ ql = QLearning(actions=['a', 'b', 'c'])
94
+ assert ql.available_actions == ['a', 'b', 'c']
95
+
96
+ def test_available_actions_getter(self):
97
+ ql = QLearning(actions=['a', 'b', 'c'])
98
+ actions_list = ql.available_actions
99
+ assert actions_list == ['a', 'b', 'c']
100
+
101
+ def test00_save(self):
102
+ ql = QLearning(actions=['a', 'b', 'c'])
103
+ ql.check_state_exist(state='s')
104
+ ql.previous_state = "new_action"
105
+ ql.check_state_exist(state='s1')
106
+ ql.save("tests/resources/qlearning/save/save_coverage.gzip")
107
+ ql.save(MODEL)
108
+ assert os.path.exists("tests/resources/qlearning/save/save_coverage.gzip")
109
+
110
+ def test01_load(self):
111
+ ql = QLearning(actions=['a', 'b', 'c'])
112
+ ql.load(do_load=True, file_to_load=MODEL)
113
+ assert isinstance(ql.qtable, DataFrame)
114
+
115
+ def test03_choose_action(self):
116
+ time.sleep(1.0)
117
+ ql = QLearning(actions=['a', 'b', 'c'], should_load=True,
118
+ qtable_file_to_load=MODEL)
119
+
120
+ action = ql.choose_action(state='s')
121
+ assert action in ['a', 'b', 'c']
122
+
123
+ def test_learn(self):
124
+ ql = QLearning(actions=['a', 'b', 'c'], should_load=True,
125
+ qtable_file_to_load=MODEL)
126
+ for i in range(2):
127
+ action = ql.choose_action(state=f's{i}')
128
+ ql.learn(state=f's{i}', action=action, reward=1.0)
129
+ assert ql.qtable.index.tolist() == ['start', 's1']
130
+
131
+ def test_check_state_exist(self):
132
+ ql = QLearning(actions=['a', 'b', 'c'])
133
+ ql.check_state_exist(state='s')
134
+ qtable_len = len(ql.qtable.index)
135
+ ql.check_state_exist(state='s')
136
+ qtable2_len = len(ql.qtable.index)
137
+ ql.previous_state = "new_action"
138
+ ql.check_state_exist(state='s1')
139
+ qtable3_len = len(ql.qtable.index)
140
+ assert qtable_len == qtable2_len and qtable_len != qtable3_len
141
+
142
+ def test_representation(self):
143
+ ql = QLearning(actions=['a', 'b', 'c'])
144
+ assert ql.__repr__() == f"QLearning(learning_rate={ql.learning_rate}, discount_factor={ql.discount_factor}, learning_method={ql._learning_method})"
145
+
146
+ def test_me_setter_error(self):
147
+ ql = QLearning(actions=['a', 'b', 'c'])
148
+ with self.assertRaises(TypeError):
149
+ ql.me = 1
150
+
151
+ def test_logger_setter_error(self):
152
+ ql = QLearning(actions=['a', 'b', 'c'])
153
+ with self.assertRaises(TypeError):
154
+ ql.logger = 1
155
+
156
+ def test_config_setter_error(self):
157
+ ql = QLearning(actions=['a', 'b', 'c'])
158
+ with self.assertRaises(TypeError):
159
+ ql.config = 1
160
+
161
+ def test_learning_rate_setter_error(self):
162
+ ql = QLearning(actions=['a', 'b', 'c'])
163
+ with self.assertRaises(TypeError):
164
+ ql.learning_rate = "0"
165
+
166
+ def test_discount_factor_setter_error(self):
167
+ ql = QLearning(actions=['a', 'b', 'c'])
168
+ with self.assertRaises(TypeError):
169
+ ql.discount_factor = "0"
170
+
171
+ def test_qtable_setter_error(self):
172
+ ql = QLearning(actions=['a', 'b', 'c'])
173
+ with self.assertRaises(TypeError):
174
+ ql.qtable = "0"
175
+
176
+ def test_previous_state_setter_error(self):
177
+ ql = QLearning(actions=['a', 'b', 'c'])
178
+ with self.assertRaises(TypeError):
179
+ ql.previous_state = 1
180
+
181
+ def test_previous_action_setter_error(self):
182
+ ql = QLearning(actions=['a', 'b', 'c'])
183
+ with self.assertRaises(TypeError):
184
+ ql.previous_action = 1
185
+
186
+ def test_available_actions_setter_error(self):
187
+ ql = QLearning(actions=['a', 'b', 'c'])
188
+ with self.assertRaises(TypeError):
189
+ ql.available_actions = 1
@@ -0,0 +1,52 @@
1
+ import os
2
+ import shutil
3
+
4
+ from unittest import TestCase
5
+
6
+ from yaml.scanner import ScannerError
7
+ from rkt_lib_toolkit.rkt_config_lib import Config
8
+
9
+
10
+ class TestConfig(TestCase):
11
+ def test_get_data(self):
12
+ config = Config()
13
+ config.data.clear()
14
+ config.get_data(_config_dir="valid_config")
15
+
16
+ assert config.data == {
17
+ 'valid_config': {'key1': {
18
+ 'sub_key': 'value',
19
+ 'sub_key2': 'value',
20
+ 'sub_key3': 'value',
21
+ 'sub_key4': 'value'
22
+ }
23
+ }
24
+ }
25
+
26
+ def test_get_data_dummy_data(self):
27
+ config = Config()
28
+ config.data.clear()
29
+ try:
30
+ config.get_data(_config_dir="dummy_config")
31
+ self.fail("ScannerError expected here!")
32
+ except ScannerError:
33
+ assert config.data == {}
34
+
35
+ def test_get_data_dummy_folder(self):
36
+ config = Config()
37
+ config.data.clear()
38
+ config.get_data(_config_dir="missing_folder", create_if_not_exist=True)
39
+ assert os.path.exists("missing_folder")
40
+ shutil.rmtree("missing_folder")
41
+
42
+ def test_get_data_from_specific_file(self):
43
+ config = Config()
44
+ config.data.clear()
45
+ config.get_data(_config_dir="valid_config", needed_file="valid_config.yml")
46
+
47
+ assert config.data == {
48
+ 'valid_config': {'key1': {
49
+ 'sub_key': 'value', 'sub_key2': 'value', 'sub_key3': 'value', 'sub_key4': 'value'
50
+ }
51
+ }
52
+ }
@@ -0,0 +1,53 @@
1
+ import os
2
+ import shutil
3
+ from datetime import datetime
4
+ from unittest import TestCase
5
+
6
+ from rkt_lib_toolkit.rkt_logger_lib import Logger
7
+ from rkt_lib_toolkit.rkt_exception_lib import InvalidLogLevelError, LogIsNotDirError
8
+ from rkt_lib_toolkit.rkt_tool_lib import Singleton, Tool
9
+
10
+ tool = Tool()
11
+
12
+
13
+ class TestLogger(TestCase):
14
+
15
+ def test_logger_when_log_is_not_directory(self):
16
+ log = f"{tool.formatted_from_os(tool.get_cwd())}log_misstake"
17
+ if os.path.exists(log):
18
+ if os.path.isdir(log):
19
+ shutil.rmtree(log)
20
+ else:
21
+ os.remove(log)
22
+
23
+ open(log, "x").close()
24
+ Singleton._instances = {}
25
+ with self.assertRaises(LogIsNotDirError) as context:
26
+ logger = Logger("coverage", "log_misstake")
27
+
28
+ assert str(context.exception) == "\"log\" isn't a directory"
29
+ os.remove(log)
30
+
31
+ def test_add_without_exiting_log_level(self):
32
+ logger = Logger("coverage", "log")
33
+ logger.set_logger(caller_class="coverage", output="stream")
34
+ with self.assertRaises(InvalidLogLevelError):
35
+ logger.add("coverage", "tests coverage", 42)
36
+
37
+ def test_add_with_exiting_log_level(self):
38
+ logger = Logger("coverage", "log")
39
+ logger.set_logger(caller_class="coverage", output="stream")
40
+ assert logger.add("coverage", "tests coverage", 20) is None
41
+
42
+ def test_set_logger(self):
43
+ logger = Logger("coverage", "log")
44
+ logger.set_logger(caller_class="coverage", output="both", out_file="coverage", level=10)
45
+ logger.add("coverage", "test", 20)
46
+ excepted = f"{logger._tool.get_dir(folder='log')}coverage_{datetime.today().date()}.log"
47
+ obtained = logger.get_logger_file('coverage')
48
+ assert obtained == excepted, print(f"Expected: {excepted}\nObtained: {obtained}")
49
+
50
+ def test_no_logger_file(self):
51
+ logger = Logger("coverage", "log")
52
+ logger.set_logger(caller_class="coverage", output="both", out_file="coverage")
53
+ assert logger.get_logger_file('not_exist') is None
@@ -0,0 +1,45 @@
1
+ import os
2
+
3
+ from rkt_lib_toolkit.rkt_tool_lib import Tool, Singleton
4
+
5
+ obj = Tool()
6
+
7
+
8
+ class FakeClass(metaclass=Singleton):
9
+ def __init__(self):
10
+ self.name = "singleton"
11
+
12
+
13
+ class TestTool:
14
+ def test_formatted_from_os(self):
15
+ if os.name == "nt":
16
+ excepted = obj.formatted_from_os(os.getcwd()).endswith("\\")
17
+ else:
18
+ excepted = obj.formatted_from_os(os.getcwd()).endswith("/")
19
+ assert excepted
20
+
21
+ def test_get_cwd(self):
22
+ assert obj.get_cwd() == f"{os.getcwd()}\\" if os.name == "nt" else f"{os.getcwd()}/"
23
+
24
+ def test_get_dir_flat(self):
25
+ if os.name == "nt":
26
+ excepted = f"{os.getcwd()}\\tests\\"
27
+ else:
28
+ excepted = f"{os.getcwd()}/tests/"
29
+ assert obj.get_dir("tests") == excepted
30
+
31
+ def test_get_dir_recursive(self):
32
+ if os.name == "nt":
33
+ excepted = f"{os.getcwd()}\\tests\\resources\\"
34
+ else:
35
+ excepted = f"{os.getcwd()}/tests/resources/"
36
+
37
+ assert obj.get_dir("resources") == excepted
38
+
39
+
40
+ class TestSingleton:
41
+ def test_singleton(self):
42
+ obj_1 = FakeClass()
43
+ obj_2 = FakeClass()
44
+ assert obj_1 == obj_2
45
+
File without changes
File without changes