abstract-utilities 0.2.2.496__py3-none-any.whl → 0.2.2.504__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.
- abstract_utilities/__init__.py +5 -9
- abstract_utilities/class_utils/__init__.py +7 -0
- abstract_utilities/class_utils/abstract_classes.py +74 -0
- abstract_utilities/class_utils/caller_utils.py +35 -0
- abstract_utilities/class_utils/class_utils.py +109 -0
- abstract_utilities/class_utils/function_utils.py +153 -0
- abstract_utilities/class_utils/global_utils.py +56 -0
- abstract_utilities/class_utils/imports/__init__.py +2 -0
- abstract_utilities/class_utils/imports/imports.py +2 -0
- abstract_utilities/class_utils/imports/utils.py +40 -0
- abstract_utilities/class_utils/module_utils.py +63 -0
- abstract_utilities/env_utils/imports/imports.py +3 -2
- abstract_utilities/error_utils/__init__.py +2 -0
- abstract_utilities/error_utils/error_utils.py +25 -0
- abstract_utilities/error_utils/imports/__init__.py +2 -0
- abstract_utilities/error_utils/imports/imports.py +1 -0
- abstract_utilities/error_utils/imports/module_imports.py +1 -0
- abstract_utilities/file_utils/imports/imports.py +3 -18
- abstract_utilities/file_utils/imports/module_imports.py +3 -6
- abstract_utilities/file_utils/src/type_checks.py +0 -1
- abstract_utilities/hash_utils/__init__.py +2 -0
- abstract_utilities/hash_utils/hash_utils.py +5 -0
- abstract_utilities/hash_utils/imports/__init__.py +2 -0
- abstract_utilities/hash_utils/imports/imports.py +1 -0
- abstract_utilities/hash_utils/imports/module_imports.py +0 -0
- abstract_utilities/history_utils/__init__.py +2 -0
- abstract_utilities/history_utils/history_utils.py +37 -0
- abstract_utilities/history_utils/imports/__init__.py +2 -0
- abstract_utilities/history_utils/imports/imports.py +1 -0
- abstract_utilities/history_utils/imports/module_imports.py +0 -0
- abstract_utilities/import_utils/imports/imports.py +1 -1
- abstract_utilities/import_utils/imports/module_imports.py +1 -1
- abstract_utilities/import_utils/src/__init__.py +1 -1
- abstract_utilities/import_utils/src/clean_imports.py +31 -5
- abstract_utilities/import_utils/src/dot_utils.py +9 -0
- abstract_utilities/import_utils/src/package_utilss/__init__.py +139 -0
- abstract_utilities/import_utils/src/package_utilss/context_utils.py +27 -0
- abstract_utilities/import_utils/src/package_utilss/import_collectors.py +53 -0
- abstract_utilities/import_utils/src/package_utilss/path_utils.py +28 -0
- abstract_utilities/import_utils/src/package_utilss/safe_import.py +27 -0
- abstract_utilities/import_utils/src/pkg_utils.py +140 -0
- abstract_utilities/imports.py +18 -0
- abstract_utilities/json_utils/__init__.py +2 -0
- abstract_utilities/json_utils/imports/__init__.py +2 -0
- abstract_utilities/json_utils/imports/imports.py +2 -0
- abstract_utilities/json_utils/imports/module_imports.py +5 -0
- abstract_utilities/json_utils/json_utils.py +743 -0
- abstract_utilities/list_utils/__init__.py +2 -0
- abstract_utilities/list_utils/imports/__init__.py +2 -0
- abstract_utilities/list_utils/imports/imports.py +1 -0
- abstract_utilities/list_utils/imports/module_imports.py +0 -0
- abstract_utilities/list_utils/list_utils.py +199 -0
- abstract_utilities/log_utils/__init__.py +5 -0
- abstract_utilities/log_utils/abstractLogManager.py +64 -0
- abstract_utilities/log_utils/call_response.py +68 -0
- abstract_utilities/log_utils/imports/__init__.py +2 -0
- abstract_utilities/log_utils/imports/imports.py +7 -0
- abstract_utilities/log_utils/imports/module_imports.py +2 -0
- abstract_utilities/log_utils/log_file.py +56 -0
- abstract_utilities/log_utils/logger_callable.py +49 -0
- abstract_utilities/math_utils/__init__.py +2 -0
- abstract_utilities/math_utils/imports/__init__.py +2 -0
- abstract_utilities/math_utils/imports/imports.py +2 -0
- abstract_utilities/math_utils/imports/module_imports.py +1 -0
- abstract_utilities/math_utils/math_utils.py +208 -0
- abstract_utilities/parse_utils/__init__.py +2 -0
- abstract_utilities/parse_utils/imports/__init__.py +3 -0
- abstract_utilities/parse_utils/imports/constants.py +10 -0
- abstract_utilities/parse_utils/imports/imports.py +2 -0
- abstract_utilities/parse_utils/imports/module_imports.py +4 -0
- abstract_utilities/parse_utils/parse_utils.py +516 -0
- abstract_utilities/path_utils/__init__.py +2 -0
- abstract_utilities/path_utils/imports/__init__.py +2 -0
- abstract_utilities/path_utils/imports/imports.py +1 -0
- abstract_utilities/path_utils/imports/module_imports.py +6 -0
- abstract_utilities/path_utils/path_utils.py +715 -0
- abstract_utilities/path_utils.py +94 -2
- abstract_utilities/read_write_utils/__init__.py +1 -0
- abstract_utilities/read_write_utils/imports/__init__.py +2 -0
- abstract_utilities/read_write_utils/imports/imports.py +2 -0
- abstract_utilities/read_write_utils/imports/module_imports.py +5 -0
- abstract_utilities/read_write_utils/read_write_utils.py +338 -0
- abstract_utilities/read_write_utils.py +2 -4
- abstract_utilities/safe_utils/__init__.py +2 -0
- abstract_utilities/safe_utils/imports/__init__.py +3 -0
- abstract_utilities/safe_utils/imports/imports.py +1 -0
- abstract_utilities/safe_utils/imports/module_imports.py +2 -0
- abstract_utilities/safe_utils/safe_utils.py +130 -0
- abstract_utilities/ssh_utils/__init__.py +2 -1
- abstract_utilities/ssh_utils/classes.py +0 -1
- abstract_utilities/ssh_utils/cmd_utils.py +207 -0
- abstract_utilities/ssh_utils/imports/__init__.py +3 -0
- abstract_utilities/ssh_utils/imports/imports.py +5 -0
- abstract_utilities/ssh_utils/imports/module_imports.py +5 -0
- abstract_utilities/ssh_utils/imports/utils.py +189 -0
- abstract_utilities/ssh_utils/pexpect_utils.py +11 -18
- abstract_utilities/string_utils/__init__.py +4 -0
- abstract_utilities/string_utils/clean_utils.py +28 -0
- abstract_utilities/string_utils/eat_utils.py +103 -0
- abstract_utilities/string_utils/imports/__init__.py +3 -0
- abstract_utilities/string_utils/imports/imports.py +2 -0
- abstract_utilities/string_utils/imports/module_imports.py +2 -0
- abstract_utilities/string_utils/imports/utils.py +81 -0
- abstract_utilities/string_utils/replace_utils.py +27 -0
- abstract_utilities/thread_utils/__init__.py +2 -0
- abstract_utilities/thread_utils/imports/__init__.py +2 -0
- abstract_utilities/thread_utils/imports/imports.py +2 -0
- abstract_utilities/thread_utils/imports/module_imports.py +2 -0
- abstract_utilities/thread_utils/thread_utils.py +140 -0
- abstract_utilities/time_utils/__init__.py +2 -0
- abstract_utilities/time_utils/imports/__init__.py +2 -0
- abstract_utilities/time_utils/imports/imports.py +3 -0
- abstract_utilities/time_utils/imports/module_imports.py +1 -0
- abstract_utilities/time_utils/time_utils.py +392 -0
- abstract_utilities/type_utils/__init__.py +3 -0
- abstract_utilities/type_utils/alpha_utils.py +59 -0
- abstract_utilities/type_utils/imports/__init__.py +2 -0
- abstract_utilities/type_utils/imports/imports.py +4 -0
- abstract_utilities/type_utils/imports/module_imports.py +1 -0
- abstract_utilities/type_utils/num_utils.py +19 -0
- abstract_utilities/type_utils/type_utils.py +981 -0
- {abstract_utilities-0.2.2.496.dist-info → abstract_utilities-0.2.2.504.dist-info}/METADATA +1 -1
- abstract_utilities-0.2.2.504.dist-info/RECORD +229 -0
- abstract_utilities-0.2.2.496.dist-info/RECORD +0 -123
- {abstract_utilities-0.2.2.496.dist-info → abstract_utilities-0.2.2.504.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.496.dist-info → abstract_utilities-0.2.2.504.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
def quoteIt(st: str, ls: list) -> str:
|
|
2
|
+
"""
|
|
3
|
+
Quotes specific elements in a string.
|
|
4
|
+
|
|
5
|
+
Args:
|
|
6
|
+
st (str): The input string.
|
|
7
|
+
ls (list): The list of elements to quote.
|
|
8
|
+
|
|
9
|
+
Returns:
|
|
10
|
+
str: The modified string with quoted elements.
|
|
11
|
+
"""
|
|
12
|
+
lsQ = ["'", '"']
|
|
13
|
+
for i in range(len(ls)):
|
|
14
|
+
for k in range(2):
|
|
15
|
+
if lsQ[k] + ls[i] in st:
|
|
16
|
+
st = st.replace(lsQ[k] + ls[i], ls[i])
|
|
17
|
+
if ls[i] + lsQ[k] in st:
|
|
18
|
+
st = st.replace(ls[i] + lsQ[k], ls[i])
|
|
19
|
+
st = st.replace(ls[i], '"' + str(ls[i]) + '"')
|
|
20
|
+
return st
|
|
21
|
+
|
|
22
|
+
def truncate_text(text, max_chars):
|
|
23
|
+
"""
|
|
24
|
+
Truncates a text to a specified maximum number of characters, preserving the last complete sentence or word.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
text (str): The input text.
|
|
28
|
+
max_chars (int): The maximum number of characters.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
str: The truncated text.
|
|
32
|
+
"""
|
|
33
|
+
if len(text) <= max_chars:
|
|
34
|
+
return text
|
|
35
|
+
truncated = text[:max_chars]
|
|
36
|
+
# Find the last complete sentence
|
|
37
|
+
last_sentence_end = max(truncated.rfind('.'), truncated.rfind('!'), truncated.rfind('?'))
|
|
38
|
+
# If a complete sentence is found, truncate up to its end
|
|
39
|
+
if last_sentence_end != -1:
|
|
40
|
+
truncated = truncated[:last_sentence_end + 1]
|
|
41
|
+
else:
|
|
42
|
+
# If no complete sentence is found, find the last complete word
|
|
43
|
+
last_word_end = truncated.rfind(' ')
|
|
44
|
+
|
|
45
|
+
# If a complete word is found, truncate up to its end
|
|
46
|
+
if last_word_end != -1:
|
|
47
|
+
truncated = truncated[:last_word_end]
|
|
48
|
+
return truncated
|
|
49
|
+
def capitalize(string):
|
|
50
|
+
return string[:1].upper() + string[1:].lower() if string else string
|
|
51
|
+
def get_from_kwargs(*args,**kwargs):
|
|
52
|
+
del_kwarg = kwargs.get('del_kwargs',False)
|
|
53
|
+
values = {}
|
|
54
|
+
for key in args:
|
|
55
|
+
if key:
|
|
56
|
+
key = str(key)
|
|
57
|
+
if key in kwargs:
|
|
58
|
+
values[key] = kwargs.get(key)
|
|
59
|
+
if del_kwarg:
|
|
60
|
+
del kwargs[key]
|
|
61
|
+
return values,kwargs
|
|
62
|
+
def get_lines(string,strip=True):
|
|
63
|
+
lines = string.split('\n')
|
|
64
|
+
if strip:
|
|
65
|
+
lines = [line for line in lines if line]
|
|
66
|
+
return lines
|
|
67
|
+
def clean_spaces(obj: str) -> str:
|
|
68
|
+
"""
|
|
69
|
+
Removes leading spaces and tabs from a string.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
obj (str): The input string.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
str: The string with leading spaces and tabs removed.
|
|
76
|
+
"""
|
|
77
|
+
if len(obj) == 0:
|
|
78
|
+
return obj
|
|
79
|
+
while obj[0] in [' ', '\t']:
|
|
80
|
+
obj = obj[1:]
|
|
81
|
+
return obj
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def replace_it(string,item,rep):
|
|
3
|
+
if item in string:
|
|
4
|
+
string = string.replace(item,rep)
|
|
5
|
+
return string
|
|
6
|
+
def while_replace(string,item,rep):
|
|
7
|
+
while True:
|
|
8
|
+
string = replace_it(string,item,rep)
|
|
9
|
+
if item not in string or item in rep:
|
|
10
|
+
return string
|
|
11
|
+
def for_replace(string,item,replace):
|
|
12
|
+
replace = make_list(replace)
|
|
13
|
+
for rep in replace:
|
|
14
|
+
string = while_replace(string,item,rep)
|
|
15
|
+
return string
|
|
16
|
+
def replace_all(string,*args,**kwargs):
|
|
17
|
+
for items in args:
|
|
18
|
+
if items and isinstance(items,list):
|
|
19
|
+
item = items[0]
|
|
20
|
+
replace = items[1:] if len(items)>1 else items[-1]
|
|
21
|
+
string = for_replace(string,item,replace)
|
|
22
|
+
values,kwargs = get_from_kwargs('item','replace',**kwargs)
|
|
23
|
+
if values:
|
|
24
|
+
string = for_replace(string,**values)
|
|
25
|
+
for item,replace in kwargs.items():
|
|
26
|
+
string = for_replace(string,item,rep)
|
|
27
|
+
return string
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""
|
|
2
|
+
thread_utils.py - Thread Utilities Module
|
|
3
|
+
|
|
4
|
+
This module provides utility functions for working with threads in Python.
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
import abstract_utilities.thread_utils as thread_utils
|
|
8
|
+
|
|
9
|
+
Example:
|
|
10
|
+
# Creating a thread
|
|
11
|
+
my_thread = thread_utils.get_thread(target=my_function, args=(arg1, arg2), daemon=True)
|
|
12
|
+
|
|
13
|
+
# Starting the thread
|
|
14
|
+
thread_utils.start_thread(my_thread)
|
|
15
|
+
|
|
16
|
+
# Verifying if a given object is a valid thread
|
|
17
|
+
is_valid = thread_utils.verify_thread(my_thread)
|
|
18
|
+
|
|
19
|
+
# Checking if a thread is alive
|
|
20
|
+
if thread_utils.thread_alive(my_thread):
|
|
21
|
+
print("Thread is alive")
|
|
22
|
+
else:
|
|
23
|
+
print("Thread is not alive")
|
|
24
|
+
|
|
25
|
+
This module is part of the `abstract_utilities` package.
|
|
26
|
+
|
|
27
|
+
Author: putkoff
|
|
28
|
+
Date: 10/25/2023
|
|
29
|
+
Version: 0.1.2
|
|
30
|
+
"""
|
|
31
|
+
from .imports import *
|
|
32
|
+
class ThreadedEvent:
|
|
33
|
+
def __init__(self, target_function,termination_result=None,function_args={},daemon=True):
|
|
34
|
+
self._event = threading.Event()
|
|
35
|
+
self._queue = queue.Queue() # Add a queue for return values
|
|
36
|
+
self._thread = threading.Thread(target=self._run, args=(target_function,termination_result,function_args),daemon=daemon)
|
|
37
|
+
def _run(self, target_function, termination_result, function_args):
|
|
38
|
+
print(f"Thread starting with event status: {self._event.is_set()}")
|
|
39
|
+
while not self._event.is_set():
|
|
40
|
+
print(f"In loop. Event status: {self._event.is_set()}")
|
|
41
|
+
result = target_function(**function_args)
|
|
42
|
+
if result:
|
|
43
|
+
self._queue.put(result) # Store the return value in the queue
|
|
44
|
+
self._event.wait(1) # This will wait for 1 second or until interrupted by calling `stop`.
|
|
45
|
+
print(f"Thread {self._thread.name} is exiting.")
|
|
46
|
+
def get_result(self):
|
|
47
|
+
"""Retrieve the result from the queue. Returns None if no result available."""
|
|
48
|
+
if not self._queue.empty():
|
|
49
|
+
return self._queue.get()
|
|
50
|
+
return None
|
|
51
|
+
def start(self):
|
|
52
|
+
"""Starts the thread."""
|
|
53
|
+
self._thread.start()
|
|
54
|
+
def stop(self):
|
|
55
|
+
print(f"Attempting to stop thread {self._thread.name}")
|
|
56
|
+
self._event.set()
|
|
57
|
+
# # Optionally, you can wait for the thread to finish.
|
|
58
|
+
def wait(self,n=0):
|
|
59
|
+
self._thread.wait(n)
|
|
60
|
+
def join(self):
|
|
61
|
+
self._thread.join()
|
|
62
|
+
def is_alive(self):
|
|
63
|
+
"""Returns whether the thread is still running."""
|
|
64
|
+
return self._thread.is_alive()
|
|
65
|
+
class ThreadManager:
|
|
66
|
+
def __init__(self):
|
|
67
|
+
self.threads = {}
|
|
68
|
+
self.thread_name_list=[]
|
|
69
|
+
def add_thread(self,name=None,target_function=None,termination_result=None,function_args={},overwrite=False,make_default=True,daemon=True):
|
|
70
|
+
if name == None:
|
|
71
|
+
if make_default:
|
|
72
|
+
name = 'Default_Thread_name'
|
|
73
|
+
else:
|
|
74
|
+
return
|
|
75
|
+
if target_function == None:
|
|
76
|
+
return
|
|
77
|
+
if overwrite==False:
|
|
78
|
+
name=create_new_name(name=name,names_list=self.all_thread_names())
|
|
79
|
+
if name in self.thread_name_list:
|
|
80
|
+
self.thread_name_list.remove(name)
|
|
81
|
+
self.thread_name_list.append(name)
|
|
82
|
+
"""Add a thread with a name and target function."""
|
|
83
|
+
self.threads[name] = ThreadedEvent(target_function=target_function,termination_result=termination_result,function_args=function_args,daemon=daemon)
|
|
84
|
+
return name
|
|
85
|
+
def start(self, name,overwrite=False):
|
|
86
|
+
"""Start a specific thread by name."""
|
|
87
|
+
if self.is_alive(name)==False:
|
|
88
|
+
self.threads[name].start()
|
|
89
|
+
def wait(self,name,n=0):
|
|
90
|
+
self.threads[name].wait(n)
|
|
91
|
+
def join(self,name):
|
|
92
|
+
self.threads[name].join()
|
|
93
|
+
def stop(self, name, result=None):
|
|
94
|
+
if self.check_name(name):
|
|
95
|
+
if result:
|
|
96
|
+
self.threads[name]._queue.put(result)
|
|
97
|
+
self.threads[name].stop()
|
|
98
|
+
return True
|
|
99
|
+
def stop_last(self,name=None,result=None):
|
|
100
|
+
str_name = f"for {name}" if name else ""
|
|
101
|
+
if name ==None:
|
|
102
|
+
name=get_last_comp_list(name,self.all_thread_names())
|
|
103
|
+
if self.check_name(name):
|
|
104
|
+
return self.stop(name,result=result)
|
|
105
|
+
return False
|
|
106
|
+
def start_all(self):
|
|
107
|
+
"""Start all threads."""
|
|
108
|
+
for thread in self.threads.values():
|
|
109
|
+
thread.start()
|
|
110
|
+
|
|
111
|
+
def stop_all(self):
|
|
112
|
+
"""Stop all threads."""
|
|
113
|
+
for thread in self.threads.values():
|
|
114
|
+
thread.stop()
|
|
115
|
+
|
|
116
|
+
def is_alive(self, name):
|
|
117
|
+
"""Check if a specific thread is alive by name."""
|
|
118
|
+
if self.check_name(name):
|
|
119
|
+
status = self.threads[name].is_alive()
|
|
120
|
+
return status
|
|
121
|
+
return False
|
|
122
|
+
def all_alive(self):
|
|
123
|
+
"""Return a dictionary indicating if each thread is alive."""
|
|
124
|
+
return {name: thread.is_alive() for name, thread in self.threads.items()}
|
|
125
|
+
def all_thread_names(self):
|
|
126
|
+
return self.threads.keys()
|
|
127
|
+
def get_last_result(self,name=None):
|
|
128
|
+
str_name = f"for {name}" if name else ""
|
|
129
|
+
result = None
|
|
130
|
+
if name:
|
|
131
|
+
result_name=get_last_comp_list(name,self.all_thread_names())
|
|
132
|
+
if self.check_name(name):
|
|
133
|
+
result = self.threads[result_name].get_result()
|
|
134
|
+
elif len(self.thread_name_list)>0:
|
|
135
|
+
result = self.threads[self.thread_name_list[-1]].get_result()
|
|
136
|
+
return result
|
|
137
|
+
def check_name(self,name):
|
|
138
|
+
if name in self.all_thread_names():
|
|
139
|
+
return True
|
|
140
|
+
return False
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from ...type_utils import is_number,make_list
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
"""
|
|
2
|
+
time_utils.py
|
|
3
|
+
|
|
4
|
+
This module provides utility functions for working with timestamps and time-related operations.
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
import abstract_utilities.time_utils as time_utils
|
|
8
|
+
|
|
9
|
+
Functions:
|
|
10
|
+
- get_sleep(sleep_timer): Pause the execution of the program for a specified duration.
|
|
11
|
+
- get_time_stamp() -> float: Returns the current timestamp in seconds.
|
|
12
|
+
- get_milisecond_time_stamp() -> float: Returns the current timestamp in milliseconds.
|
|
13
|
+
- get_day(now=get_time_stamp()) -> str: Returns the current day of the week.
|
|
14
|
+
- get_time(now=get_time_stamp()) -> str: Returns the current time.
|
|
15
|
+
- get_date(now=get_time_stamp()) -> str: Returns the current date.
|
|
16
|
+
- save_last_time(now=get_time_stamp()) -> str: Saves the last timestamp to a file.
|
|
17
|
+
- get_day_seconds() -> float: Returns the number of seconds in a day.
|
|
18
|
+
- get_week_seconds() -> float: Returns the number of seconds in a week.
|
|
19
|
+
- get_hour_seconds() -> float: Returns the number of seconds in an hour.
|
|
20
|
+
- get_minute_seconds() -> float: Returns the number of seconds in a minute.
|
|
21
|
+
- get_second() -> float: Returns the value of one second.
|
|
22
|
+
- get_24_hr_start(now=get_time_stamp()) -> int: Returns the timestamp for the start of the current day.
|
|
23
|
+
|
|
24
|
+
This module is part of the `abstract_utilities` package.
|
|
25
|
+
|
|
26
|
+
Author: putkoff
|
|
27
|
+
Date: 05/31/2023
|
|
28
|
+
Version: 0.1.2
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from .imports import *
|
|
32
|
+
def sleep_count_down(sleep_time):
|
|
33
|
+
while sleep_time>float(0):
|
|
34
|
+
sleep_time -= float(1)
|
|
35
|
+
print(f"{max(sleep_time,0)} seconds till start")
|
|
36
|
+
get_sleep(1)
|
|
37
|
+
def sleep_timer(last_time: Union[str, float], wait_limit: Union[float, int] = 0) -> None:
|
|
38
|
+
"""
|
|
39
|
+
Pause execution for a specified amount of time, calculated based on time differences.
|
|
40
|
+
|
|
41
|
+
Parameters:
|
|
42
|
+
last_time (str or float): The reference time or timestamp.
|
|
43
|
+
wait_limit (float or int, optional): The maximum wait time in seconds. Defaults to 0.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
None
|
|
47
|
+
"""
|
|
48
|
+
curr_time = get_time_stamp()
|
|
49
|
+
time_difference = curr_time - float(last_time)
|
|
50
|
+
if time_difference < wait_limit:
|
|
51
|
+
sleep_length = float(wait_limit - time_difference)
|
|
52
|
+
get_sleep(sleep_timer=sleep_length)
|
|
53
|
+
def get_sleep(sleep_timer: Union[int, float] = 0):
|
|
54
|
+
"""
|
|
55
|
+
Pause the execution of the program for a specified duration.
|
|
56
|
+
|
|
57
|
+
This function pauses the program's execution for the given amount of time
|
|
58
|
+
before continuing. The sleep duration can be specified in seconds as an integer
|
|
59
|
+
or a floating-point number.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
sleep_timer (int or float, optional): The duration in seconds to pause the execution.
|
|
63
|
+
If not provided or set to 0, the function does nothing.
|
|
64
|
+
|
|
65
|
+
Example:
|
|
66
|
+
get_sleep(2) # Pause the program for 2 seconds.
|
|
67
|
+
"""
|
|
68
|
+
if isinstance(sleep_timer,int):
|
|
69
|
+
sleep_timer=float(sleep_timer)
|
|
70
|
+
if isinstance(sleep_timer,float):
|
|
71
|
+
time.sleep(sleep_timer)
|
|
72
|
+
def get_time_stamp() -> float:
|
|
73
|
+
"""
|
|
74
|
+
Returns the current timestamp in seconds.
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
float: The current timestamp.
|
|
78
|
+
"""
|
|
79
|
+
return datetime.now().timestamp()
|
|
80
|
+
def get_date_time_str(date_str,military_time_str):
|
|
81
|
+
return f"{date_str} {military_time_str}"
|
|
82
|
+
def get_time_obj(date_time_str):
|
|
83
|
+
return datetime.strptime(date_time_str, '%Y-%m-%d %H:%M')
|
|
84
|
+
def get_current_year() -> int:
|
|
85
|
+
"""
|
|
86
|
+
Get the current year.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
int: The current year as an integer.
|
|
90
|
+
"""
|
|
91
|
+
now = datetime.now()
|
|
92
|
+
current_year = now.year
|
|
93
|
+
return current_year
|
|
94
|
+
|
|
95
|
+
def get_24_hr_start(now=get_time_stamp()) -> int:
|
|
96
|
+
"""
|
|
97
|
+
Returns the timestamp for the start of the current day.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
now (float): The timestamp. Defaults to the current timestamp.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
int: The timestamp for the start of the current day.
|
|
104
|
+
"""
|
|
105
|
+
return int(now) - int(get_day_seconds())
|
|
106
|
+
|
|
107
|
+
def create_timestamp(date_str, military_time_str):
|
|
108
|
+
"""
|
|
109
|
+
Converts a date string and military time string to a timestamp.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
date_str (str): The date string in the format 'YYYY-MM-DD'.
|
|
113
|
+
military_time_str (str): The military time string in the format 'HH:MM'.
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
int: The timestamp corresponding to the date and time.
|
|
117
|
+
"""
|
|
118
|
+
date_time_str = get_date_time_str(date_str,military_time_str)
|
|
119
|
+
return int(get_time_obj(date_time_str).timestamp())
|
|
120
|
+
|
|
121
|
+
def get_milisecond_time_stamp():
|
|
122
|
+
"""
|
|
123
|
+
Returns the current timestamp in milliseconds.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
float: The current timestamp in milliseconds.
|
|
127
|
+
"""
|
|
128
|
+
return datetime.now().timestamp() * 1000
|
|
129
|
+
|
|
130
|
+
def get_day(now=get_time_stamp()):
|
|
131
|
+
"""
|
|
132
|
+
Returns the current day of the week.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
now (float): The timestamp. Defaults to the current timestamp.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
str: The current day of the week.
|
|
139
|
+
"""
|
|
140
|
+
return datetime.fromtimestamp(now).strftime("%A")
|
|
141
|
+
|
|
142
|
+
def get_time(now=get_time_stamp()):
|
|
143
|
+
"""
|
|
144
|
+
Returns the current time.
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
now (float): The timestamp. Defaults to the current timestamp.
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
str: The current time.
|
|
151
|
+
"""
|
|
152
|
+
return str(datetime.fromtimestamp(now))[10:]
|
|
153
|
+
|
|
154
|
+
def get_date(now=get_time_stamp()):
|
|
155
|
+
"""
|
|
156
|
+
Returns the current date.
|
|
157
|
+
|
|
158
|
+
Args:
|
|
159
|
+
now (float): The timestamp. Defaults to the current timestamp.
|
|
160
|
+
|
|
161
|
+
Returns:
|
|
162
|
+
str: The current date.
|
|
163
|
+
"""
|
|
164
|
+
return str(datetime.fromtimestamp(now))[:10]
|
|
165
|
+
|
|
166
|
+
def save_last_time(now=get_time_stamp()):
|
|
167
|
+
"""
|
|
168
|
+
Saves the last timestamp to a file.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
now (float): The timestamp. Defaults to the current timestamp.
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
str: The filename where the timestamp is saved.
|
|
175
|
+
"""
|
|
176
|
+
return pen(str(now), 'last.txt')
|
|
177
|
+
|
|
178
|
+
def get_day_seconds():
|
|
179
|
+
"""
|
|
180
|
+
Returns the number of seconds in a day.
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
float: The number of seconds in a day.
|
|
184
|
+
"""
|
|
185
|
+
return float(24 * 60 * 60)
|
|
186
|
+
|
|
187
|
+
def get_week_seconds():
|
|
188
|
+
"""
|
|
189
|
+
Returns the number of seconds in a week.
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
float: The number of seconds in a week.
|
|
193
|
+
"""
|
|
194
|
+
return float(7 * 24 * 60 * 60)
|
|
195
|
+
|
|
196
|
+
def get_hour_seconds():
|
|
197
|
+
"""
|
|
198
|
+
Returns the number of seconds in an hour.
|
|
199
|
+
|
|
200
|
+
Returns:
|
|
201
|
+
float: The number of seconds in an hour.
|
|
202
|
+
"""
|
|
203
|
+
return float(60 * 60)
|
|
204
|
+
|
|
205
|
+
def get_minute_seconds():
|
|
206
|
+
"""
|
|
207
|
+
Returns the number of seconds in a minute.
|
|
208
|
+
|
|
209
|
+
Returns:
|
|
210
|
+
float: The number of seconds in a minute.
|
|
211
|
+
"""
|
|
212
|
+
return float(60)
|
|
213
|
+
|
|
214
|
+
def get_second():
|
|
215
|
+
"""
|
|
216
|
+
Returns the value of one second.
|
|
217
|
+
|
|
218
|
+
Returns:
|
|
219
|
+
float: The value of one second.
|
|
220
|
+
"""
|
|
221
|
+
return float(1)
|
|
222
|
+
|
|
223
|
+
def all_combinations_of_strings(strings):
|
|
224
|
+
"""
|
|
225
|
+
Generate all possible combinations of the input strings.
|
|
226
|
+
Each combination is a concatenation of the input strings in different orders.
|
|
227
|
+
|
|
228
|
+
:param strings: A list of strings for which we want to generate all combinations.
|
|
229
|
+
:return: A list of strings representing all possible combinations.
|
|
230
|
+
"""
|
|
231
|
+
from itertools import permutations
|
|
232
|
+
|
|
233
|
+
# Generate all permutations of the input list
|
|
234
|
+
all_perms = permutations(strings)
|
|
235
|
+
|
|
236
|
+
# Concatenate strings in each permutation to form the combinations
|
|
237
|
+
combinations = [''.join(perm) for perm in all_perms]
|
|
238
|
+
|
|
239
|
+
return combinations
|
|
240
|
+
def all_date_formats():
|
|
241
|
+
date_formats=[]
|
|
242
|
+
for part in all_combinations_of_strings(list("Ymd")):
|
|
243
|
+
for seperator in ['/','-','_']:
|
|
244
|
+
date_range='%'
|
|
245
|
+
for piece in list(str(part)):
|
|
246
|
+
date_range+=f"{piece}{seperator}%"
|
|
247
|
+
for times in ["%H:%M:%S.%f","%H:%M:%S"]:
|
|
248
|
+
date_format = f"{date_range[:-2]} {times}"
|
|
249
|
+
if date_format not in date_formats:
|
|
250
|
+
date_formats.append(date_format)
|
|
251
|
+
return date_formats
|
|
252
|
+
|
|
253
|
+
def get_time_string(data):
|
|
254
|
+
strin = 0
|
|
255
|
+
for string in ["Timestamp","Data_Time_Stamp"]:
|
|
256
|
+
strings = data.get(string)
|
|
257
|
+
if strin:
|
|
258
|
+
pass
|
|
259
|
+
return strings
|
|
260
|
+
def convert_date_to_timestamp(date_string,date_format=[]):
|
|
261
|
+
date_formats = all_date_formats()
|
|
262
|
+
date_string = str(date_string)
|
|
263
|
+
date_formats = make_list(date_format)+date_formats
|
|
264
|
+
for date_format in date_formats:
|
|
265
|
+
try:
|
|
266
|
+
date_object = datetime.strptime(date_string, date_format)
|
|
267
|
+
return date_object.timestamp()
|
|
268
|
+
except ValueError:
|
|
269
|
+
continue
|
|
270
|
+
print(f"Date format not recognized: {date_string}")
|
|
271
|
+
return None
|
|
272
|
+
def get_rounded_hour_datetime(timeStamp=None,hours=0):
|
|
273
|
+
"""
|
|
274
|
+
Returns the current datetime rounded down to the nearest hour,
|
|
275
|
+
with an optional adjustment by a specified number of hours.
|
|
276
|
+
|
|
277
|
+
:param hours: Number of hours to adjust the rounded datetime by.
|
|
278
|
+
Defaults to 0 (no adjustment).
|
|
279
|
+
:return: A datetime object for the adjusted, rounded-down hour.
|
|
280
|
+
"""
|
|
281
|
+
now = get_convert_timestamp_to_datetime(timeStamp or datetime.now())
|
|
282
|
+
# Round down to the nearest hour
|
|
283
|
+
rounded_hour = now.replace(minute=0, second=0, microsecond=0)
|
|
284
|
+
# Adjust by the specified number of hours
|
|
285
|
+
adjusted_datetime = rounded_hour + timedelta(hours=hours)
|
|
286
|
+
return adjusted_datetime
|
|
287
|
+
|
|
288
|
+
def get_current_time_with_delta(days=0, hours=0, minutes=0, seconds=0,milliseconds=0):
|
|
289
|
+
# Get the current datetime
|
|
290
|
+
current_time = get_time_stamp()
|
|
291
|
+
|
|
292
|
+
# Create a timedelta with the specified duration
|
|
293
|
+
delta = timedelta(days=days, hours=hours, minutes=minutes,seconds=seconds, milliseconds=milliseconds)
|
|
294
|
+
|
|
295
|
+
# Add the delta to the current time
|
|
296
|
+
new_time = current_time + delta
|
|
297
|
+
|
|
298
|
+
return new_time
|
|
299
|
+
def get_time_stamp_date():
|
|
300
|
+
return datetime.now()
|
|
301
|
+
|
|
302
|
+
def get_time_stamp_now():
|
|
303
|
+
return datetime.datetime.now()
|
|
304
|
+
|
|
305
|
+
def get_timestamp():
|
|
306
|
+
return time.time()
|
|
307
|
+
|
|
308
|
+
def get_hours_ago_datetime(hours=1):
|
|
309
|
+
return get_time_stamp() - get_time_delta_hour(hours=hours)
|
|
310
|
+
|
|
311
|
+
def get_hours_ago(hours=1):
|
|
312
|
+
hours_ago = get_hours_ago_datetime(hours=1)
|
|
313
|
+
return hours_ago.timestamp()
|
|
314
|
+
|
|
315
|
+
def get_daily_output(timeStamp=None):
|
|
316
|
+
timeStamp = get_convert_timestamp_to_datetime(convert_date_to_timestamp(timeStamp or get_time_stamp()))
|
|
317
|
+
return timeStamp.strftime('%m-%d-%Y')
|
|
318
|
+
|
|
319
|
+
def get_hourly_output(timeStamp=None):
|
|
320
|
+
timeStamp = get_convert_timestamp_to_datetime(convert_date_to_timestamp(timeStamp or get_time_stamp()))
|
|
321
|
+
return timeStamp.strftime('%H') + '00'
|
|
322
|
+
|
|
323
|
+
def get_hour_time_stamp(hours=0):
|
|
324
|
+
return convert_date_to_timestamp(f"{get_rounded_hour_datetime(hours=hours)}")
|
|
325
|
+
|
|
326
|
+
def get_variable_time_stamp(days=0,hours=0,minutes=0,seconds=0):
|
|
327
|
+
return convert_date_to_timestamp(f"{get_daily_output()} 0:0:00")+(days*(60*60*24))+(hours*(60*60))+(minutes*60)+seconds
|
|
328
|
+
|
|
329
|
+
def get_variable_datetime(days=0,hours=0,minutes=0,seconds=0):
|
|
330
|
+
timeStamp = get_variable_time_stamp(days=0,hours=0,minutes=0,seconds=0)
|
|
331
|
+
return get_convert_timestamp_to_datetime(timeStamp)
|
|
332
|
+
def get_convert_datetime_to_timeStamp(timeStamp):
|
|
333
|
+
timeStamp = timeStamp or get_timestamp()
|
|
334
|
+
if not is_number(timeStamp):
|
|
335
|
+
timeStamp = convert_date_to_timestamp(timeStamp)
|
|
336
|
+
return timeStamp
|
|
337
|
+
def get_convert_timestamp_to_datetime(timeStamp=None):
|
|
338
|
+
timeStamp = timeStamp or get_time_stamp()
|
|
339
|
+
if is_number(timeStamp):
|
|
340
|
+
timeStamp = datetime.fromtimestamp(timeStamp)
|
|
341
|
+
return timeStamp
|
|
342
|
+
def is_within_day(timeStamp):
|
|
343
|
+
return is_between_range(get_convert_timestamp(timeStamp),get_day_time_stamp(),get_day_time_stamp(days=1))
|
|
344
|
+
def is_within_hour(timeStamp):
|
|
345
|
+
return is_between_range(get_convert_timestamp(timeStamp),get_hour_time_stamp(),get_hour_time_stamp(hours=1))
|
|
346
|
+
|
|
347
|
+
def is_between_range(target,start,end):
|
|
348
|
+
if target >= start and target <=end:
|
|
349
|
+
return True
|
|
350
|
+
return False
|
|
351
|
+
def get_creation_time_of_file(file_path):
|
|
352
|
+
if os.path.isfile(file_path):
|
|
353
|
+
creation_time = os.path.getctime(file_path)
|
|
354
|
+
return datetime.datetime.fromtimestamp(creation_time)
|
|
355
|
+
|
|
356
|
+
def timestamp_to_milliseconds(timestamp):
|
|
357
|
+
try:
|
|
358
|
+
t = datetime.strptime(timestamp, "%H:%M:%S.%f")
|
|
359
|
+
return (t.hour * 3600 + t.minute * 60 + t.second) * 1000 + int(t.microsecond / 1000)
|
|
360
|
+
except ValueError:
|
|
361
|
+
raise ValueError(f"Invalid timestamp format: {timestamp}")
|
|
362
|
+
|
|
363
|
+
def parse_timestamp(ts_str):
|
|
364
|
+
parts = ts_str.split(":")
|
|
365
|
+
return (int(parts[0]) * 60 + int(parts[1])) * 1000 if len(parts) == 2 else int(parts[0]) * 1000
|
|
366
|
+
def format_timestamp(ms):
|
|
367
|
+
"""Convert milliseconds to a formatted timestamp (HH:MM:SS.mmm)."""
|
|
368
|
+
td = timedelta(milliseconds=ms)
|
|
369
|
+
hours, remainder = divmod(td.seconds, 3600)
|
|
370
|
+
minutes, seconds = divmod(remainder, 60)
|
|
371
|
+
milliseconds = td.microseconds // 1000
|
|
372
|
+
return f"{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}"
|
|
373
|
+
|
|
374
|
+
def get_time_now_iso():
|
|
375
|
+
return datetime.now().isoformat()
|
|
376
|
+
def is_valid_time(comp_timestamp, timestamp=None, before=True):
|
|
377
|
+
if timestamp is None:
|
|
378
|
+
return True
|
|
379
|
+
return (timestamp >= comp_timestamp) if before else (timestamp <= comp_timestamp)
|
|
380
|
+
|
|
381
|
+
# Function: get_time_stamp
|
|
382
|
+
# Function: get_milisecond_time_stamp
|
|
383
|
+
# Function: get_day
|
|
384
|
+
# Function: get_time
|
|
385
|
+
# Function: get_date
|
|
386
|
+
# Function: save_last_time
|
|
387
|
+
# Function: get_day_seconds
|
|
388
|
+
# Function: get_week_seconds
|
|
389
|
+
# Function: get_hour_seconds
|
|
390
|
+
# Function: get_minute_seconds
|
|
391
|
+
# Function: get_second
|
|
392
|
+
# Function: get_24_hr_start
|