Skip to content

Instantly share code, notes, and snippets.

@toby-p
Created August 17, 2020 19:14
Show Gist options
  • Save toby-p/0930e7a6c15736a85bbcbd5746b0bf20 to your computer and use it in GitHub Desktop.
Save toby-p/0930e7a6c15736a85bbcbd5746b0bf20 to your computer and use it in GitHub Desktop.
decorator to store runtime performance of a function in a CSV file.
import datetime
import pandas as pd
import pytz
import time
# To switch logging on/off add config logic here to determine this variable:
LOG_PERFORMANCE = True
TIMEZONE = "US/Eastern"
DATE_FORMAT = "%Y_%m_%d %H;%M;%S",
class LogRuntime:
def __init__(self, module: str, cls: str = None, desc: str = None):
"""Decorator to log function performance."""
self.module = module
self.cls = cls
self.desc = desc
def __call__(self, func):
utc_now = pytz.utc.localize(datetime.datetime.utcnow())
now_as_string = utc_now.astimezone(pytz.timezone(TIMEZONE)).strftime(DATE_FORMAT)
def timed(*a, **kw):
# Time the function and store the results:
t1 = time.time()
f = func(*a, **kw)
# Log the runtime:
runtime = time.time() - t1
fp = os.path.join("path", "to", "master.csv")
log = pd.read_csv(fp, encoding="utf-8")
new_row = {"Timestamp": now_as_string(date_format="%Y-%m-%d %H:%M:%S,%f"),
"Module": self.module, "Class": self.cls, "Function": func.__name__,
"Description": self.desc, "Runtime": runtime}
log = log.append(new_row, sort=False, ignore_index=True)
log.to_csv(fp, encoding="utf-8", index=False)
# Return the function results:
return f
return timed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment