-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
224 lines (190 loc) · 7.14 KB
/
Copy pathutils.py
File metadata and controls
224 lines (190 loc) · 7.14 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# === utils.py ===
import logging
logger = logging.getLogger(__name__)
import time
import os
import sys
import pickle
from datetime import datetime
import importlib
import importlib.util
import json
from residue import Residue
class ResidueError(Exception):
def __init__(self, *args):
super().__init__(*args)
class TimeCount:
"""
A simple timer class to track the elapsed time from initialization or from the last reset.
"""
name = 'timecount'
def __init__(self) -> None:
"""
Initializes the timer with the current time as the starting point.
"""
self.ini_time = time.time()
def restart(self) -> None:
"""
Resets the starting point of the timer to the current time.
"""
self.ini_time = time.time()
def calc_time(self, format: str = "full") -> str:
"""
Calculates the elapsed time since the timer started or was last reset.
Parameters:
format (str): The format of the output time. Options:
- "s": seconds
- "m": minutes
- "h": hours
- "full": full breakdown as "0 h 0 m 0 s"
Returns:
str: The elapsed time in the specified format.
Raises:
ValueError: If an unsupported format string is provided.
"""
elapsed = time.time() - self.ini_time
if format == "s":
return f"{int(elapsed)} s"
elif format == "m":
return f"{int(elapsed / 60)} m"
elif format == "h":
return f"{int(elapsed / 3600)} h"
elif format == "full":
hours = int(elapsed // 3600)
minutes = int((elapsed % 3600) // 60)
seconds = int(elapsed % 60)
return f"{hours} h {minutes} m {seconds} s"
else:
raise ValueError("Invalid format. Choose 's', 'm', 'h', or 'full'.")
# self-explanatory
def current_time():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def exists(filename: str) -> bool:
"""
Checks whether a file exists in the current working directory.
Parameters:
filename (str): Name of the file to check.
Returns:
bool: True if the file exists, False otherwise.
"""
full_path = os.path.join(os.getcwd(), filename)
file_exists = os.path.isfile(full_path)
logger.debug(f'Does "{filename}" exists? --> {file_exists}')
return file_exists
def save_pkl(object, outfile: str) -> None:
with open(outfile, 'wb') as fo:
logger.debug(f'Writing {outfile} . . .')
pickle.dump(object, fo)
fo.flush() # Vacía el búfer del intérprete
os.fsync(fo.fileno()) # Fuerza escritura física a disco
def read_pkl(infile):
with open(infile, 'rb') as fi:
logger.info(f'Reading {infile} . . .')
return pickle.load(fi)
def save_json(object, outfile: str) -> None:
import numpy as np
# transform object into a dictionary
dictionary = object
if not isinstance(dictionary, dict):
dictionary = object.__dict__
# remove np.arrays, tuples and sets; also Residue class
dictionary = {k: v for k, v in dictionary.items() if not isinstance(dictionary[k], (np.ndarray, tuple, set, Residue))}
# Check for lists where all elements are instances of Residue and remove them
for k, v in list(dictionary.items()):
if isinstance(v, list) and any(isinstance(i, Residue) for i in v):
del dictionary[k]
# print(dictionary, type(dictionary), '************')
# save as json file
with open(outfile, 'w') as jsonfile:
logger.info(f'Writing {outfile} . . .')
json.dump(dictionary, jsonfile, indent=4)
def read_json(infile):
with open(infile, 'r') as fi:
logger.info(f'Reading {infile} . . .')
data = json.load(fi)
return data
def dynamic_import(module_name: str):
"""
Dynamically imports a module from:
1. PYTHONPATH
2. The main script's directory
3. The current working directory
:param module_name: Name of the module (without '.py')
:return: Imported module object
:raises ImportError: If the module cannot be located
"""
if not module_name:
raise ImportError("No module name was provided.")
try:
return importlib.import_module(module_name)
except ModuleNotFoundError:
# Try other directories
main_dir = os.path.dirname(os.path.abspath(sys.modules['__main__'].__file__))
# Include here known directories where module can be found
candidate_paths = [os.getcwd(), main_dir]
for path in candidate_paths:
module_file = os.path.join(path, module_name + ".py")
if os.path.exists(module_file):
if path not in sys.path:
sys.path.insert(0, path)
spec = importlib.util.spec_from_file_location(module_name, module_file)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
raise ImportError(
f"Module '{module_name}' not found in any tested directory."
)
def get_module_function(module_name: str, function_name: str, critical: bool = False):
"""
Imports a module and returns a specified function or attribute.
:param module_name: Name of the module (without '.py')
:param function_name: Name of the function or attribute to retrieve
:param critical: If True, raises error on failure. Otherwise returns None.
:return: Callable or object from the module
:raises ImportError or AttributeError
"""
try:
module = dynamic_import(module_name)
except ImportError as e:
msg = f"Module '{module_name}' could not be imported: {e}"
if critical:
logger.error(msg)
raise
else:
logger.warning(msg)
return None
if not hasattr(module, function_name):
msg = f"Function '{function_name}' not found in module '{module_name}'"
if critical:
logger.error(msg)
raise AttributeError(msg)
else:
logger.warning(msg)
return None
return getattr(module, function_name)
def change_directory(path: str) -> None:
"""
Change the current working directory to the specified path.
Parameters:
path (str): The target directory path.
Raises:
FileNotFoundError: If the path does not exist.
"""
if not os.path.exists(path):
logger.error(f"Directory '{path}' does not exist.")
raise FileNotFoundError(f"Directory '{path}' does not exist.")
os.chdir(path)
logger.info(f"Changed working directory to: {path}")
def int_to_roman(n: int) -> str:
"""Converts a positive integer to its Roman numeral representation."""
values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
symbols = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
result = ''
for v, s in zip(values, symbols):
while n >= v:
result += s
n -= v
return result
if __name__ == '__main__':
pass