Skip to content

Commit

Permalink
Move expand() function to wv_helper.py
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Jul 9, 2020
1 parent 6d6057e commit 4309ec5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
29 changes: 1 addition & 28 deletions src/wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


from wireviz.Harness import Harness
from wireviz.wv_helper import expand


def parse(yaml_input, file_out=None, generate_bom=False, return_types: (None, str, Tuple[str]) = None):
Expand All @@ -31,34 +32,6 @@ def parse(yaml_input, file_out=None, generate_bom=False, return_types: (None, st

yaml_data = yaml.safe_load(yaml_input)

def expand(yaml_data):
# yaml_data can be:
# - a singleton (normally str or int)
# - a list of str or int
# if str is of the format '#-#', it is treated as a range (inclusive) and expanded
output = []
if not isinstance(yaml_data, list):
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)
else:
try:
x = int(e)
except Exception:
x = e
output.append(x)
return output

harness = Harness()

# add items
Expand Down
29 changes: 29 additions & 0 deletions src/wireviz/wv_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ def nested_html_table(rows):
return html


def expand(yaml_data):
# yaml_data can be:
# - a singleton (normally str or int)
# - a list of str or int
# if str is of the format '#-#', it is treated as a range (inclusive) and expanded
output = []
if not isinstance(yaml_data, list):
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)
else:
try:
x = int(e)
except Exception:
x = e
output.append(x)
return output


def int2tuple(inp):
if isinstance(inp, tuple):
output = inp
Expand Down

0 comments on commit 4309ec5

Please sign in to comment.