Add the ability to add MinerData together and sum them, as well as compute division and floor division on them.

This commit is contained in:
UpstreamData
2022-09-30 16:00:45 -06:00
parent d3a5517fa9
commit 73b1a0493c
3 changed files with 59 additions and 7 deletions

View File

@@ -17,6 +17,7 @@ from dataclasses import dataclass, field, asdict
from datetime import datetime, timezone
import time
import json
import copy
from .error_codes import X19Error, WhatsminerError, BraiinsOSError, InnosiliconError
@@ -121,6 +122,43 @@ class MinerData:
def __iter__(self):
return iter([item for item in self.asdict()])
def __truediv__(self, other):
return self // other
def __floordiv__(self, other):
cp = copy.deepcopy(self)
for key in self:
item = getattr(self, key)
if isinstance(item, int):
setattr(cp, key, item // other)
if isinstance(item, float):
setattr(cp, key, round(item / other, 2))
return cp
def __add__(self, other):
if not isinstance(other, MinerData):
raise TypeError("Cannot add MinerData to non MinerData type.")
cp = copy.deepcopy(self)
for key in self:
item = getattr(self, key)
other_item = getattr(other, key)
if item is None:
item = 0
if other_item is None:
other_item = 0
if isinstance(item, int):
setattr(cp, key, item + other_item)
if isinstance(item, float):
setattr(cp, key, item + other_item)
if isinstance(item, str):
setattr(cp, key, "")
if isinstance(item, list):
setattr(cp, key, item + other_item)
if isinstance(item, bool):
setattr(cp, key, item & other_item)
return cp
@property
def total_chips(self): # noqa - Skip PyCharm inspection
return self.right_chips + self.center_chips + self.left_chips