Skip to content

Commit

Permalink
Update time functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mxbi committed Jan 30, 2018
1 parent 697ee17 commit 4b19147
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```

Expand Down
9 changes: 9 additions & 0 deletions mlcrate/backend.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 11 additions & 5 deletions mlcrate/time.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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):
Expand All @@ -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:
Expand Down

0 comments on commit 4b19147

Please sign in to comment.