From 675e7adc2c457d7221e3d3e735a295b63ec08af8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20M=C3=BCller?= Date: Tue, 7 Jul 2020 20:14:10 +0200 Subject: [PATCH] Use with for locking / linting --- NanoVNASaver/Calibration.py | 4 +++- NanoVNASaver/NanoVNASaver.py | 19 +++++-------------- NanoVNASaver/RFTools.py | 8 +++----- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/NanoVNASaver/Calibration.py b/NanoVNASaver/Calibration.py index 56d0bfe..b514698 100644 --- a/NanoVNASaver/Calibration.py +++ b/NanoVNASaver/Calibration.py @@ -40,6 +40,7 @@ RXP_CAL_LINE = re.compile(r"""^\s* logger = logging.getLogger(__name__) + def correct_delay(d: Datapoint, delay: float, reflect: bool = False): mult = 2 if reflect else 1 corr_data = d.z * cmath.exp( @@ -78,12 +79,13 @@ class CalData(UserDict): f' {d["isolation"].re} {d["isolation"].im}') return s + class CalDataSet: def __init__(self): self.data = defaultdict(CalData) def insert(self, name: str, dp: Datapoint): - if not name in self.data[dp.freq]: + if name not in self.data[dp.freq]: raise KeyError(name) self.data[dp.freq]["freq"] = dp.freq self.data[dp.freq][name] = dp diff --git a/NanoVNASaver/NanoVNASaver.py b/NanoVNASaver/NanoVNASaver.py index 6fc230b..ee58672 100644 --- a/NanoVNASaver/NanoVNASaver.py +++ b/NanoVNASaver/NanoVNASaver.py @@ -601,7 +601,6 @@ class NanoVNASaver(QtWidgets.QWidget): self.connect_device() else: self.disconnect_device() - return def connect_device(self): if not self.interface: @@ -692,15 +691,11 @@ class NanoVNASaver(QtWidgets.QWidget): self.worker.stopped = True def saveData(self, data, data21, source=None): - if self.dataLock.acquire(blocking=True): + with self.dataLock: self.data = data + self.data21 = data21 if self.s21att > 0: - self.data21 = corr_att_data(data21, self.s21att) - else: - self.data21 = data21 - else: - logger.error("Failed acquiring data lock while saving.") - self.dataLock.release() + self.data21 = corr_att_data(self.data21, self.s21att) if source is not None: self.sweepSource = source else: @@ -710,7 +705,7 @@ class NanoVNASaver(QtWidgets.QWidget): ).lstrip() def markerUpdated(self, marker: Marker): - if self.dataLock.acquire(blocking=True): + with self.dataLock: marker.findLocation(self.data) for m in self.markers: m.resetLabels() @@ -718,10 +713,9 @@ class NanoVNASaver(QtWidgets.QWidget): for c in self.subscribing_charts: c.update() - self.dataLock.release() def dataUpdated(self): - if self.dataLock.acquire(blocking=True): + with self.dataLock: for m in self.markers: m.resetLabels() m.updateLabels(self.data, self.data21) @@ -781,10 +775,7 @@ class NanoVNASaver(QtWidgets.QWidget): self.s21_min_gain_label.setText("") self.s21_max_gain_label.setText("") - else: - logger.error("Failed acquiring data lock while updating.") self.updateTitle() - self.dataLock.release() self.dataAvailable.emit() def sweepFinished(self): diff --git a/NanoVNASaver/RFTools.py b/NanoVNASaver/RFTools.py index d84049e..160f85e 100644 --- a/NanoVNASaver/RFTools.py +++ b/NanoVNASaver/RFTools.py @@ -149,7 +149,7 @@ def serial_to_parallel(z: complex) -> complex: return complex(z_sq_sum / z.real, z_sq_sum / z.imag) -def corr_att_data(data: List[Datapoint], att: float): +def corr_att_data(data: List[Datapoint], att: float) -> List[Datapoint]: """Correct the ratio for a given attenuation on s21 input""" if att <= 0: return data @@ -157,8 +157,6 @@ def corr_att_data(data: List[Datapoint], att: float): att = 10**(att/20) ndata = [] for dp in data: - freq, re, im = dp - orig = complex(re, im) - corrected = orig * att - ndata.append(Datapoint(freq, corrected.real, corrected.imag)) + corrected = dp.z * att + ndata.append(Datapoint(dp.freq, corrected.real, corrected.imag)) return ndata