From 255377b47d28c233a60130c8dbf6ebce3b694d00 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Thu, 30 Jul 2020 18:39:57 +0200 Subject: [PATCH] Improve handling of strings with '-' in wv_helper.expand() Fixes #139 --- src/wireviz/wv_helper.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 77309e50..21cac8e8 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -59,21 +59,26 @@ def expand(yaml_data): yaml_data = [yaml_data] for e in yaml_data: e = str(e) - if '-' in e: # list of pins - a, b = tuple(map(int, e.split('-'))) - if a < b: - for x in range(a, b + 1): - output.append(x) - elif a > b: - for x in range(a, b - 1, -1): - output.append(x) - elif a == b: - output.append(a) + if '-' in e: + a, b = e.split('-', 1) + try: + a = int(a) + b = int(b) + if a < b: + for x in range(a, b + 1): + output.append(x) # ascending range + elif a > b: + for x in range(a, b - 1, -1): + output.append(x) # descending range + elif a == b: + output.append(a) # range of length 1 + except: + output.append(e) # '-' was not a delimiter between two ints, pass e through unchanged else: try: - x = int(e) + x = int(e) # single int except Exception: - x = e + x = e # string output.append(x) return output