Skip to content

Commit 4b24ce8

Browse files
author
DUYN
committed
Add TimeUnit
1 parent ad2bb8a commit 4b24ce8

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

investing_bot_framework/core/utils.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import sys
22
from threading import Thread
33
from wrapt import synchronized
4+
from enum import Enum
5+
6+
from investing_bot_framework.core.exceptions import OperationalException
47

58

69
class Singleton(type):
@@ -52,4 +55,52 @@ def localtrace(self, frame, event, arg):
5255
return self.localtrace
5356

5457
def kill(self):
55-
self.killed = True
58+
self.killed = True
59+
60+
61+
class TimeUnit(Enum):
62+
"""
63+
Class TimeUnit: Enum for TimeUnit
64+
"""
65+
66+
SECOND = 'SEC',
67+
MINUTE = 'MIN',
68+
HOUR = 'HR',
69+
ALWAYS = 'ALWAYS'
70+
71+
# Static factory method to convert a string to time_unit
72+
@staticmethod
73+
def from_string(value: str):
74+
75+
if isinstance(value, str):
76+
77+
if value.lower() in ('sec', 'second', 'seconds'):
78+
return TimeUnit.SECOND
79+
80+
elif value.lower() in ('min', 'minute', 'minutes'):
81+
return TimeUnit.MINUTE
82+
83+
elif value.lower() in ('hr', 'hour', 'hours'):
84+
return TimeUnit.HOUR
85+
86+
elif value.lower() in ('always', 'every', 'continuous', 'every_time'):
87+
return TimeUnit.ALWAYS
88+
else:
89+
raise OperationalException('Could not convert value {} to a time_unit'.format(value))
90+
91+
else:
92+
raise OperationalException("Could not convert non string value to a time_unit")
93+
94+
def equals(self, other):
95+
96+
if isinstance(other, Enum):
97+
return self.value == other.value
98+
else:
99+
100+
try:
101+
time_unit = TimeUnit.from_string(other)
102+
return time_unit == self
103+
except OperationalException:
104+
pass
105+
106+
return other == self.value

0 commit comments

Comments
 (0)