Skip to content

Commit

Permalink
Refactored timing_decorator for clarity and separation of concerns
Browse files Browse the repository at this point in the history
Functionality of the `timing_decorator` has been broken down into smaller functions for improved clarity and separation of concerns. Unnecessary code elements have been removed, and the function now logs execution time to both the console and a designated log file.
  • Loading branch information
EllingOftedalKV committed Apr 2, 2024
1 parent 7e8abd8 commit 8767939
Showing 1 changed file with 46 additions and 68 deletions.
114 changes: 46 additions & 68 deletions custom_tools/timing_decorator.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,53 @@
import time
import os
from functools import wraps

# Importing temporary files
from file_manager.n100.file_manager_buildings import Building_N100

import time
from functools import wraps

# Importing temporary files
from file_manager.n100.file_manager_buildings import Building_N100
def timing_decorator(func):
"""Logs the execution time of a function to both the console and a log file"""

@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()

result = func(*args, **kwargs)

elapsed_time = compute_elapsed_time(start_time)

log_to_console_and_file(func.__name__, elapsed_time)

return result

return wrapper


def compute_elapsed_time(start_time):
"""Computes the elapsed time given a starting time"""
elapsed_time_seconds = time.time() - start_time

elapsed_minutes, elapsed_seconds = divmod(elapsed_time_seconds, 60)

return elapsed_minutes, elapsed_seconds


def log_to_console_and_file(function_name, elapsed_time):
"""Logs a messages to both the console and a file"""
elapsed_minutes, elapsed_seconds = elapsed_time
output = f"{function_name} execution time: {int(elapsed_minutes)} minutes {elapsed_seconds:.0f} seconds"

log_to_console(output)
log_to_file(output)


def log_to_console(message):
"""Prints a given message to the console"""
print(message)


# List to store print statements
print_output = []

# Decorator to measure execution time of functions
def timing_decorator(arg=None):
if isinstance(arg, str): # If arg is a string, use it as a custom name
custom_name = arg

def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
minutes = int(elapsed_time // 60)
seconds = elapsed_time % 60
output = f"{custom_name} execution time: {minutes} minutes {seconds:.2f} seconds"
print_output.append(output) # Append to the list
return result

return wrapper

return decorator
else: # If arg is not a string (or None), use the function name as the default name
func = arg

@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
minutes = int(elapsed_time // 60)
seconds = elapsed_time % 60
output = f"{func.__name__} execution time: {minutes} minutes {seconds:.2f} seconds"
print_output.append(output) # Append to the list
return result

return wrapper


# Total elapsed time accumulator
total_elapsed_time = 0

# Calculate total elapsed time
for line in print_output:
minutes = int(line.split(":")[1].split()[0])
seconds = float(line.split(":")[1].split()[2])
total_elapsed_time += minutes * 60 + seconds

# Write all print statements to a file
output_file = Building_N100.overview__runtime_all_building_functions__n100.value

# Write total elapsed time to the file
with open(output_file, "w") as f:
f.write(
f"Total run time: {int(total_elapsed_time // 3600)} hours {int((total_elapsed_time % 3600) // 60)} minutes {total_elapsed_time % 60:.2f} seconds\n\n"
)

# Write all print statements to the file with additional newline characters
for line in print_output:
f.write(line + "\n")
def log_to_file(message):
"""Writes a given message to a log file"""
log_file_path = Building_N100.overview__runtime_all_building_functions__n100.value
with open(log_file_path, "a") as f:
f.write(message + "\n")

0 comments on commit 8767939

Please sign in to comment.