from time import time
import datetime
import math


def _human_time(*args, **kwargs):
	secs = float(datetime.timedelta(*args, **kwargs).total_seconds())
	units = [("day", 86400), ("hour", 3600), ("minute", 60), ("second", 1)]
	parts = []
	for unit, mul in units:
		if secs / mul >= 1 or mul == 1:
			if mul > 1:
				n = int(math.floor(secs / mul))
				secs -= n * mul
			else:
				n = secs if secs != int(secs) else int(secs)
			parts.append("%s %s%s" % (n, unit, "" if n == 1 else "s"))
	return ", ".join(parts)


class Profiler:
	def __init__(self, message):
		self.message = message

	def __enter__(self):
		self.start = time()
		return None  # could return anything, to be used like this: with Timer("Message") as value:

	def __exit__(self, type, value, traceback):
		sec = time() - self.start
		elapsed_time = _human_time(seconds=sec)
		print(f"{self.message} took {format(elapsed_time)}")
