-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Grouped utilities into the utils.py package
- Loading branch information
1 parent
f9ba6cf
commit 048a1c0
Showing
4 changed files
with
64 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/python3 | ||
# SPDX-License-Identifier: MIT | ||
|
||
import time | ||
import logging | ||
import sys | ||
|
||
log = logging.getLogger("testcrush logger") | ||
log.setLevel(logging.DEBUG) | ||
|
||
log_stream = logging.StreamHandler(stream=sys.stdout) | ||
log_stream.setLevel(logging.INFO) | ||
log_stream.setFormatter(logging.Formatter('[%(levelname)s]: %(message)s')) | ||
|
||
log_file = logging.FileHandler(filename="testcrush_debug.log", mode='w') | ||
log_file.setLevel(logging.DEBUG) | ||
log_file.setFormatter(logging.Formatter('%(lineno)d:[%(levelname)s|%(module)s|%(funcName)s]: %(message)s')) | ||
|
||
log.addHandler(log_stream) | ||
log.addHandler(log_file) | ||
|
||
|
||
class Timer(): | ||
""" | ||
Context manager style timer. To be used as: ``with Timer():`` | ||
""" | ||
|
||
def __enter__(self): | ||
|
||
self.start = time.perf_counter() | ||
return self | ||
|
||
def __exit__(self, *args): | ||
|
||
self.end = time.perf_counter() | ||
self.interval = self.end - self.start | ||
print(f"Execution time: {self.format_time(self.interval)}") | ||
|
||
def format_time(self, seconds): | ||
|
||
days, remainder = divmod(seconds, 86_400) # 86400 seconds in a day | ||
hours, remainder = divmod(remainder, 3_600) # 3600 seconds in an hour | ||
minutes, seconds = divmod(remainder, 60) # 60 seconds in a minute | ||
return f"{int(days)}d {int(hours)}h {int(minutes)}m {seconds:.2f}s" | ||
|
||
|
||
class Singleton(type): | ||
""" | ||
Singleton design pattern. To be used as a metaclass: ``class A(metaclass = Singleton)`` | ||
""" | ||
|
||
_instances = {} | ||
|
||
def __call__(cls, *args, **kwargs): | ||
|
||
if cls not in cls._instances: | ||
|
||
instance = super().__call__(*args, **kwargs) | ||
cls._instances[cls] = instance | ||
|
||
return cls._instances[cls] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters