From af2dc449a9d1cfe57d7b0082697cf1ad884e712a Mon Sep 17 00:00:00 2001 From: Alexey Shamrin Date: Fri, 7 Nov 2025 14:36:54 +0200 Subject: [PATCH 1/2] InstanceType dataclass --- datacrunch/instance_types/instance_types.py | 192 ++++---------------- 1 file changed, 33 insertions(+), 159 deletions(-) diff --git a/datacrunch/instance_types/instance_types.py b/datacrunch/instance_types/instance_types.py index ba7ab03..322174d 100644 --- a/datacrunch/instance_types/instance_types.py +++ b/datacrunch/instance_types/instance_types.py @@ -1,164 +1,38 @@ -INSTANCE_TYPES_ENDPOINT = '/instance-types' - - -class InstanceType: - """Instance type.""" - - def __init__( - self, - id: str, - instance_type: str, - price_per_hour: float, - spot_price_per_hour: float, - description: str, - cpu: dict, - gpu: dict, - memory: dict, - gpu_memory: dict, - storage: dict, - ) -> None: - """Initialize an instance type object. - - :param id: instance type id - :type id: str - :param instance_type: instance type. e.g. '8V100.48M' - :type instance_type: str - :param price_per_hour: price per hour - :type price_per_hour: float - :param spot_price_per_hour: spot price per hour - :type spot_price_per_hour: float - :param description: instance type description - :type description: str - :param cpu: cpu details - :type cpu: dict - :param gpu: gpu details - :type gpu: dict - :param memory: memory details - :type memory: dict - :param gpu_memory: gpu memory details - :type gpu_memory: dict - :param storage: storage details - :type storage: dict - """ - self._id = id - self._instance_type = instance_type - self._price_per_hour = float(price_per_hour) - self._spot_price_per_hour = float(spot_price_per_hour) - self._description = description - self._cpu = cpu - self._gpu = gpu - self._memory = memory - self._gpu_memory = gpu_memory - self._storage = storage - - @property - def id(self) -> str: - """Get the instance type id. - - :return: instance type id - :rtype: str - """ - return self._id - - @property - def instance_type(self) -> str: - """Get the instance type. - - :return: instance type. e.g. '8V100.48M' - :rtype: str - """ - return self._instance_type - - @property - def price_per_hour(self) -> float: - """Get the instance type price per hour. - - :return: price per hour - :rtype: float - """ - return self._price_per_hour - - @property - def spot_price_per_hour(self) -> float: - """Get the instance spot price per hour. - - :return: spot price per hour - :rtype: float - """ - return self._spot_price_per_hour +from dataclasses import dataclass - @property - def description(self) -> str: - """Get the instance type description. +from dataclasses_json import dataclass_json - :return: instance type description - :rtype: str - """ - return self._description - - @property - def cpu(self) -> dict: - """Get the instance type cpu details. - - :return: cpu details - :rtype: dict - """ - return self._cpu - - @property - def gpu(self) -> dict: - """Get the instance type gpu details. - - :return: gpu details - :rtype: dict - """ - return self._gpu - - @property - def memory(self) -> dict: - """Get the instance type memory details. - - :return: memory details - :rtype: dict - """ - return self._memory - - @property - def gpu_memory(self) -> dict: - """Get the instance type gpu_memory details. - - :return: gpu_memory details - :rtype: dict - """ - return self._gpu_memory - - @property - def storage(self) -> dict: - """Get the instance type storage details. - - :return: storage details - :rtype: dict - """ - return self._storage +INSTANCE_TYPES_ENDPOINT = '/instance-types' - def __str__(self) -> str: - """Prints the instance type. - :return: instance type string representation - :rtype: str - """ - return ( - f'id: {self._id}\n' - f'instance type: {self._instance_type}\n' - f'price_per_hour: ${self._price_per_hour}\n' - f'spot_price_per_hour: ${self._spot_price_per_hour}\n' - f'description: {self._description}\n' - f'cpu: {self._cpu}\n' - f'gpu: {self._gpu}\n' - f'memory :{self._memory}\n' - f'gpu_memory :{self._gpu_memory}\n' - f'storage :{self._storage}\n' - ) +@dataclass_json +@dataclass +class InstanceType: + """Instance type. + + Attributes: + id: instance type id. + instance_type: instance type, e.g. '8V100.48M'. + price_per_hour: instance type price per hour. + spot_price_per_hour: instance type spot price per hour. + description: instance type description. + cpu: instance type cpu details. + gpu: instance type gpu details. + memory: instance type memory details. + gpu_memory: instance type gpu memory details. + storage: instance type storage details. + """ + + id: str + instance_type: str + price_per_hour: float + spot_price_per_hour: float + description: str + cpu: dict + gpu: dict + memory: dict + gpu_memory: dict + storage: dict class InstanceTypesService: @@ -178,8 +52,8 @@ def get(self) -> list[InstanceType]: InstanceType( id=instance_type['id'], instance_type=instance_type['instance_type'], - price_per_hour=instance_type['price_per_hour'], - spot_price_per_hour=instance_type['spot_price'], + price_per_hour=float(instance_type['price_per_hour']), + spot_price_per_hour=float(instance_type['spot_price']), description=instance_type['description'], cpu=instance_type['cpu'], gpu=instance_type['gpu'], From 9babb3c7fb84d9e974e245ceac15bdd376fdb606 Mon Sep 17 00:00:00 2001 From: Alexey Shamrin Date: Mon, 17 Nov 2025 22:40:14 +0200 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91f9538..2fc63ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added shared filesystem (SFS) type constant and example +### Changed + +- Refactor `instance_types.py` to use dataclass + ## [1.16.0] - 2025-10-27 ### Changed