Bugfix for comparing of physical quantities

This commit is contained in:
Anne de Jong 2021-06-17 10:33:22 +02:00
parent ab1edaa530
commit aaf6597776
2 changed files with 52 additions and 32 deletions

View File

@ -1,12 +1,13 @@
__all__ = ['DaqChannel']
from ..lasp_common import Qty, SIQtys
import json, logging
from dataclasses import dataclass, field
from dataclasses_json import dataclass_json
from typing import List
import json
from dataclasses_json import dataclass_json
from ..lasp_common import Qty, SIQtys
@dataclass_json
@dataclass
@dataclass(eq=False)
class DaqChannel:
channel_enabled: bool
channel_name: str = 'Unnamed channel'
@ -17,12 +18,12 @@ class DaqChannel:
channel_metadata: str = ''
def __post_init__(self):
# logging.debug(f'__post_init__({self.channel_name})')
self._qty = SIQtys.default()
if len(self.channel_metadata) > 0:
meta = json.loads(self.channel_metadata)
if '_qty' in meta:
self._qty = Qty.from_json(meta['_qty'])
else:
self._qty = SIQtys.default
if 'qty' in meta:
self._qty = Qty.from_json(meta['qty'])
@property
def qty(self):
@ -31,7 +32,7 @@ class DaqChannel:
@qty.setter
def qty(self, newqty):
self._qty = newqty
self._store('_qty', newqty)
self._store('qty', newqty)
def _store(self, name, val):
if len(self.channel_metadata) > 0:
@ -42,3 +43,15 @@ class DaqChannel:
meta[name] = val.to_json()
self.channel_metadata = json.dumps(meta)
def __eq__(self, other):
"""
We overwrite the default equal-check as the floating point values
cannot be exactly checked due to conversion from/to JSON
"""
return (self.channel_enabled == other.channel_enabled and
self.channel_name == other.channel_name and
self.range_index == other.range_index and
self.ACCoupling_enabled == other.ACCoupling_enabled and
self.IEPE_enabled == other.IEPE_enabled and
self.channel_metadata == other.channel_metadata)

View File

@ -1,8 +1,5 @@
# -*- coding: utf-8 -*-
import os, platform
import shelve
import sys
import appdirs
import shelve, logging, sys, appdirs, os, platform
import numpy as np
from .wrappers import Window as wWindow
@ -59,21 +56,36 @@ class AvType(Enum):
@dataclass_json
@dataclass
@dataclass(eq=False)
class Qty:
name: str
unit_name: str
unit_symb: str
level_unit: str
level_ref_name: str
level_ref_value: str
level_unit: object
# Contains a tuple of possible level names, including its reference value.
# For now, it is only able to compute for the first one. I.e.e we are not
# yet able to compute `dBPa's'
level_ref_name: object
level_ref_value: object
def __str__(self):
return self.name + f' [{self.unit_symb}]'
return f'{self.name} [{self.unit_symb}]'
def __eq__(self, other):
# logging.debug('eq()')
"""
Comparison breaks for the other objects, level unit, level ref name,
etc as these are tuples / a single string.
"""
return (self.name == other.name and
self.unit_name == other.unit_name)
class SIQtys:
@unique
class SIQtys(Enum):
N = Qty(name='Number',
unit_name='No unit / full scale',
unit_symb='-',
@ -96,9 +108,6 @@ class SIQtys:
level_ref_name=('1V',),
level_ref_value=(1.0,),
)
types = (N, AP, V)
default = N
default_index = 0
@staticmethod
def fillComboBox(cb):
@ -109,20 +118,18 @@ class SIQtys:
cb: QComboBox to fill
"""
cb.clear()
for ty in SIQtys.types:
cb.addItem(f'{ty.unit_name}')
cb.setCurrentIndex(SIQtys.default_index)
for ty in SIQtys:
cb.addItem(f'{ty.value.unit_name}')
cb.setCurrentIndex(0)
@staticmethod
def default():
return SIQtys.N.value
@staticmethod
def getCurrent(cb):
return SIQtys.types[cb.currentIndex()]
return list(SIQtys)[cb.currentIndex()]
@staticmethod
def getIndex(qty):
for i, qtyi in enumerate(SIQtys.types):
if qtyi == qty:
return i
return -1
@dataclass
class CalSetting: