tradezero_api.time_helpers
1from __future__ import annotations 2 3import time 4import datetime as dt 5 6import pytz 7 8 9class Time: 10 @property 11 def time(self): 12 """ return current EST time as a datetime object """ 13 tz_ny = pytz.timezone('US/Eastern') 14 time1 = dt.datetime.now(tz=tz_ny).time() 15 return time1 16 17 def time_between(self, time1: tuple, time2: tuple): 18 """ 19 return True if current time between: time1, and time2, else: return False 20 21 :param time1: tuple, ex: (10, 30), or with sec and micro-sec: (10, 30, 0, 250000) 22 :param time2: tuple, ex: (12, 45) 23 :return: bool 24 """ 25 if dt.time(*time1) < self.time < dt.time(*time2): 26 return True 27 return False 28 29 30class Timer: 31 """ 32 A class that behaves like a stopwatch, when initialized the counter starts, 33 and at any given moment you can check the total time elapsed 34 with the time_elapsed property 35 """ 36 def __init__(self): 37 self.timer_start = time.perf_counter() 38 39 @property 40 def time_elapsed(self): 41 return time.perf_counter() - self.timer_start 42 43 44def time_it(func): 45 """Decorator for debugging the time taken to run a function""" 46 def wrapper(*args, **kwargs): 47 timer = Timer() 48 rv = func(*args, **kwargs) 49 50 if kwargs.get('log_time_elapsed') or kwargs.get('log_info'): 51 print(f'Time elapsed: {timer.time_elapsed:.2f} seconds') 52 53 return rv 54 return wrapper
class
Time:
10class Time: 11 @property 12 def time(self): 13 """ return current EST time as a datetime object """ 14 tz_ny = pytz.timezone('US/Eastern') 15 time1 = dt.datetime.now(tz=tz_ny).time() 16 return time1 17 18 def time_between(self, time1: tuple, time2: tuple): 19 """ 20 return True if current time between: time1, and time2, else: return False 21 22 :param time1: tuple, ex: (10, 30), or with sec and micro-sec: (10, 30, 0, 250000) 23 :param time2: tuple, ex: (12, 45) 24 :return: bool 25 """ 26 if dt.time(*time1) < self.time < dt.time(*time2): 27 return True 28 return False
def
time_between(self, time1: tuple, time2: tuple):
18 def time_between(self, time1: tuple, time2: tuple): 19 """ 20 return True if current time between: time1, and time2, else: return False 21 22 :param time1: tuple, ex: (10, 30), or with sec and micro-sec: (10, 30, 0, 250000) 23 :param time2: tuple, ex: (12, 45) 24 :return: bool 25 """ 26 if dt.time(*time1) < self.time < dt.time(*time2): 27 return True 28 return False
return True if current time between: time1, and time2, else: return False
Parameters
- time1: tuple, ex: (10, 30), or with sec and micro-sec: (10, 30, 0, 250000)
- time2: tuple, ex: (12, 45)
Returns
bool
class
Timer:
31class Timer: 32 """ 33 A class that behaves like a stopwatch, when initialized the counter starts, 34 and at any given moment you can check the total time elapsed 35 with the time_elapsed property 36 """ 37 def __init__(self): 38 self.timer_start = time.perf_counter() 39 40 @property 41 def time_elapsed(self): 42 return time.perf_counter() - self.timer_start
A class that behaves like a stopwatch, when initialized the counter starts, and at any given moment you can check the total time elapsed with the time_elapsed property
def
time_it(func):
45def time_it(func): 46 """Decorator for debugging the time taken to run a function""" 47 def wrapper(*args, **kwargs): 48 timer = Timer() 49 rv = func(*args, **kwargs) 50 51 if kwargs.get('log_time_elapsed') or kwargs.get('log_info'): 52 print(f'Time elapsed: {timer.time_elapsed:.2f} seconds') 53 54 return rv 55 return wrapper
Decorator for debugging the time taken to run a function