From d9b67e45677c2278eedeb7193b6895663140501f Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sun, 6 Dec 2020 12:37:02 +0100 Subject: [PATCH] Apply suggestions from code review by @kvid Co-authored-by: kvid --- src/wireviz/DataClasses.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index 8c7c888..010e06f 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -200,9 +200,11 @@ class Cable: try: g, u = self.gauge.split(' ') except Exception: - raise Exception(f'{self.gauge} - Gauge must be a number, or number and unit separated by a space') + raise Exception(f'Cable {self.name} gauge={self.gauge} - Gauge must be a number, or number and unit separated by a space') self.gauge = g + if self.gauge_unit is not None: + print(f'Warning: Cable {self.name} gauge_unit={self.gauge_unit} is ignored because its gauge contains {u}') if u.upper() == 'AWG': self.gauge_unit = u.upper() else: @@ -216,17 +218,18 @@ class Cable: if isinstance(self.length, str): # length and unit specified try: - l, u = self.length.split(' ') - l = float(l) + L, u = self.length.split(' ') + L = float(L) except Exception: - raise Exception(f'{self.length} - Length must be a number, or number and unit separated by a space') - self.length = l + raise Exception(f'Cable {self.name} length={self.length} - Length must be a number, or number and unit separated by a space') + self.length = L + if self.length_unit is not None: + print(f'Warning: Cable {self.name} length_unit={self.length_unit} is ignored because its length contains {u}') self.length_unit = u - elif self.length is not None: # length specified, assume m - if self.gauge_unit is None: - self.length_unit = 'm' - else: - pass # length not specified + elif not any(isinstance(self.length, t) for t in [int, float]): + raise Exception(f'Cable {self.name} length has a non-numeric value') + elif self.gauge_unit is None: + self.length_unit = 'm' self.connections = []