Skip to content

Commit 53716ff

Browse files
authored
Merge pull request #55 from DataCrunch-io/instance-types-dataclass
InstanceType dataclass
2 parents 2febc41 + 9babb3c commit 53716ff

File tree

2 files changed

+37
-159
lines changed

2 files changed

+37
-159
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Added shared filesystem (SFS) type constant and example
1313

14+
### Changed
15+
16+
- Refactor `instance_types.py` to use dataclass
17+
1418
## [1.16.0] - 2025-10-27
1519

1620
### Changed

datacrunch/instance_types/instance_types.py

Lines changed: 33 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,38 @@
1-
INSTANCE_TYPES_ENDPOINT = '/instance-types'
2-
3-
4-
class InstanceType:
5-
"""Instance type."""
6-
7-
def __init__(
8-
self,
9-
id: str,
10-
instance_type: str,
11-
price_per_hour: float,
12-
spot_price_per_hour: float,
13-
description: str,
14-
cpu: dict,
15-
gpu: dict,
16-
memory: dict,
17-
gpu_memory: dict,
18-
storage: dict,
19-
) -> None:
20-
"""Initialize an instance type object.
21-
22-
:param id: instance type id
23-
:type id: str
24-
:param instance_type: instance type. e.g. '8V100.48M'
25-
:type instance_type: str
26-
:param price_per_hour: price per hour
27-
:type price_per_hour: float
28-
:param spot_price_per_hour: spot price per hour
29-
:type spot_price_per_hour: float
30-
:param description: instance type description
31-
:type description: str
32-
:param cpu: cpu details
33-
:type cpu: dict
34-
:param gpu: gpu details
35-
:type gpu: dict
36-
:param memory: memory details
37-
:type memory: dict
38-
:param gpu_memory: gpu memory details
39-
:type gpu_memory: dict
40-
:param storage: storage details
41-
:type storage: dict
42-
"""
43-
self._id = id
44-
self._instance_type = instance_type
45-
self._price_per_hour = float(price_per_hour)
46-
self._spot_price_per_hour = float(spot_price_per_hour)
47-
self._description = description
48-
self._cpu = cpu
49-
self._gpu = gpu
50-
self._memory = memory
51-
self._gpu_memory = gpu_memory
52-
self._storage = storage
53-
54-
@property
55-
def id(self) -> str:
56-
"""Get the instance type id.
57-
58-
:return: instance type id
59-
:rtype: str
60-
"""
61-
return self._id
62-
63-
@property
64-
def instance_type(self) -> str:
65-
"""Get the instance type.
66-
67-
:return: instance type. e.g. '8V100.48M'
68-
:rtype: str
69-
"""
70-
return self._instance_type
71-
72-
@property
73-
def price_per_hour(self) -> float:
74-
"""Get the instance type price per hour.
75-
76-
:return: price per hour
77-
:rtype: float
78-
"""
79-
return self._price_per_hour
80-
81-
@property
82-
def spot_price_per_hour(self) -> float:
83-
"""Get the instance spot price per hour.
84-
85-
:return: spot price per hour
86-
:rtype: float
87-
"""
88-
return self._spot_price_per_hour
1+
from dataclasses import dataclass
892

90-
@property
91-
def description(self) -> str:
92-
"""Get the instance type description.
3+
from dataclasses_json import dataclass_json
934

94-
:return: instance type description
95-
:rtype: str
96-
"""
97-
return self._description
98-
99-
@property
100-
def cpu(self) -> dict:
101-
"""Get the instance type cpu details.
102-
103-
:return: cpu details
104-
:rtype: dict
105-
"""
106-
return self._cpu
107-
108-
@property
109-
def gpu(self) -> dict:
110-
"""Get the instance type gpu details.
111-
112-
:return: gpu details
113-
:rtype: dict
114-
"""
115-
return self._gpu
116-
117-
@property
118-
def memory(self) -> dict:
119-
"""Get the instance type memory details.
120-
121-
:return: memory details
122-
:rtype: dict
123-
"""
124-
return self._memory
125-
126-
@property
127-
def gpu_memory(self) -> dict:
128-
"""Get the instance type gpu_memory details.
129-
130-
:return: gpu_memory details
131-
:rtype: dict
132-
"""
133-
return self._gpu_memory
134-
135-
@property
136-
def storage(self) -> dict:
137-
"""Get the instance type storage details.
138-
139-
:return: storage details
140-
:rtype: dict
141-
"""
142-
return self._storage
5+
INSTANCE_TYPES_ENDPOINT = '/instance-types'
1436

144-
def __str__(self) -> str:
145-
"""Prints the instance type.
1467

147-
:return: instance type string representation
148-
:rtype: str
149-
"""
150-
return (
151-
f'id: {self._id}\n'
152-
f'instance type: {self._instance_type}\n'
153-
f'price_per_hour: ${self._price_per_hour}\n'
154-
f'spot_price_per_hour: ${self._spot_price_per_hour}\n'
155-
f'description: {self._description}\n'
156-
f'cpu: {self._cpu}\n'
157-
f'gpu: {self._gpu}\n'
158-
f'memory :{self._memory}\n'
159-
f'gpu_memory :{self._gpu_memory}\n'
160-
f'storage :{self._storage}\n'
161-
)
8+
@dataclass_json
9+
@dataclass
10+
class InstanceType:
11+
"""Instance type.
12+
13+
Attributes:
14+
id: instance type id.
15+
instance_type: instance type, e.g. '8V100.48M'.
16+
price_per_hour: instance type price per hour.
17+
spot_price_per_hour: instance type spot price per hour.
18+
description: instance type description.
19+
cpu: instance type cpu details.
20+
gpu: instance type gpu details.
21+
memory: instance type memory details.
22+
gpu_memory: instance type gpu memory details.
23+
storage: instance type storage details.
24+
"""
25+
26+
id: str
27+
instance_type: str
28+
price_per_hour: float
29+
spot_price_per_hour: float
30+
description: str
31+
cpu: dict
32+
gpu: dict
33+
memory: dict
34+
gpu_memory: dict
35+
storage: dict
16236

16337

16438
class InstanceTypesService:
@@ -178,8 +52,8 @@ def get(self) -> list[InstanceType]:
17852
InstanceType(
17953
id=instance_type['id'],
18054
instance_type=instance_type['instance_type'],
181-
price_per_hour=instance_type['price_per_hour'],
182-
spot_price_per_hour=instance_type['spot_price'],
55+
price_per_hour=float(instance_type['price_per_hour']),
56+
spot_price_per_hour=float(instance_type['spot_price']),
18357
description=instance_type['description'],
18458
cpu=instance_type['cpu'],
18559
gpu=instance_type['gpu'],

0 commit comments

Comments
 (0)