forked from franciszac-dlc/CAPRI-FAIR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
60 lines (53 loc) · 1.91 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import logging
import operator
import datetime
from typing_extensions import Literal
logLevelType = Literal["info", "warn", "error"]
operators = {"Sum": operator.add,
"Product": operator.mul, "WeightedSum": operator.add}
def logger(message: str, logLevel: logLevelType = "info", noConsolePrint: bool = False):
"""
Generates logs for the system in both command line and logger file
Parameters
----------
message: str
A message to be shown in both logger file and command line
example: "My Sample Message"
logLevel: Literal, optional (default to "info)
The level of logs. Possible values are "info", "warn", and "error"
example: "warn"
noConsolePrint: bool, optional (default to False)
If True, the log will only be printed in the logger file
example: True
"""
# Create a console log by default
if (not noConsolePrint):
print(message)
# Create a log in the log file
currentMoment = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
printMessage = f'[{currentMoment}] {message}'
if (logLevel is 'warn'):
logging.warn(printMessage)
elif (logLevel is 'error'):
logging.error(printMessage)
else:
logging.info(printMessage)
def textToOperator(operator: str, operands: list, weights: list | None = None):
"""
Converts a pre-defined text into operator
Parameters
----------
operator: str
A message to be shown in both logger file and command line
example: "Sum"
"""
if operator != 'WeightedSum':
weights = [1] * len(operands)
else:
if weights is None:
raise ValueError("Weights missing")
# Final result initialized by effect-less value
result = 1 if operator == 'Product' else 0
for weight, operand in zip(weights, operands):
result = operators[operator](result, weight * operand)
return result