Coverage for source/utils/granularity.py: 67%
18 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-01 20:51 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-01 20:51 +0000
1# utils/granularity.py
3# global imports
4from enum import Enum
6# local imports
8class Granularity(Enum):
9 """
10 Enum representing possible values of time granularity that coinbase API can handle.
11 Values assigned to particular options represent number of seconds that certain option denotes.
12 """
14 # global class constants
15 ONE_MINUTE = 60
16 FIVE_MINUTES = 300
17 FIFTEEN_MINUTES = 900
18 THIRTY_MINUTES = 1800
19 ONE_HOUR = 3600
20 SIX_HOURS = 21600
21 ONE_DAY = 86400
23 def __str__(self) -> str:
24 """
25 Returns string representation of the granularity.
27 Returns:
28 (str): String representation of the granularity.
29 """
31 map = {
32 Granularity.ONE_MINUTE: "1m",
33 Granularity.FIVE_MINUTES: "5m",
34 Granularity.FIFTEEN_MINUTES: "15m",
35 Granularity.THIRTY_MINUTES: "30m",
36 Granularity.ONE_HOUR: "1h",
37 Granularity.SIX_HOURS: "6h",
38 Granularity.ONE_DAY: "1d"
39 }
41 return map[self]
43 @classmethod
44 def from_string(cls, granularity_str: str) -> 'Granularity':
45 """
46 Converts string representation of granularity to Granularity enum.
48 Parameters:
49 granularity_str (str): String representation of granularity.
51 Returns:
52 Granularity: Corresponding Granularity enum.
53 """
55 for granularity in cls:
56 if granularity_str == str(granularity):
57 return granularity
58 raise ValueError(f"Invalid granularity string: {granularity_str}")