ka-uts-com 2.0.0.250407__py3-none-any.whl → 2.1.1.250415__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.
- ka_uts_com/__version__.py +1 -1
- ka_uts_com/{base/app_.py → app.py} +8 -8
- ka_uts_com/{base/cfg_.py → cfg.py} +8 -7
- ka_uts_com/com.py +47 -47
- build/lib/ka_uts_com/base/exit_.py → ka_uts_com/exit.py +1 -1
- ka_uts_com/timer.py +1 -1
- ka_uts_com-2.1.1.250415.dist-info/METADATA +910 -0
- ka_uts_com-2.1.1.250415.dist-info/RECORD +14 -0
- {ka_uts_com-2.0.0.250407.dist-info → ka_uts_com-2.1.1.250415.dist-info}/top_level.txt +0 -1
- build/lib/ka_uts_com/__init__.py +0 -0
- build/lib/ka_uts_com/__version__.py +0 -10
- build/lib/ka_uts_com/base/app_.py +0 -50
- build/lib/ka_uts_com/base/cfg_.py +0 -35
- build/lib/ka_uts_com/base/log_.py +0 -131
- build/lib/ka_uts_com/com.py +0 -121
- build/lib/ka_uts_com/data/__init__.py +0 -0
- build/lib/ka_uts_com/data/log.std.yml +0 -107
- build/lib/ka_uts_com/data/log.usr.yml +0 -106
- build/lib/ka_uts_com/decorators/dec.py +0 -13
- build/lib/ka_uts_com/fnc.py +0 -42
- build/lib/ka_uts_com/ioc/jinja2_.py +0 -42
- build/lib/ka_uts_com/ioc/yaml_.py +0 -30
- build/lib/ka_uts_com/log.py +0 -86
- build/lib/ka_uts_com/py.typed +0 -0
- build/lib/ka_uts_com/timer.py +0 -69
- build/lib/ka_uts_com/utils/aoeqstmt.py +0 -37
- build/lib/ka_uts_com/utils/date.py +0 -15
- build/lib/ka_uts_com/utils/doeq.py +0 -99
- build/lib/ka_uts_com/utils/pacmod.py +0 -123
- build/lib/ka_uts_com/utils/str.py +0 -265
- ka_uts_com/base/exit_.py +0 -37
- ka_uts_com/base/log_.py +0 -131
- ka_uts_com/data/__init__.py +0 -0
- ka_uts_com/data/log.std.yml +0 -107
- ka_uts_com/data/log.usr.yml +0 -106
- ka_uts_com/fnc.py +0 -42
- ka_uts_com/ioc/jinja2_.py +0 -42
- ka_uts_com/ioc/yaml_.py +0 -30
- ka_uts_com/log.py +0 -86
- ka_uts_com/utils/aoeqstmt.py +0 -37
- ka_uts_com/utils/date.py +0 -15
- ka_uts_com/utils/doeq.py +0 -99
- ka_uts_com/utils/pacmod.py +0 -123
- ka_uts_com/utils/str.py +0 -265
- ka_uts_com-2.0.0.250407.dist-info/METADATA +0 -1611
- ka_uts_com-2.0.0.250407.dist-info/RECORD +0 -49
- {ka_uts_com-2.0.0.250407.dist-info → ka_uts_com-2.1.1.250415.dist-info}/WHEEL +0 -0
- {ka_uts_com-2.0.0.250407.dist-info → ka_uts_com-2.1.1.250415.dist-info}/licenses/LICENSE.txt +0 -0
ka_uts_com/ioc/jinja2_.py
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
# coding=utf-8
|
2
|
-
# from collections.abc import Callable
|
3
|
-
from typing import Any
|
4
|
-
|
5
|
-
import os
|
6
|
-
import yaml
|
7
|
-
import jinja2
|
8
|
-
# import logging
|
9
|
-
# import logging.config
|
10
|
-
from logging import Logger
|
11
|
-
|
12
|
-
TyAny = Any
|
13
|
-
TyDic = dict[Any, Any]
|
14
|
-
TyLogger = Logger
|
15
|
-
|
16
|
-
TnDic = None | TyDic
|
17
|
-
|
18
|
-
|
19
|
-
class Jinja2_:
|
20
|
-
|
21
|
-
""" Manage Object to Json file affilitation
|
22
|
-
"""
|
23
|
-
@staticmethod
|
24
|
-
def read_template(path: str) -> Any:
|
25
|
-
directory, file = os.path.split(path)
|
26
|
-
env = jinja2.Environment(loader=jinja2.FileSystemLoader(directory))
|
27
|
-
return env.get_template(file)
|
28
|
-
|
29
|
-
@classmethod
|
30
|
-
def read(cls, path: str, log: TyLogger, **kwargs) -> Any:
|
31
|
-
try:
|
32
|
-
# read jinja template from file
|
33
|
-
template = cls.read_template(path)
|
34
|
-
# render template as yaml string
|
35
|
-
template_rendered = template.render(kwargs)
|
36
|
-
# load yaml string into object
|
37
|
-
obj: Any = yaml.safe_load(template_rendered)
|
38
|
-
return obj
|
39
|
-
except IOError as exc:
|
40
|
-
log.error(exc, exc_info=True)
|
41
|
-
# log.error(f"No such file or directory: {path}")
|
42
|
-
raise
|
ka_uts_com/ioc/yaml_.py
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
# coding=utf-8
|
2
|
-
import yaml
|
3
|
-
|
4
|
-
from typing import Any
|
5
|
-
TyAny = Any
|
6
|
-
TyArr = list[Any]
|
7
|
-
TyDic = dict[Any, Any]
|
8
|
-
|
9
|
-
TnAny = None | Any
|
10
|
-
|
11
|
-
|
12
|
-
class Yaml_:
|
13
|
-
""" Manage Object to Yaml file affilitation
|
14
|
-
"""
|
15
|
-
@staticmethod
|
16
|
-
def read(path: str, log) -> TnAny:
|
17
|
-
try:
|
18
|
-
with open(path) as fd:
|
19
|
-
# The Loader parameter handles the conversion from YAML
|
20
|
-
# scalar values to Python object format
|
21
|
-
obj = yaml.load(fd, Loader=yaml.SafeLoader)
|
22
|
-
return obj
|
23
|
-
except FileNotFoundError:
|
24
|
-
log.error(f"No such file or directory: {path}")
|
25
|
-
except IOError:
|
26
|
-
# if Com.Log is not None:
|
27
|
-
# fnc_error = Com.Log.error
|
28
|
-
# fnc_error(exc, exc_info=True)
|
29
|
-
raise
|
30
|
-
return None
|
ka_uts_com/log.py
DELETED
@@ -1,86 +0,0 @@
|
|
1
|
-
from ka_uts_com.com import Com
|
2
|
-
from typing import Any
|
3
|
-
TyDic = dict[Any, Any]
|
4
|
-
|
5
|
-
|
6
|
-
class Log:
|
7
|
-
"""Logging Class
|
8
|
-
"""
|
9
|
-
class Eq:
|
10
|
-
|
11
|
-
@classmethod
|
12
|
-
def error(cls, key: Any, value: Any) -> None:
|
13
|
-
Log.error(f"{key} = {value}", stacklevel=3)
|
14
|
-
|
15
|
-
@classmethod
|
16
|
-
def warning(cls, key: Any, value: Any) -> None:
|
17
|
-
Log.warning(f"{key} = {value}", stacklevel=3)
|
18
|
-
|
19
|
-
@classmethod
|
20
|
-
def info(cls, key: Any, value: Any) -> None:
|
21
|
-
Log.info(f"{key} = {value}", stacklevel=3)
|
22
|
-
|
23
|
-
@classmethod
|
24
|
-
def log(cls, key: Any, value: Any) -> None:
|
25
|
-
Log.log(f"{key} = {value}", stacklevel=3)
|
26
|
-
|
27
|
-
@classmethod
|
28
|
-
def debug(cls, key: Any, value: Any) -> None:
|
29
|
-
Log.debug(f"{key} = {value}", stacklevel=3)
|
30
|
-
|
31
|
-
class Dic:
|
32
|
-
|
33
|
-
@classmethod
|
34
|
-
def debug(cls, dic: TyDic) -> None:
|
35
|
-
for key, value in dic.items():
|
36
|
-
Log.debug(f"{key} = {value}", stacklevel=3)
|
37
|
-
|
38
|
-
@classmethod
|
39
|
-
def error(cls, dic: TyDic) -> None:
|
40
|
-
for key, value in dic.items():
|
41
|
-
Log.error(f"{key} = {value}", stacklevel=3)
|
42
|
-
|
43
|
-
@classmethod
|
44
|
-
def info(cls, dic: TyDic) -> None:
|
45
|
-
for key, value in dic.items():
|
46
|
-
Log.info(f"{key} = {value}", stacklevel=3)
|
47
|
-
|
48
|
-
@classmethod
|
49
|
-
def warning(cls, dic: TyDic) -> None:
|
50
|
-
for key, value in dic.items():
|
51
|
-
Log.warning(f"{key} = {value}", stacklevel=3)
|
52
|
-
|
53
|
-
@staticmethod
|
54
|
-
def error(*args, **kwargs) -> None:
|
55
|
-
if kwargs is None:
|
56
|
-
kwargs = {}
|
57
|
-
kwargs['stacklevel'] = kwargs.get('stacklevel', 2)
|
58
|
-
Com.Log.error(*args, **kwargs)
|
59
|
-
|
60
|
-
@staticmethod
|
61
|
-
def warning(*args, **kwargs) -> None:
|
62
|
-
if kwargs is None:
|
63
|
-
kwargs = {}
|
64
|
-
kwargs['stacklevel'] = kwargs.get('stacklevel', 2)
|
65
|
-
Com.Log.warning(*args, **kwargs)
|
66
|
-
|
67
|
-
@staticmethod
|
68
|
-
def info(*args, **kwargs) -> None:
|
69
|
-
if kwargs is None:
|
70
|
-
kwargs = {}
|
71
|
-
kwargs['stacklevel'] = kwargs.get('stacklevel', 2)
|
72
|
-
Com.Log.info(*args, **kwargs)
|
73
|
-
|
74
|
-
@staticmethod
|
75
|
-
def log(*args, **kwargs) -> None:
|
76
|
-
if kwargs is None:
|
77
|
-
kwargs = {}
|
78
|
-
kwargs['stacklevel'] = kwargs.get('stacklevel', 2)
|
79
|
-
Com.Log.log(*args, **kwargs)
|
80
|
-
|
81
|
-
@staticmethod
|
82
|
-
def debug(*args, **kwargs) -> None:
|
83
|
-
if kwargs is None:
|
84
|
-
kwargs = {}
|
85
|
-
kwargs['stacklevel'] = kwargs.get('stacklevel', 2)
|
86
|
-
Com.Log.debug(*args, **kwargs)
|
ka_uts_com/utils/aoeqstmt.py
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
# coding=utf-8
|
2
|
-
from ka_uts_com.utils.doeq import DoEq
|
3
|
-
|
4
|
-
from typing import Any
|
5
|
-
TyArr = list[Any]
|
6
|
-
TyDic = dict[Any, Any]
|
7
|
-
|
8
|
-
TnArr = None | TyArr
|
9
|
-
TnDic = None | TyDic
|
10
|
-
TnStr = None | str
|
11
|
-
|
12
|
-
|
13
|
-
class AoEqStmt:
|
14
|
-
""" Dictionary of Equates
|
15
|
-
"""
|
16
|
-
@staticmethod
|
17
|
-
def init_d_eq(a_eqstmt: TyArr) -> TyDic:
|
18
|
-
d_eq = {}
|
19
|
-
for s_eq in a_eqstmt[1:]:
|
20
|
-
a_eq = s_eq.split('=')
|
21
|
-
if len(a_eq) == 1:
|
22
|
-
d_eq['cmd'] = a_eq[0]
|
23
|
-
else:
|
24
|
-
d_eq[a_eq[0]] = a_eq[1]
|
25
|
-
d_eq[a_eq[0]] = a_eq[1]
|
26
|
-
return d_eq
|
27
|
-
|
28
|
-
@classmethod
|
29
|
-
def sh_d_eq(cls, a_eqstmt: TyArr, **kwargs) -> TyDic:
|
30
|
-
""" show equates dictionary
|
31
|
-
"""
|
32
|
-
d_parms: TnDic = kwargs.get('d_parms')
|
33
|
-
_sh_prof = kwargs.get('sh_prof')
|
34
|
-
d_eq: TyDic = cls.init_d_eq(a_eqstmt)
|
35
|
-
d_eq_new: TyDic = DoEq.verify(d_eq, d_parms)
|
36
|
-
DoEq._set_sh_prof(d_eq_new, _sh_prof)
|
37
|
-
return d_eq_new
|
ka_uts_com/utils/date.py
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
from datetime import datetime
|
2
|
-
from datetime import date
|
3
|
-
|
4
|
-
TyDate = date
|
5
|
-
TnDate = None | TyDate
|
6
|
-
TnStr = None | str
|
7
|
-
|
8
|
-
|
9
|
-
class Date:
|
10
|
-
|
11
|
-
@staticmethod
|
12
|
-
def sh(datestring: TnStr, fmt: str) -> TnDate:
|
13
|
-
if not datestring:
|
14
|
-
return None
|
15
|
-
return datetime.strptime(datestring, fmt).date()
|
ka_uts_com/utils/doeq.py
DELETED
@@ -1,99 +0,0 @@
|
|
1
|
-
# coding=utf-8
|
2
|
-
from collections.abc import Callable
|
3
|
-
from typing import Any
|
4
|
-
|
5
|
-
# from ka_uts_com.utils.pacmod import PacMod
|
6
|
-
from ka_uts_com.utils.str import Str
|
7
|
-
from ka_uts_com.utils.date import Date
|
8
|
-
|
9
|
-
|
10
|
-
TyArr = list[Any]
|
11
|
-
TyCall = Callable[..., Any]
|
12
|
-
TyDic = dict[Any, Any]
|
13
|
-
TyStr = str
|
14
|
-
|
15
|
-
TnArr = None | TyArr
|
16
|
-
TnDic = None | TyDic
|
17
|
-
TnStr = None | TyStr
|
18
|
-
|
19
|
-
|
20
|
-
class DoEq:
|
21
|
-
""" Manage Commandline Arguments
|
22
|
-
"""
|
23
|
-
@classmethod
|
24
|
-
def sh_value(cls, key: str, value: Any, d_valid_parms: TnDic) -> Any:
|
25
|
-
|
26
|
-
# print(f"key = {key}, type(key) = {type(key)}")
|
27
|
-
# print(f"value = {value}, type(value) = {type(value)}")
|
28
|
-
if not d_valid_parms:
|
29
|
-
return value
|
30
|
-
_type: TnStr = d_valid_parms.get(key)
|
31
|
-
# print(f"_type = {_type}")
|
32
|
-
if not _type:
|
33
|
-
return value
|
34
|
-
if isinstance(_type, str):
|
35
|
-
match _type:
|
36
|
-
case 'int':
|
37
|
-
value = int(value)
|
38
|
-
case 'bool':
|
39
|
-
value = Str.sh_boolean(value)
|
40
|
-
case 'dict':
|
41
|
-
value = Str.sh_dic(value)
|
42
|
-
case 'list':
|
43
|
-
value = Str.sh_arr(value)
|
44
|
-
case '%Y-%m-%d':
|
45
|
-
value = Date.sh(value, _type)
|
46
|
-
case '_':
|
47
|
-
match _type[0]:
|
48
|
-
case '[', '{':
|
49
|
-
_obj = Str.sh_dic(_type)
|
50
|
-
if value not in _obj:
|
51
|
-
msg = (f"parameter={key} value={value} is invalid; "
|
52
|
-
f"valid values are={_obj}")
|
53
|
-
raise Exception(msg)
|
54
|
-
|
55
|
-
# print(f"value = {value}, type(value) = {type(value)}")
|
56
|
-
return value
|
57
|
-
|
58
|
-
# @staticmethod
|
59
|
-
# def _set_d_pacmod(d_eq: TyDic, root_cls) -> None:
|
60
|
-
# """ set current pacmod dictionary
|
61
|
-
# """
|
62
|
-
# tenant = d_eq.get('tenant')
|
63
|
-
# d_eq['d_pacmod'] = PacMod.sh_d_pacmod(root_cls, tenant)
|
64
|
-
|
65
|
-
@staticmethod
|
66
|
-
def _set_sh_prof(d_eq: TyDic, sh_prof: TyCall | Any) -> None:
|
67
|
-
""" set current pacmod dictionary
|
68
|
-
"""
|
69
|
-
if callable(sh_prof):
|
70
|
-
d_eq['sh_prof'] = sh_prof()
|
71
|
-
else:
|
72
|
-
d_eq['sh_prof'] = sh_prof
|
73
|
-
|
74
|
-
@classmethod
|
75
|
-
def verify(cls, d_eq: TyDic, d_parms: TnDic) -> TyDic:
|
76
|
-
if d_parms is None:
|
77
|
-
return d_eq
|
78
|
-
if 'cmd' in d_eq:
|
79
|
-
_d_valid_parms = d_parms
|
80
|
-
_cmd = d_eq['cmd']
|
81
|
-
_valid_commands = list(d_parms.keys())
|
82
|
-
if _cmd not in _valid_commands:
|
83
|
-
msg = (f"Wrong command: {_cmd}; "
|
84
|
-
f"valid commands are: {_valid_commands}")
|
85
|
-
raise Exception(msg)
|
86
|
-
_d_valid_parms = d_parms[_cmd]
|
87
|
-
else:
|
88
|
-
_d_valid_parms = d_parms
|
89
|
-
if _d_valid_parms is None:
|
90
|
-
return d_eq
|
91
|
-
|
92
|
-
d_eq_new = {}
|
93
|
-
for key, value in d_eq.items():
|
94
|
-
if key not in _d_valid_parms:
|
95
|
-
msg = (f"Wrong parameter: {key}; "
|
96
|
-
f"valid parameters are: {_d_valid_parms}")
|
97
|
-
raise Exception(msg)
|
98
|
-
d_eq_new[key] = cls.sh_value(key, value, _d_valid_parms)
|
99
|
-
return d_eq_new
|
ka_uts_com/utils/pacmod.py
DELETED
@@ -1,123 +0,0 @@
|
|
1
|
-
# coding=utf-8
|
2
|
-
from typing import Any
|
3
|
-
|
4
|
-
from os import path as os_path
|
5
|
-
# import pkg_resources
|
6
|
-
import importlib.resources as resources
|
7
|
-
|
8
|
-
TyArr = list[Any]
|
9
|
-
TyDic = dict[Any, Any]
|
10
|
-
TyPath = str
|
11
|
-
|
12
|
-
TnDic = None | TyDic
|
13
|
-
TnPath = None | TyPath
|
14
|
-
|
15
|
-
|
16
|
-
class PacMod:
|
17
|
-
""" Package Module Management
|
18
|
-
"""
|
19
|
-
@staticmethod
|
20
|
-
def sh_d_pacmod(cls) -> TyDic:
|
21
|
-
""" Show Pacmod Dictionary
|
22
|
-
"""
|
23
|
-
a_pacmod: TyArr = cls.__module__.split(".")
|
24
|
-
return {'package': a_pacmod[0], 'module': a_pacmod[1]}
|
25
|
-
|
26
|
-
@staticmethod
|
27
|
-
def sh_path_module_yaml(d_pacmod: TyDic) -> str:
|
28
|
-
""" show directory
|
29
|
-
"""
|
30
|
-
# def sh_path_cfg_yaml(d_pacmod: TyDic) -> str:
|
31
|
-
package = d_pacmod['package']
|
32
|
-
module = d_pacmod['module']
|
33
|
-
filename = f"{module}.yml"
|
34
|
-
file = resources.files(f"{package}.data")
|
35
|
-
print("sh_path_module_yaml files = {files}")
|
36
|
-
path: TyPath = file.joinpath(filename)
|
37
|
-
return path
|
38
|
-
|
39
|
-
@staticmethod
|
40
|
-
def sh_path_keys(
|
41
|
-
d_pacmod: TyDic, filename: str = 'keys.yml') -> TyPath:
|
42
|
-
""" show directory
|
43
|
-
"""
|
44
|
-
package = d_pacmod['package']
|
45
|
-
files = resources.files(f"{package}.data")
|
46
|
-
print("sh_path_keys files = {files}")
|
47
|
-
path: TyPath = files.joinpath(filename)
|
48
|
-
return path
|
49
|
-
|
50
|
-
# @staticmethod
|
51
|
-
# def sh_path_log_cfg(com) -> TyPath:
|
52
|
-
# """ show directory
|
53
|
-
# """
|
54
|
-
# package = com.d_app_pacmod['package']
|
55
|
-
# path = resources.files(package).joinpath(f"data/log.{com.log_type}.yml")
|
56
|
-
# if path.is_file():
|
57
|
-
# return path
|
58
|
-
# package = com.d_com_pacmod['package']
|
59
|
-
# path = resources.files(package).joinpath(f"data/log.{com.log_type}.yml")
|
60
|
-
# if path.is_file():
|
61
|
-
# return path
|
62
|
-
# raise ModuleNotFoundError
|
63
|
-
|
64
|
-
@staticmethod
|
65
|
-
def sh_path_cfg(com) -> TyPath:
|
66
|
-
""" show directory
|
67
|
-
"""
|
68
|
-
filename = 'cfg.yml'
|
69
|
-
package = com.d_app_pacmod['package']
|
70
|
-
files = resources.files(f"{package}.data")
|
71
|
-
print(f"sh_path_cfg files = {files}")
|
72
|
-
path: TyPath = files.joinpath(filename)
|
73
|
-
return path
|
74
|
-
|
75
|
-
# @staticmethod
|
76
|
-
# def sh_path_type(d_pacmod: TyDic, type_: str) -> str:
|
77
|
-
# """ show Data File Path
|
78
|
-
# """
|
79
|
-
# # def sh_pacmod_type(d_pacmod: TyDic, type_: str) -> str:
|
80
|
-
# package = d_pacmod['package']
|
81
|
-
# module = d_pacmod['module']
|
82
|
-
# return f"/data/{package}/{module}/{type_}"
|
83
|
-
|
84
|
-
# @classmethod
|
85
|
-
# def sh_file_path(
|
86
|
-
# cls, d_pacmod: TyDic, type_: str, suffix: str,
|
87
|
-
# pid: Any, ts: Any, **kwargs) -> str:
|
88
|
-
# """ show type specific path
|
89
|
-
# """
|
90
|
-
# filename_ = kwargs.get('filename', type_)
|
91
|
-
# sw_run_pid_ts = kwargs.get('sw_run_pid_ts', True)
|
92
|
-
# if sw_run_pid_ts is None:
|
93
|
-
# sw_run_pid_ts = True
|
94
|
-
#
|
95
|
-
# _dir: str = cls.sh_pacmod_type(d_pacmod, type_)
|
96
|
-
# if sw_run_pid_ts:
|
97
|
-
# file_path = os_path.join(
|
98
|
-
# _dir, f"{filename_}_{pid}_{ts}.{suffix}")
|
99
|
-
# else:
|
100
|
-
# file_path = os_path.join(_dir, f"{filename_}.{suffix}")
|
101
|
-
# return file_path
|
102
|
-
|
103
|
-
@staticmethod
|
104
|
-
def sh_dir_type(com, type_: str) -> str:
|
105
|
-
"""Show run_dir
|
106
|
-
"""
|
107
|
-
dir_dat: str = com.dir_dat
|
108
|
-
tenant: str = com.tenant
|
109
|
-
package: str = com.d_app_pacmod['package']
|
110
|
-
module: str = com.d_app_pacmod['module']
|
111
|
-
if not tenant:
|
112
|
-
return f"{dir_dat}/{tenant}/{package}/{module}/{type_}"
|
113
|
-
else:
|
114
|
-
return f"{dir_dat}/{package}/{module}/{type_}"
|
115
|
-
|
116
|
-
@classmethod
|
117
|
-
def sh_path_pattern(
|
118
|
-
cls, com, filename, type_: str, suffix: str) -> str:
|
119
|
-
""" show type specific path
|
120
|
-
"""
|
121
|
-
_dir: str = cls.sh_dir_type(com, type_)
|
122
|
-
path: TyPath = os_path.join(_dir, f"{filename}*.{suffix}")
|
123
|
-
return path
|
ka_uts_com/utils/str.py
DELETED
@@ -1,265 +0,0 @@
|
|
1
|
-
# coding=utf-8
|
2
|
-
from typing import Any
|
3
|
-
import traceback
|
4
|
-
|
5
|
-
from datetime import datetime
|
6
|
-
import re
|
7
|
-
import orjson
|
8
|
-
import simplejson
|
9
|
-
|
10
|
-
TyArr = list[Any]
|
11
|
-
TyDic = dict[Any, Any]
|
12
|
-
TyAoA = list[TyArr]
|
13
|
-
|
14
|
-
TnInt = None | int
|
15
|
-
TnFloat = None | float
|
16
|
-
TnStr = None | str
|
17
|
-
TnAoA = None | TyAoA
|
18
|
-
TnArr = None | TyArr
|
19
|
-
TnDic = None | TyDic
|
20
|
-
TnDatetime = None | datetime
|
21
|
-
|
22
|
-
|
23
|
-
class Str:
|
24
|
-
""" Manage String Class
|
25
|
-
"""
|
26
|
-
@staticmethod
|
27
|
-
def sh_date(string: str, fmt: TnStr = None) -> TnDatetime:
|
28
|
-
""" show string as date using the format string
|
29
|
-
"""
|
30
|
-
try:
|
31
|
-
if fmt is None:
|
32
|
-
fmt = "%m/%d/%Y"
|
33
|
-
_date: TnDatetime = datetime.strptime(string, fmt)
|
34
|
-
return _date
|
35
|
-
except Exception:
|
36
|
-
print(traceback.format_exc())
|
37
|
-
return None
|
38
|
-
|
39
|
-
@staticmethod
|
40
|
-
def lchop(string: str, prefix: str) -> str:
|
41
|
-
""" return substring of string which starts at the
|
42
|
-
end of the contained prefix
|
43
|
-
"""
|
44
|
-
if string.startswith(prefix):
|
45
|
-
return string[len(prefix):]
|
46
|
-
return string
|
47
|
-
|
48
|
-
@staticmethod
|
49
|
-
def rchop(string: str, suffix: str) -> str:
|
50
|
-
""" return substring of string which ends at the
|
51
|
-
beginning of the contained suffix
|
52
|
-
"""
|
53
|
-
if suffix and string.endswith(suffix):
|
54
|
-
return string[:-len(suffix)]
|
55
|
-
return string
|
56
|
-
|
57
|
-
@staticmethod
|
58
|
-
def strip_multiple_chars(string: str, chars: str) -> str:
|
59
|
-
""" replace multiple characters in string
|
60
|
-
"""
|
61
|
-
return string.translate(str.maketrans("", "", chars))
|
62
|
-
|
63
|
-
@staticmethod
|
64
|
-
def is_odd(string: str) -> bool:
|
65
|
-
""" check if string is odd number
|
66
|
-
"""
|
67
|
-
if string.isnumeric():
|
68
|
-
if int(string) % 2 == 0:
|
69
|
-
return False
|
70
|
-
return True
|
71
|
-
return False
|
72
|
-
|
73
|
-
@staticmethod
|
74
|
-
def is_integer(string: str) -> bool:
|
75
|
-
""" check if string is integer
|
76
|
-
"""
|
77
|
-
if string[0] in ('-', '+'):
|
78
|
-
return string[1:].isdigit()
|
79
|
-
return string.isdigit()
|
80
|
-
|
81
|
-
@staticmethod
|
82
|
-
def is_boolean(string: str) -> bool:
|
83
|
-
""" check if string is boolean
|
84
|
-
"""
|
85
|
-
if string.strip().lower() in ['true', 'false']:
|
86
|
-
return True
|
87
|
-
return False
|
88
|
-
|
89
|
-
@staticmethod
|
90
|
-
def is_undefined(string: TnStr) -> bool:
|
91
|
-
""" check if string is undefined (None or empty)
|
92
|
-
"""
|
93
|
-
if string is None or string == '':
|
94
|
-
return True
|
95
|
-
return False
|
96
|
-
|
97
|
-
@staticmethod
|
98
|
-
def nvl(string: TnStr) -> TnStr:
|
99
|
-
""" nvl function similar to SQL NVL function
|
100
|
-
"""
|
101
|
-
if string is None:
|
102
|
-
return ''
|
103
|
-
return string
|
104
|
-
|
105
|
-
@staticmethod
|
106
|
-
def strip_n(string: str) -> str:
|
107
|
-
""" Replace new line characters by Blanks and strip Blanks
|
108
|
-
"""
|
109
|
-
return string.replace('\n', ' ').strip()
|
110
|
-
|
111
|
-
@staticmethod
|
112
|
-
def remove(string: str, a_to_remove: TyArr) -> str:
|
113
|
-
""" remove all character of a list
|
114
|
-
"""
|
115
|
-
for to_remove in a_to_remove:
|
116
|
-
string = string.replace(to_remove, '')
|
117
|
-
return string
|
118
|
-
|
119
|
-
@staticmethod
|
120
|
-
def sh_boolean(string: str) -> bool:
|
121
|
-
""" Show string as boolean if string is a boolean
|
122
|
-
"""
|
123
|
-
match string.lower():
|
124
|
-
case 'true':
|
125
|
-
return True
|
126
|
-
case 'false':
|
127
|
-
return False
|
128
|
-
case _:
|
129
|
-
raise ValueError
|
130
|
-
|
131
|
-
@staticmethod
|
132
|
-
def sh_float(string: str) -> TnFloat:
|
133
|
-
""" Returns Float if string is of Type Float
|
134
|
-
otherwise None
|
135
|
-
"""
|
136
|
-
try:
|
137
|
-
return float(string)
|
138
|
-
except Exception:
|
139
|
-
print(traceback.format_exc())
|
140
|
-
return None
|
141
|
-
|
142
|
-
@staticmethod
|
143
|
-
def sh_int(string: str) -> TnInt:
|
144
|
-
""" Returns Int if string is of Type Int
|
145
|
-
otherwise None
|
146
|
-
"""
|
147
|
-
try:
|
148
|
-
return int(string)
|
149
|
-
except ValueError:
|
150
|
-
return None
|
151
|
-
|
152
|
-
@staticmethod
|
153
|
-
def sh_dic(string: str, sw_decimal=False) -> Any:
|
154
|
-
""" Returns Dic if string is of Type Json-String
|
155
|
-
otherwise None
|
156
|
-
"""
|
157
|
-
try:
|
158
|
-
if sw_decimal:
|
159
|
-
return simplejson.loads(string, use_decimal=True)
|
160
|
-
else:
|
161
|
-
return orjson.loads(string)
|
162
|
-
except Exception:
|
163
|
-
print(traceback.format_exc())
|
164
|
-
return None
|
165
|
-
|
166
|
-
@staticmethod
|
167
|
-
def sh_arr(string: str) -> Any:
|
168
|
-
""" Show valid Array string as Array
|
169
|
-
"""
|
170
|
-
try:
|
171
|
-
return orjson.loads(string)
|
172
|
-
except Exception:
|
173
|
-
print(traceback.format_exc())
|
174
|
-
return None
|
175
|
-
|
176
|
-
@staticmethod
|
177
|
-
def sh_aoa(string: str) -> Any:
|
178
|
-
""" Show valid Array string as Array
|
179
|
-
"""
|
180
|
-
try:
|
181
|
-
return orjson.loads(string)
|
182
|
-
except Exception:
|
183
|
-
print(traceback.format_exc())
|
184
|
-
return None
|
185
|
-
|
186
|
-
@staticmethod
|
187
|
-
def sh_first_item(string: str) -> Any:
|
188
|
-
""" Show first substring of string
|
189
|
-
"""
|
190
|
-
return string.split()[0]
|
191
|
-
|
192
|
-
@classmethod
|
193
|
-
def sh_a_int(cls, string: str, sep: str) -> TyArr:
|
194
|
-
""" Show first substring of string
|
195
|
-
"""
|
196
|
-
# arr = string.split(sep)
|
197
|
-
arr = re.split(sep, string)
|
198
|
-
arr_new = []
|
199
|
-
for item in arr:
|
200
|
-
_item = item.strip()
|
201
|
-
if not isinstance(_item, str):
|
202
|
-
continue
|
203
|
-
if not _item.isdigit():
|
204
|
-
continue
|
205
|
-
arr_new.append(cls.sh_int(_item))
|
206
|
-
return arr_new
|
207
|
-
|
208
|
-
@classmethod
|
209
|
-
def sh_a_str(
|
210
|
-
cls, string: str, sep: str, a_exclude: TnArr = None) -> Any:
|
211
|
-
""" Show first substring of string
|
212
|
-
"""
|
213
|
-
# arr = string.split(sep)
|
214
|
-
arr = re.split(sep, string)
|
215
|
-
|
216
|
-
if a_exclude is None:
|
217
|
-
_arr = arr
|
218
|
-
else:
|
219
|
-
_arr = []
|
220
|
-
for item in arr:
|
221
|
-
_item = item.strip()
|
222
|
-
if _item not in a_exclude:
|
223
|
-
_arr.append(_item)
|
224
|
-
|
225
|
-
arr_new = []
|
226
|
-
for item in _arr:
|
227
|
-
if isinstance(item, str):
|
228
|
-
arr_new.append(item.strip())
|
229
|
-
elif isinstance(item, int):
|
230
|
-
arr_new.append(str(item))
|
231
|
-
else:
|
232
|
-
arr_new.append(item)
|
233
|
-
|
234
|
-
return arr_new
|
235
|
-
|
236
|
-
@classmethod
|
237
|
-
def sh_a_obj(
|
238
|
-
cls, string: str, sep: str, a_exclude: TnArr = None) -> Any:
|
239
|
-
""" Show first substring of string
|
240
|
-
"""
|
241
|
-
# arr = string.split(sep)
|
242
|
-
arr = re.split(sep, string)
|
243
|
-
|
244
|
-
if a_exclude is None:
|
245
|
-
_arr = arr
|
246
|
-
else:
|
247
|
-
_arr = []
|
248
|
-
for item in arr:
|
249
|
-
_item = item.strip()
|
250
|
-
if _item not in a_exclude:
|
251
|
-
_arr.append(_item)
|
252
|
-
|
253
|
-
arr_new: TyArr = []
|
254
|
-
for item in _arr:
|
255
|
-
if isinstance(item, str):
|
256
|
-
_item = item.strip()
|
257
|
-
if _item.isdigit():
|
258
|
-
_item = cls.sh_int(_item)
|
259
|
-
arr_new.append(_item)
|
260
|
-
else:
|
261
|
-
arr_new.append(_item)
|
262
|
-
else:
|
263
|
-
arr_new.append(item)
|
264
|
-
|
265
|
-
return arr_new
|