aletk 0.1.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.
aletk/ResultMonad.py ADDED
@@ -0,0 +1,279 @@
1
+ from logging import Logger
2
+ from typing import Any, Callable, NamedTuple
3
+ from functools import wraps
4
+
5
+
6
+
7
+ class Ok[T](NamedTuple):
8
+ """
9
+ Model for the success case of a function in this project.
10
+
11
+ Attributes
12
+ ----------
13
+ out : T
14
+ The data returned by the function.
15
+ """
16
+
17
+ out: T
18
+
19
+
20
+ class Err(NamedTuple):
21
+ """
22
+ Model for the error case of a function in this project.
23
+
24
+ Attributes
25
+ ----------
26
+ message : str
27
+ A message describing the error.
28
+ code : int
29
+ A code that can be used to handle different error cases.
30
+ """
31
+
32
+ message: str
33
+ code: int
34
+
35
+
36
+ type TResult[T] = Ok[T] | Err
37
+
38
+
39
+ def rmap[T, U](f: Callable[[T], U], result: TResult[T]) -> TResult[U]:
40
+ """
41
+ Map a function over a Result.
42
+
43
+ Parameters
44
+ ----------
45
+ f : Callable[[T], U]
46
+ The function to map over the Result.
47
+ result : Result[T]
48
+ The Result to map the function over.
49
+
50
+ Returns
51
+ -------
52
+ Result[U]
53
+ The Result with the function mapped over it.
54
+ """
55
+
56
+ match result:
57
+ case Ok(out=data):
58
+ return Ok(out=f(data))
59
+ case Err(message=msg, code=code):
60
+ return Err(message=msg, code=code)
61
+ case _:
62
+ raise ValueError("Unknown Result type.")
63
+
64
+
65
+ def rbind[T, U](f: Callable[[T], TResult[U]], result: TResult[T]) -> TResult[U]:
66
+ """
67
+ "result bind". Binds a function to a Result.
68
+
69
+ Parameters
70
+ ----------
71
+ f : Callable[[T], Result[E]]
72
+ The function to bind to the Result.
73
+ result : Result[T]
74
+ The Result to bind the function to.
75
+
76
+ Returns
77
+ -------
78
+ Result[E]
79
+ The Result with the function bound to it.
80
+ """
81
+
82
+ match result:
83
+ case Ok(out=data):
84
+ return f(data)
85
+ case Err(message=msg, code=code):
86
+ return Err(message=msg, code=code)
87
+ case _:
88
+ raise ValueError("Unknown Result type.")
89
+
90
+
91
+ def runwrap[T](result: TResult[T]) -> T:
92
+ """
93
+ Unwrap a Result.
94
+
95
+ Parameters
96
+ ----------
97
+ result : Result[T]
98
+ The Result to unwrap.
99
+
100
+ Returns
101
+ -------
102
+ T
103
+ The data inside the Result.
104
+
105
+ Raises
106
+ ------
107
+ ValueError
108
+ If the Result is an Err.
109
+ """
110
+
111
+ match result:
112
+ case Ok(out=data):
113
+ return data
114
+ case Err(message=msg, code=code):
115
+ raise ValueError(f"Error: {msg}")
116
+
117
+
118
+ def runwrap_or[T](result: TResult[T], default: T) -> T:
119
+ """
120
+ Unwrap a Result, returning a default value if the Result is an Err.
121
+
122
+ Parameters
123
+ ----------
124
+ result : Result[T]
125
+ The Result to unwrap.
126
+ default : T
127
+ The default value to return if the Result is an Err.
128
+
129
+ Returns
130
+ -------
131
+ T
132
+ The data inside the Result, or the default value if the Result is an Err.
133
+ """
134
+
135
+ match result:
136
+ case Ok(out=data):
137
+ return data
138
+ case Err(message=msg, code=code):
139
+ return default
140
+
141
+
142
+ def try_except_wrapper[T](logger: Logger) -> Callable[[Callable[..., T]], Callable[..., TResult[T]]]:
143
+ """
144
+ Decorator that wraps a function in a try-except block, logging any errors that occur. The wrapped function will then always return a Ok[T] or Err as output.
145
+
146
+ """
147
+
148
+ def decorator(func: Callable[..., T]) -> Callable[..., TResult[T]]:
149
+ @wraps(func)
150
+ def wrapper(*args: Any, **kwargs: Any) -> TResult[T]:
151
+ try:
152
+ # logger.debug(f"Calling function '{func.__name__}' with args: {args} and kwargs: {kwargs}")
153
+ result = func(*args, **kwargs)
154
+
155
+ match result:
156
+ case Err(message=msg, code=code):
157
+ return result
158
+ case Ok():
159
+ # logger.debug(f"Function '{func.__name__}' executed successfully.")
160
+ return result
161
+ case _:
162
+ # logger.debug(f"Function '{func.__name__}' executed successfully.")
163
+ return Ok[T](out=result)
164
+
165
+ except Exception as e:
166
+ error_message_logger = f"An error occurred in function '{func.__name__}'. Detail:\n{e}"
167
+ logger.error(error_message_logger)
168
+
169
+ error_message_debug = f"The function that triggered the error was '{func.__name__}', called with... \n\t\targs: '[ {args} ]\n\t\tkwargs: [ {kwargs} ]'"
170
+ logger.debug(error_message_debug)
171
+
172
+ error_message_return = f"An error occurred in function '{func.__name__}'. Detail: {e}"
173
+
174
+ return Err(message=error_message_return, code=-1)
175
+
176
+ return wrapper
177
+
178
+ return decorator
179
+
180
+
181
+ def runwrap_soft[T](result: TResult[T]) -> T | Err:
182
+ """
183
+ Unwrap a Result.
184
+
185
+ Parameters
186
+ ----------
187
+ result : Result[T]
188
+ The Result to unwrap.
189
+
190
+ Returns
191
+ -------
192
+ T
193
+ The data inside the Result.
194
+
195
+ Raises
196
+ ------
197
+ ValueError
198
+ If the Result is an Err.
199
+ """
200
+
201
+ match result:
202
+ case Ok(out=data):
203
+ return data
204
+ case Err(message=msg, code=code):
205
+ return Err(message=msg, code=code)
206
+
207
+
208
+ def funwrap[T](func: Callable[..., TResult[T]]) -> Callable[..., T | Err]:
209
+ """
210
+ Wrap a function that returns a Result in a function that returns the data inside the Result.
211
+ """
212
+
213
+ def wrapper(*args: Any, **kwargs: Any) -> T | Err:
214
+ result = func(*args, **kwargs)
215
+ match result:
216
+ case Ok(out=data):
217
+ return data
218
+ case Err(message=msg, code=code):
219
+ return Err(message=msg, code=code)
220
+ case _:
221
+ raise ValueError(f"Function '{func.__name__}' returned an unknown Result type, with value '{result}'.")
222
+
223
+ return wrapper
224
+
225
+
226
+ def main_try_except_wrapper[T](logger: Logger) -> Callable[[Callable[..., T]], Callable[..., TResult[T]]]:
227
+ """
228
+ Decorator that wraps a function in a try-except block, logging any errors that occur. The wrapped function will then always return a Ok[T] or Err as output.
229
+ """
230
+
231
+ def decorator(func: Callable[..., T]) -> Callable[..., TResult[T]]:
232
+ @wraps(func)
233
+ def wrapper(*args: Any, **kwargs: Any) -> TResult[T]:
234
+ try:
235
+ result = func(*args, **kwargs)
236
+
237
+ match result:
238
+ case Err(message=msg, code=code):
239
+ return result
240
+ case Ok():
241
+ return result
242
+ case _:
243
+ return Ok(out=result)
244
+
245
+ except Exception as e:
246
+ error_message_logger = f"'{func.__name__}':\n{e}"
247
+ logger.error(error_message_logger)
248
+
249
+ error_message_return = f"'{func.__name__}': {e}"
250
+ return Err(message=error_message_return, code=-1)
251
+
252
+ return wrapper
253
+
254
+ return decorator
255
+
256
+
257
+ def light_error_handler[T](debug: bool = False) -> Callable[[Callable[..., T]], Callable[..., T]]:
258
+ """
259
+ Decorator that wraps a function in a try-except block, and returns a better error message if an exception is raised.
260
+ """
261
+
262
+ def decorator(func: Callable[..., T]) -> Callable[..., T]:
263
+ @wraps(func)
264
+ def wrapper(*args: Any, **kwargs: Any) -> T:
265
+ try:
266
+ return func(*args, **kwargs)
267
+
268
+ except Exception as e:
269
+ if not debug:
270
+ error_message = f"An error occurred in function '{func.__name__}'. {e.__class__.__name__}: {e}"
271
+
272
+ else:
273
+ error_message = f"An error occured in function '{func.__name__}', called with...\n\targs: [[ {args} ]]\n\tkwargs: [[ {kwargs} ]]\n\n{e.__class__.__name__}: {e}"
274
+
275
+ raise Exception(error_message)
276
+
277
+ return wrapper
278
+
279
+ return decorator
aletk/__init__.py ADDED
File without changes
aletk/adapters.py ADDED
@@ -0,0 +1,29 @@
1
+ from typing import Callable, Generator, Iterator
2
+
3
+
4
+
5
+ class ReusableGenerator[T]():
6
+ """
7
+ Adapter class to allow consuming a generator more than once. Usage example:
8
+
9
+ ```python
10
+ def my_generator() -> Generator[int, None, None]:
11
+ yield 1
12
+ yield 2
13
+ yield 3
14
+
15
+ adapter = GeneratorAdapter(my_generator)
16
+
17
+ for item in adapter:
18
+ print(item)
19
+
20
+ for item in adapter:
21
+ print(item)
22
+ ```
23
+ """
24
+
25
+ def __init__(self, iterator_factory: Callable[[], Generator[T, None, None]]) -> None:
26
+ self.iterator_factory = iterator_factory
27
+
28
+ def __iter__(self) -> Iterator[T]:
29
+ return self.iterator_factory()
aletk/utils.py ADDED
@@ -0,0 +1,125 @@
1
+ from datetime import datetime
2
+ import logging
3
+ from os import getenv
4
+ from typing import FrozenSet
5
+ from fuzzywuzzy import fuzz
6
+
7
+ from .ResultMonad import Err
8
+
9
+
10
+ def get_logger(name: str) -> logging.Logger:
11
+ """
12
+ Returns a logger object. Pass the name of the logger as an argument.
13
+
14
+ You can also pass the environment variables 'LOGGING_LEVEL' or 'LOG_LEVEL' to set the logging level. If none is passed, the default is 'INFO'.
15
+ """
16
+
17
+ if name is None or not isinstance(name, str):
18
+ raise ValueError("Utils::GetLogger::Argument 'name' has to be a non None string.")
19
+
20
+ logging_level = getenv("LOGGING_LEVEL", "")
21
+
22
+ if not logging_level:
23
+ logging_level = getenv("LOG_LEVEL", "INFO")
24
+
25
+ logger = logging.getLogger(name)
26
+ logger.setLevel(logging_level)
27
+
28
+ if not logger.handlers:
29
+ handler = logging.StreamHandler()
30
+ handler.setLevel(logging_level)
31
+ datefmt = "%Y-%m-%d %H:%M:%S"
32
+ formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt)
33
+ handler.setFormatter(formatter)
34
+ logger.addHandler(handler)
35
+
36
+ return logger
37
+
38
+
39
+ def remove_extra_whitespace(string: str) -> str:
40
+ """
41
+ Remove extra whitespace from a string. This version removes all newlines, tabs, carriage returns, trailing and leading whitespace, and multiple spaces in a row.
42
+
43
+ If the input is None or not a string, a ValueError is raised.
44
+ """
45
+ if string is None or not isinstance(string, str):
46
+ raise ValueError("Utils::RemoveExtraWhiteSpace::Argument string has to be a (non None) string.")
47
+
48
+ cleaned_string = " ".join(string.split()).strip()
49
+
50
+ return cleaned_string
51
+
52
+
53
+ def get_timestamp() -> str:
54
+ """
55
+ Returns a string timestamp in the format YYYYMMDD_HHMMSS.
56
+ """
57
+ dt = datetime.now().strftime("%Y%m%d_%H%M%S")
58
+
59
+ return dt
60
+
61
+
62
+ def fuzzy_match_score(
63
+ str1: str,
64
+ str2: str,
65
+ ) -> int:
66
+ """
67
+ Returns a fuzzy match score between two strings.
68
+
69
+ If any of the inputs is None or not a string, a ValueError is raised.
70
+ """
71
+
72
+ if str1 is None or str2 is None or not isinstance(str1, str) or not isinstance(str2, str):
73
+ raise ValueError(f"Utils::FuzzyMatchScore::Arguments have to be non None strings. Got:\n'{str1}'\n'{str2}'")
74
+
75
+ score = fuzz.token_sort_ratio(str1, str2)
76
+
77
+ if not isinstance(score, int):
78
+ raise ValueError(
79
+ f"Utils::FuzzyMatchScore::The score returned by the fuzzywuzzy library is not an integer. Got: {score}"
80
+ )
81
+
82
+ return score
83
+
84
+
85
+ def lginf(frame: str, msg: str, logger: logging.Logger) -> None:
86
+ """
87
+ Log an info message in a standard format.
88
+ """
89
+ logger.info(f"{frame}\n\t{msg}")
90
+
91
+
92
+ def handle_error(frame: str, msg: str, logger: logging.Logger, code: int) -> Err:
93
+ """
94
+ Handle errors. This function returns an Err object with the message and code.
95
+ """
96
+ message = f"{frame}\n\t{msg}"
97
+ logger.error(message)
98
+ return Err(message=message, code=code)
99
+
100
+
101
+ def handle_unexpected_exception(msg: str, logger: logging.Logger, code: int = -1) -> Err:
102
+ """
103
+ Handle unexpected exceptions. This function logs the message and returns an Err object with the message and code.
104
+ """
105
+ message = f"Unexpected exception!\n{msg}"
106
+ logger.error(message)
107
+ return Err(message=message, code=code)
108
+
109
+
110
+ def pretty_format_frozenset(fs: FrozenSet[object]) -> str:
111
+ """
112
+ Pretty format a FrozenSet object, to be used in logging or printing.
113
+ """
114
+ if fs is None or fs == frozenset():
115
+ return ""
116
+ return ", ".join(sorted([f"{item}" for item in fs]))
117
+
118
+
119
+ def dump_frozenset(fs: FrozenSet[object]) -> list[str]:
120
+ """
121
+ Dump a FrozenSet object to a list of strings.
122
+ """
123
+ if fs is None or fs == frozenset():
124
+ return []
125
+ return sorted([f"{item}" for item in fs])
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Luis Alejandro Bordo García
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.1
2
+ Name: aletk
3
+ Version: 0.1.0
4
+ Classifier: Programming Language :: Python :: 3
5
+ Classifier: License :: OSI Approved :: MIT License
6
+ Classifier: Operating System :: OS Independent
7
+ Requires-Python: >=3.13
8
+ License-File: LICENSE
9
+ Requires-Dist: fuzzywuzzy
10
+ Requires-Dist: python-Levenshtein
11
+ Provides-Extra: dev
12
+ Requires-Dist: mypy; extra == "dev"
13
+ Requires-Dist: black; extra == "dev"
14
+ Requires-Dist: pytest; extra == "dev"
15
+ Requires-Dist: jupyter; extra == "dev"
@@ -0,0 +1,9 @@
1
+ aletk/ResultMonad.py,sha256=XPK1Ckg0L9u4iTJjUqHQIYT_RFdBlL47zm33GrGApG0,7732
2
+ aletk/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ aletk/adapters.py,sha256=xahzDbinDpxaomP-gzFZ5CcasFTD1CkjP2H8Ly7bM1M,660
4
+ aletk/utils.py,sha256=nvG-fRt4qIr-hBOFcpLN27mC0EEp877k3sf72O1rBE0,3800
5
+ aletk-0.1.0.dist-info/LICENSE,sha256=hZFMbARrXo4zfuo80-Sg3-FT5_ceiSDf0QpjgJoUdGc,1085
6
+ aletk-0.1.0.dist-info/METADATA,sha256=y5UVAZ4EQyZsuzqk4zkjqyKBM58WzdceYtD55Amf62Y,471
7
+ aletk-0.1.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
8
+ aletk-0.1.0.dist-info/top_level.txt,sha256=LYDb8QWbh4W4hIkF0TVg5ITKEsaj-pb_xcYQRGzLqUU,6
9
+ aletk-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.6.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ aletk