From 4b19147bacc177b8ee4c2c5d45d21a5c2ad698c9 Mon Sep 17 00:00:00 2001 From: Mikel Bober-Irizar Date: Tue, 30 Jan 2018 19:44:20 +0000 Subject: [PATCH] Update time functions --- README.md | 8 ++++---- mlcrate/backend.py | 9 +++++++++ mlcrate/time.py | 16 +++++++++++----- 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 mlcrate/backend.py diff --git a/README.md b/README.md index 4d39158..7788895 100644 --- a/README.md +++ b/README.md @@ -111,20 +111,20 @@ A class for tracking timestamps and time elapsed since events. Useful for profil >>> t.elapsed(0) # Number of seconds since initialisation 3.0880863666534424 >>> t.add('event') # Log an event (eg. the start of some code you want to measure) ->>> t.elapsed('event') # Elapsed seconds since the event +>>> t.since('event') # Elapsed seconds since the event 4.758380889892578 ->>> t.format_elapsed('event') # Get the elapsed time in a pretty format +>>> t.fsince('event') # Get the elapsed time in a pretty format '1h03m12s' >>> t['event'] # Get the timestamp of event 1514476396.0099056 ``` -###### [mlcrate.time.str_time_now()](https://github.com/mxbi/mlcrate/blob/4cf3f95f557886d8fdf97e4a5ab0908edaa51332/mlcrate/time.py#L33) +###### [mlcrate.time.now()](https://github.com/mxbi/mlcrate/blob/4cf3f95f557886d8fdf97e4a5ab0908edaa51332/mlcrate/time.py#L33) Returns the current time as a string in the format `'YYYY_MM_DD_HH_MM_SS'`. Useful for timestamping filenames etc. ```python ->>> mlc.time.str_time_now() +>>> mlc.time.now() '2017_12_28_16_58_29' ``` diff --git a/mlcrate/backend.py b/mlcrate/backend.py new file mode 100644 index 0000000..ddfb6c4 --- /dev/null +++ b/mlcrate/backend.py @@ -0,0 +1,9 @@ +from warnings import warn + +# Function wrapper that warns is deprecated, and call the function anyway +def _deprecated(func, old_name, new_name): + def new_func(*args, **kwargs): + message = '{}() has been deprecated in favour of {}() and will be removed soon'.format(old_name, new_name) + warn(message) + return func(*args, **kwargs) + return new_func \ No newline at end of file diff --git a/mlcrate/time.py b/mlcrate/time.py index c93a4d5..fd3357e 100644 --- a/mlcrate/time.py +++ b/mlcrate/time.py @@ -1,5 +1,5 @@ import time -import datetime +from .backend import _deprecated class Timer: """A class for tracking timestamps and time elapsed since events. Useful for profiling code. @@ -8,7 +8,7 @@ class Timer: >>> t = Timer() >>> t.elapsed(0) # Seconds since the timetracker was initialised >>> t.add('func') # Save the current timestamp as 'func' - >>> t.elapsed('func') # Seconds since 'func' was added + >>> t.since('func') # Seconds since 'func' was added >>> t['func'] # Get the absolute timestamp of 'func' for other uses """ def __init__(self): @@ -22,18 +22,24 @@ def add(self, key): """Add the current time to the index with the specified key""" self.times[key] = time.time() - def elapsed(self, key): + def since(self, key): """Get the time elapsed in seconds since the specified key was added to the index""" return time.time() - self.times[key] - def format_elapsed(self, key, max_fields=3): + def fsince(self, key, max_fields=3): """Get the time elapsed in seconds, nicely formatted by format_duration()""" - return format_duration(self.elapsed(key), max_fields) + return format_duration(self.since(key), max_fields) + + elapsed = _deprecated(since, 'Timer.elapsed', 'Timer.since') + format_elapsed = _deprecated(fsince, 'Timer.format_elapsed', 'Timer.fsince') def now(): """Returns the current time as a string in the format 'YYYY_MM_DD_HH_MM_SS'. Useful for timestamping filenames etc.""" return time.strftime("%Y_%m_%d_%H_%M_%S") +# Alias for backwards-compatibility +str_time_now = _deprecated(now, 'mlcrate.time.str_time_now', 'mlcrate.time.now') + def format_duration(seconds, max_fields=3): """Formats a number of seconds in a pretty readable format, in terms of seconds, minutes, hours and days. Example: