diff --git a/pyasic/data/device.py b/pyasic/data/device.py index fe586c7c..5f53b493 100644 --- a/pyasic/data/device.py +++ b/pyasic/data/device.py @@ -12,7 +12,7 @@ class DeviceInfo(BaseModel): make: MinerMake | None = None model: MinerModelType | None = None firmware: MinerFirmware | None = None - algo: MinerAlgoType | None = None + algo: type[MinerAlgoType] | None = None @field_serializer("make") def serialize_make(self, make: MinerMake, _info): diff --git a/pyasic/device/algorithm/base.py b/pyasic/device/algorithm/base.py index f17e5be2..3212c5b7 100644 --- a/pyasic/device/algorithm/base.py +++ b/pyasic/device/algorithm/base.py @@ -4,6 +4,13 @@ from .hashrate.base import AlgoHashRateType from .hashrate.unit.base import AlgoHashRateUnitType -class MinerAlgoType(str): +class MinerAlgoMeta(type): + name: str + + def __str__(cls): + return cls.name + + +class MinerAlgoType(metaclass=MinerAlgoMeta): hashrate: type[AlgoHashRateType] unit: type[AlgoHashRateUnitType] diff --git a/pyasic/device/algorithm/blake256.py b/pyasic/device/algorithm/blake256.py index de3792db..462c5ef8 100644 --- a/pyasic/device/algorithm/blake256.py +++ b/pyasic/device/algorithm/blake256.py @@ -6,12 +6,8 @@ from .hashrate.unit import Blake256Unit # make this json serializable -class _Blake256Algo(MinerAlgoType): - hashrate = Blake256HashRate - unit = Blake256Unit +class Blake256Algo(MinerAlgoType): + hashrate: type[Blake256HashRate] = Blake256HashRate + unit: type[Blake256Unit] = Blake256Unit - def __repr__(self): - return "Blake256Algo" - - -Blake256Algo = _Blake256Algo("Blake256") + name = "Blake256" diff --git a/pyasic/device/algorithm/eaglesong.py b/pyasic/device/algorithm/eaglesong.py index 8e2fc0b4..c2ba02ab 100644 --- a/pyasic/device/algorithm/eaglesong.py +++ b/pyasic/device/algorithm/eaglesong.py @@ -6,12 +6,8 @@ from .hashrate.unit import EaglesongUnit # make this json serializable -class _EaglesongAlgo(MinerAlgoType): - hashrate = EaglesongHashRate - unit = EaglesongUnit +class EaglesongAlgo(MinerAlgoType): + hashrate: type[EaglesongHashRate] = EaglesongHashRate + unit: type[EaglesongUnit] = EaglesongUnit - def __repr__(self): - return "EaglesongAlgo" - - -EaglesongAlgo = _EaglesongAlgo("Eaglesong") + name = "Eaglesong" diff --git a/pyasic/device/algorithm/equihash.py b/pyasic/device/algorithm/equihash.py index 5e9b3270..cb76cff6 100644 --- a/pyasic/device/algorithm/equihash.py +++ b/pyasic/device/algorithm/equihash.py @@ -6,12 +6,8 @@ from .hashrate.unit import EquihashUnit # make this json serializable -class _EquihashAlgo(MinerAlgoType): - hashrate = EquihashHashRate - unit = EquihashUnit +class EquihashAlgo(MinerAlgoType): + hashrate: type[EquihashHashRate] = EquihashHashRate + unit: type[EquihashUnit] = EquihashUnit - def __repr__(self): - return "EquihashAlgo" - - -EquihashAlgo = _EquihashAlgo("Equihash") + name = "Equihash" diff --git a/pyasic/device/algorithm/ethash.py b/pyasic/device/algorithm/ethash.py index f27164be..93cdb65f 100644 --- a/pyasic/device/algorithm/ethash.py +++ b/pyasic/device/algorithm/ethash.py @@ -5,13 +5,8 @@ from .hashrate import EtHashHashRate from .hashrate.unit import EtHashUnit -# make this json serializable -class _EtHashAlgo(MinerAlgoType): - hashrate = EtHashHashRate - unit = EtHashUnit +class EtHashAlgo(MinerAlgoType): + hashrate: type[EtHashHashRate] = EtHashHashRate + unit: type[EtHashUnit] = EtHashUnit - def __repr__(self): - return "EtHashAlgo" - - -EtHashAlgo = _EtHashAlgo("EtHash") + name = "EtHash" diff --git a/pyasic/device/algorithm/handshake.py b/pyasic/device/algorithm/handshake.py index f25016d7..d7e28b13 100644 --- a/pyasic/device/algorithm/handshake.py +++ b/pyasic/device/algorithm/handshake.py @@ -6,12 +6,8 @@ from .hashrate.unit import HandshakeUnit # make this json serializable -class _HandshakeAlgo(MinerAlgoType): - hashrate = HandshakeHashRate - unit = HandshakeUnit +class HandshakeAlgo(MinerAlgoType): + hashrate: type[HandshakeHashRate] = HandshakeHashRate + unit: type[HandshakeUnit] = HandshakeUnit - def __repr__(self): - return "HandshakeAlgo" - - -HandshakeAlgo = _HandshakeAlgo("Handshake") + name = "Handshake" diff --git a/pyasic/device/algorithm/hashrate/base.py b/pyasic/device/algorithm/hashrate/base.py index 2676d7cc..381a2a55 100644 --- a/pyasic/device/algorithm/hashrate/base.py +++ b/pyasic/device/algorithm/hashrate/base.py @@ -1,6 +1,7 @@ from abc import ABC, abstractmethod +from typing import Self -from pydantic import BaseModel +from pydantic import BaseModel, Field from .unit.base import AlgoHashRateUnitType @@ -24,3 +25,38 @@ class AlgoHashRateType(BaseModel, ABC): def __round__(self, n: int = None): return round(self.rate, n) + + def __add__(self, other: Self | int | float) -> Self: + if isinstance(other, self.__class__): + return self.__class__( + rate=self.rate + other.into(self.unit).rate, unit=self.unit + ) + return self.__class__(rate=self.rate + other, unit=self.unit) + + def __sub__(self, other: Self | int | float) -> Self: + if isinstance(other, self.__class__): + return self.__class__( + rate=self.rate - other.into(self.unit).rate, unit=self.unit + ) + return self.__class__(rate=self.rate - other, unit=self.unit) + + def __truediv__(self, other: Self | int | float) -> Self: + if isinstance(other, self.__class__): + return self.__class__( + rate=self.rate / other.into(self.unit).rate, unit=self.unit + ) + return self.__class__(rate=self.rate / other, unit=self.unit) + + def __floordiv__(self, other: Self | int | float) -> Self: + if isinstance(other, self.__class__): + return self.__class__( + rate=self.rate // other.into(self.unit).rate, unit=self.unit + ) + return self.__class__(rate=self.rate // other, unit=self.unit) + + def __mul__(self, other: Self | int | float) -> Self: + if isinstance(other, self.__class__): + return self.__class__( + rate=self.rate * other.into(self.unit).rate, unit=self.unit + ) + return self.__class__(rate=self.rate * other, unit=self.unit) diff --git a/pyasic/device/algorithm/hashrate/blake256.py b/pyasic/device/algorithm/hashrate/blake256.py index 46e83ed5..70377c74 100644 --- a/pyasic/device/algorithm/hashrate/blake256.py +++ b/pyasic/device/algorithm/hashrate/blake256.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.blake256 import Blake256Unit @@ -10,42 +12,7 @@ class Blake256HashRate(AlgoHashRateType): rate: float unit: Blake256Unit = HashUnit.BLAKE256.default - def __add__(self, other: Blake256HashRate | int | float) -> Blake256HashRate: - if isinstance(other, Blake256HashRate): - return Blake256HashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return Blake256HashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: Blake256HashRate | int | float) -> Blake256HashRate: - if isinstance(other, Blake256HashRate): - return Blake256HashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return Blake256HashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: Blake256HashRate | int | float): - if isinstance(other, Blake256HashRate): - return Blake256HashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return Blake256HashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: Blake256HashRate | int | float): - if isinstance(other, Blake256HashRate): - return Blake256HashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return Blake256HashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: Blake256HashRate | int | float): - if isinstance(other, Blake256HashRate): - return Blake256HashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return Blake256HashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: Blake256Unit) -> Blake256HashRate: - return Blake256HashRate( + def into(self, other: Blake256Unit) -> Self: + return self.__class__( rate=self.rate / (other.value / self.unit.value), unit=other ) diff --git a/pyasic/device/algorithm/hashrate/eaglesong.py b/pyasic/device/algorithm/hashrate/eaglesong.py index 70b83620..ed9926be 100644 --- a/pyasic/device/algorithm/hashrate/eaglesong.py +++ b/pyasic/device/algorithm/hashrate/eaglesong.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.eaglesong import EaglesongUnit @@ -10,42 +12,7 @@ class EaglesongHashRate(AlgoHashRateType): rate: float unit: EaglesongUnit = HashUnit.EAGLESONG.default - def __add__(self, other: EaglesongHashRate | int | float) -> EaglesongHashRate: - if isinstance(other, EaglesongHashRate): - return EaglesongHashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return EaglesongHashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: EaglesongHashRate | int | float) -> EaglesongHashRate: - if isinstance(other, EaglesongHashRate): - return EaglesongHashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return EaglesongHashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: EaglesongHashRate | int | float): - if isinstance(other, EaglesongHashRate): - return EaglesongHashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return EaglesongHashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: EaglesongHashRate | int | float): - if isinstance(other, EaglesongHashRate): - return EaglesongHashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return EaglesongHashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: EaglesongHashRate | int | float): - if isinstance(other, EaglesongHashRate): - return EaglesongHashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return EaglesongHashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: EaglesongUnit) -> EaglesongHashRate: - return EaglesongHashRate( + def into(self, other: EaglesongUnit) -> Self: + return self.__class__( rate=self.rate / (other.value / self.unit.value), unit=other ) diff --git a/pyasic/device/algorithm/hashrate/equihash.py b/pyasic/device/algorithm/hashrate/equihash.py index a3458121..69a238db 100644 --- a/pyasic/device/algorithm/hashrate/equihash.py +++ b/pyasic/device/algorithm/hashrate/equihash.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.equihash import EquihashUnit @@ -10,42 +12,7 @@ class EquihashHashRate(AlgoHashRateType): rate: float unit: EquihashUnit = HashUnit.ETHASH.default - def __add__(self, other: EquihashHashRate | int | float) -> EquihashHashRate: - if isinstance(other, EquihashHashRate): - return EquihashHashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return EquihashHashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: EquihashHashRate | int | float) -> EquihashHashRate: - if isinstance(other, EquihashHashRate): - return EquihashHashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return EquihashHashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: EquihashHashRate | int | float): - if isinstance(other, EquihashHashRate): - return EquihashHashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return EquihashHashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: EquihashHashRate | int | float): - if isinstance(other, EquihashHashRate): - return EquihashHashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return EquihashHashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: EquihashHashRate | int | float): - if isinstance(other, EquihashHashRate): - return EquihashHashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return EquihashHashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: EquihashUnit) -> EquihashHashRate: - return EquihashHashRate( + def into(self, other: EquihashUnit) -> Self: + return self.__class__( rate=self.rate / (other.value / self.unit.value), unit=other ) diff --git a/pyasic/device/algorithm/hashrate/ethash.py b/pyasic/device/algorithm/hashrate/ethash.py index dadefdfe..86e4b02b 100644 --- a/pyasic/device/algorithm/hashrate/ethash.py +++ b/pyasic/device/algorithm/hashrate/ethash.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.ethash import EtHashUnit @@ -10,42 +12,7 @@ class EtHashHashRate(AlgoHashRateType): rate: float unit: EtHashUnit = HashUnit.ETHASH.default - def __add__(self, other: EtHashHashRate | int | float) -> EtHashHashRate: - if isinstance(other, EtHashHashRate): - return EtHashHashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return EtHashHashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: EtHashHashRate | int | float) -> EtHashHashRate: - if isinstance(other, EtHashHashRate): - return EtHashHashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return EtHashHashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: EtHashHashRate | int | float): - if isinstance(other, EtHashHashRate): - return EtHashHashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return EtHashHashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: EtHashHashRate | int | float): - if isinstance(other, EtHashHashRate): - return EtHashHashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return EtHashHashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: EtHashHashRate | int | float): - if isinstance(other, EtHashHashRate): - return EtHashHashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return EtHashHashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: EtHashUnit) -> EtHashHashRate: - return EtHashHashRate( + def into(self, other: EtHashUnit) -> Self: + return self.__class__( rate=self.rate / (other.value / self.unit.value), unit=other ) diff --git a/pyasic/device/algorithm/hashrate/handshake.py b/pyasic/device/algorithm/hashrate/handshake.py index ddb7345e..bab4efe4 100644 --- a/pyasic/device/algorithm/hashrate/handshake.py +++ b/pyasic/device/algorithm/hashrate/handshake.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.handshake import HandshakeUnit @@ -10,42 +12,7 @@ class HandshakeHashRate(AlgoHashRateType): rate: float unit: HandshakeUnit = HashUnit.HANDSHAKE.default - def __add__(self, other: HandshakeHashRate | int | float) -> HandshakeHashRate: - if isinstance(other, HandshakeHashRate): - return HandshakeHashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return HandshakeHashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: HandshakeHashRate | int | float) -> HandshakeHashRate: - if isinstance(other, HandshakeHashRate): - return HandshakeHashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return HandshakeHashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: HandshakeHashRate | int | float): - if isinstance(other, HandshakeHashRate): - return HandshakeHashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return HandshakeHashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: HandshakeHashRate | int | float): - if isinstance(other, HandshakeHashRate): - return HandshakeHashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return HandshakeHashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: HandshakeHashRate | int | float): - if isinstance(other, HandshakeHashRate): - return HandshakeHashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return HandshakeHashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: HandshakeUnit) -> HandshakeHashRate: - return HandshakeHashRate( + def into(self, other: HandshakeUnit) -> Self: + return self.__class__( rate=self.rate / (other.value / self.unit.value), unit=other ) diff --git a/pyasic/device/algorithm/hashrate/kadena.py b/pyasic/device/algorithm/hashrate/kadena.py index aabad6ee..4e487311 100644 --- a/pyasic/device/algorithm/hashrate/kadena.py +++ b/pyasic/device/algorithm/hashrate/kadena.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.kadena import KadenaUnit @@ -10,42 +12,7 @@ class KadenaHashRate(AlgoHashRateType): rate: float unit: KadenaUnit = HashUnit.KADENA.default - def __add__(self, other: KadenaHashRate | int | float) -> KadenaHashRate: - if isinstance(other, KadenaHashRate): - return KadenaHashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return KadenaHashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: KadenaHashRate | int | float) -> KadenaHashRate: - if isinstance(other, KadenaHashRate): - return KadenaHashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return KadenaHashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: KadenaHashRate | int | float): - if isinstance(other, KadenaHashRate): - return KadenaHashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return KadenaHashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: KadenaHashRate | int | float): - if isinstance(other, KadenaHashRate): - return KadenaHashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return KadenaHashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: KadenaHashRate | int | float): - if isinstance(other, KadenaHashRate): - return KadenaHashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return KadenaHashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: KadenaUnit) -> KadenaHashRate: - return KadenaHashRate( + def into(self, other: KadenaUnit) -> Self: + return self.__class__( rate=self.rate / (other.value / self.unit.value), unit=other ) diff --git a/pyasic/device/algorithm/hashrate/kheavyhash.py b/pyasic/device/algorithm/hashrate/kheavyhash.py index 2cd14e17..9932af4b 100644 --- a/pyasic/device/algorithm/hashrate/kheavyhash.py +++ b/pyasic/device/algorithm/hashrate/kheavyhash.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.kheavyhash import KHeavyHashUnit @@ -10,42 +12,7 @@ class KHeavyHashHashRate(AlgoHashRateType): rate: float unit: KHeavyHashUnit = HashUnit.KHEAVYHASH.default - def __add__(self, other: KHeavyHashHashRate | int | float) -> KHeavyHashHashRate: - if isinstance(other, KHeavyHashHashRate): - return KHeavyHashHashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return KHeavyHashHashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: KHeavyHashHashRate | int | float) -> KHeavyHashHashRate: - if isinstance(other, KHeavyHashHashRate): - return KHeavyHashHashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return KHeavyHashHashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: KHeavyHashHashRate | int | float): - if isinstance(other, KHeavyHashHashRate): - return KHeavyHashHashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return KHeavyHashHashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: KHeavyHashHashRate | int | float): - if isinstance(other, KHeavyHashHashRate): - return KHeavyHashHashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return KHeavyHashHashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: KHeavyHashHashRate | int | float): - if isinstance(other, KHeavyHashHashRate): - return KHeavyHashHashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return KHeavyHashHashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: KHeavyHashUnit) -> KHeavyHashHashRate: - return KHeavyHashHashRate( + def into(self, other: KHeavyHashUnit) -> Self: + return self.__class__( rate=self.rate / (other.value / self.unit.value), unit=other ) diff --git a/pyasic/device/algorithm/hashrate/scrypt.py b/pyasic/device/algorithm/hashrate/scrypt.py index 9dc59d89..e69219e8 100644 --- a/pyasic/device/algorithm/hashrate/scrypt.py +++ b/pyasic/device/algorithm/hashrate/scrypt.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.scrypt import ScryptUnit @@ -10,42 +12,7 @@ class ScryptHashRate(AlgoHashRateType): rate: float unit: ScryptUnit = HashUnit.SCRYPT.default - def __add__(self, other: ScryptHashRate | int | float) -> ScryptHashRate: - if isinstance(other, ScryptHashRate): - return ScryptHashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return ScryptHashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: ScryptHashRate | int | float) -> ScryptHashRate: - if isinstance(other, ScryptHashRate): - return ScryptHashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return ScryptHashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: ScryptHashRate | int | float): - if isinstance(other, ScryptHashRate): - return ScryptHashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return ScryptHashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: ScryptHashRate | int | float): - if isinstance(other, ScryptHashRate): - return ScryptHashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return ScryptHashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: ScryptHashRate | int | float): - if isinstance(other, ScryptHashRate): - return ScryptHashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return ScryptHashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: ScryptUnit) -> ScryptHashRate: - return ScryptHashRate( + def into(self, other: ScryptUnit) -> Self: + return self.__class__( rate=self.rate / (other.value / self.unit.value), unit=other ) diff --git a/pyasic/device/algorithm/hashrate/sha256.py b/pyasic/device/algorithm/hashrate/sha256.py index 732f7d71..96b7cd7e 100644 --- a/pyasic/device/algorithm/hashrate/sha256.py +++ b/pyasic/device/algorithm/hashrate/sha256.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.sha256 import SHA256Unit @@ -10,42 +12,7 @@ class SHA256HashRate(AlgoHashRateType): rate: float unit: SHA256Unit = HashUnit.SHA256.default - def __add__(self, other: SHA256HashRate | int | float) -> SHA256HashRate: - if isinstance(other, SHA256HashRate): - return SHA256HashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return SHA256HashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: SHA256HashRate | int | float) -> SHA256HashRate: - if isinstance(other, SHA256HashRate): - return SHA256HashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return SHA256HashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: SHA256HashRate | int | float): - if isinstance(other, SHA256HashRate): - return SHA256HashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return SHA256HashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: SHA256HashRate | int | float): - if isinstance(other, SHA256HashRate): - return SHA256HashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return SHA256HashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: SHA256HashRate | int | float): - if isinstance(other, SHA256HashRate): - return SHA256HashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return SHA256HashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: SHA256Unit) -> SHA256HashRate: - return SHA256HashRate( + def into(self, other: SHA256Unit) -> Self: + return self.__class__( rate=self.rate / (other.value / self.unit.value), unit=other ) diff --git a/pyasic/device/algorithm/hashrate/x11.py b/pyasic/device/algorithm/hashrate/x11.py index 859e5882..cb980512 100644 --- a/pyasic/device/algorithm/hashrate/x11.py +++ b/pyasic/device/algorithm/hashrate/x11.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Self + from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.unit.x11 import X11Unit @@ -10,40 +12,7 @@ class X11HashRate(AlgoHashRateType): rate: float unit: X11Unit = HashUnit.X11.default - def __add__(self, other: X11HashRate | int | float) -> X11HashRate: - if isinstance(other, X11HashRate): - return X11HashRate( - rate=self.rate + other.into(self.unit).rate, unit=self.unit - ) - return X11HashRate(rate=self.rate + other, unit=self.unit) - - def __sub__(self, other: X11HashRate | int | float) -> X11HashRate: - if isinstance(other, X11HashRate): - return X11HashRate( - rate=self.rate - other.into(self.unit).rate, unit=self.unit - ) - return X11HashRate(rate=self.rate - other, unit=self.unit) - - def __truediv__(self, other: X11HashRate | int | float): - if isinstance(other, X11HashRate): - return X11HashRate( - rate=self.rate / other.into(self.unit).rate, unit=self.unit - ) - return X11HashRate(rate=self.rate / other, unit=self.unit) - - def __floordiv__(self, other: X11HashRate | int | float): - if isinstance(other, X11HashRate): - return X11HashRate( - rate=self.rate // other.into(self.unit).rate, unit=self.unit - ) - return X11HashRate(rate=self.rate // other, unit=self.unit) - - def __mul__(self, other: X11HashRate | int | float): - if isinstance(other, X11HashRate): - return X11HashRate( - rate=self.rate * other.into(self.unit).rate, unit=self.unit - ) - return X11HashRate(rate=self.rate * other, unit=self.unit) - - def into(self, other: X11Unit) -> X11HashRate: - return X11HashRate(rate=self.rate / (other.value / self.unit.value), unit=other) + def into(self, other: X11Unit) -> Self: + return self.__class__( + rate=self.rate / (other.value / self.unit.value), unit=other + ) diff --git a/pyasic/device/algorithm/kadena.py b/pyasic/device/algorithm/kadena.py index 3f9a7217..7a00a394 100644 --- a/pyasic/device/algorithm/kadena.py +++ b/pyasic/device/algorithm/kadena.py @@ -6,12 +6,8 @@ from .hashrate.unit import KadenaUnit # make this json serializable -class _KadenaAlgo(MinerAlgoType): - hashrate = KadenaHashRate - unit = KadenaUnit +class KadenaAlgo(MinerAlgoType): + hashrate: type[KadenaHashRate] = KadenaHashRate + unit: type[KadenaUnit] = KadenaUnit - def __repr__(self): - return "KadenaAlgo" - - -KadenaAlgo = _KadenaAlgo("Kadena") + name = "Kadena" diff --git a/pyasic/device/algorithm/kheavyhash.py b/pyasic/device/algorithm/kheavyhash.py index aed5e136..9489bd42 100644 --- a/pyasic/device/algorithm/kheavyhash.py +++ b/pyasic/device/algorithm/kheavyhash.py @@ -6,12 +6,8 @@ from .hashrate.unit import KHeavyHashUnit # make this json serializable -class _KHeavyHashAlgo(MinerAlgoType): - hashrate = KHeavyHashHashRate - unit = KHeavyHashUnit +class KHeavyHashAlgo(MinerAlgoType): + hashrate: type[KHeavyHashHashRate] = KHeavyHashHashRate + unit: type[KHeavyHashUnit] = KHeavyHashUnit - def __repr__(self): - return "KHeavyHashAlgo" - - -KHeavyHashAlgo = _KHeavyHashAlgo("KHeavyHash") + name = "KHeavyHash" diff --git a/pyasic/device/algorithm/scrypt.py b/pyasic/device/algorithm/scrypt.py index ee85b37e..ff9422a2 100644 --- a/pyasic/device/algorithm/scrypt.py +++ b/pyasic/device/algorithm/scrypt.py @@ -5,13 +5,8 @@ from .hashrate import ScryptHashRate from .hashrate.unit import ScryptUnit -# make this json serializable -class _ScryptAlgo(MinerAlgoType): - hashrate = ScryptHashRate - unit = ScryptUnit +class ScryptAlgo(MinerAlgoType): + hashrate: type[ScryptHashRate] = ScryptHashRate + unit: type[ScryptUnit] = ScryptUnit - def __repr__(self): - return "ScryptAlgo" - - -ScryptAlgo = _ScryptAlgo("Scrypt") + name = "Scrypt" diff --git a/pyasic/device/algorithm/sha256.py b/pyasic/device/algorithm/sha256.py index 50d58df3..28dbe89b 100644 --- a/pyasic/device/algorithm/sha256.py +++ b/pyasic/device/algorithm/sha256.py @@ -6,12 +6,8 @@ from .hashrate.unit import SHA256Unit # make this json serializable -class _SHA256Algo(MinerAlgoType): - hashrate = SHA256HashRate - unit = SHA256Unit +class SHA256Algo(MinerAlgoType): + hashrate: type[SHA256HashRate] = SHA256HashRate + unit: type[SHA256Unit] = SHA256Unit - def __repr__(self): - return "SHA256Algo" - - -SHA256Algo = _SHA256Algo("SHA256") + name = "SHA256" diff --git a/pyasic/device/algorithm/x11.py b/pyasic/device/algorithm/x11.py index dbb80665..10669e87 100644 --- a/pyasic/device/algorithm/x11.py +++ b/pyasic/device/algorithm/x11.py @@ -6,12 +6,8 @@ from .hashrate.unit import X11Unit # make this json serializable -class _X11Algo(MinerAlgoType): - hashrate = X11HashRate - unit = X11Unit +class X11Algo(MinerAlgoType): + hashrate: type[X11HashRate] = X11HashRate + unit: type[X11Unit] = X11Unit - def __repr__(self): - return "X11Algo" - - -X11Algo = _X11Algo("X11") + name = "X11" diff --git a/pyasic/miners/base.py b/pyasic/miners/base.py index 14b2c792..ceaab3b3 100644 --- a/pyasic/miners/base.py +++ b/pyasic/miners/base.py @@ -46,7 +46,7 @@ class MinerProtocol(Protocol): make: MinerMake = None raw_model: MinerModelType = None firmware: MinerFirmware = None - algo: MinerAlgoType = None + algo: type[MinerAlgoType] = None expected_hashboards: int = None expected_chips: int = None @@ -496,7 +496,6 @@ class MinerProtocol(Protocol): function = getattr(self, getattr(self.data_locations, data_name).cmd) miner_data[data_name] = await function(**args_to_send) except Exception as e: - raise e raise APIError( f"Failed to call {data_name} on {self} while getting data." ) from e