Simplify __init__ functions, Pt. I

This commit is contained in:
Daniel Rojas 2020-05-28 18:37:44 +02:00
parent 07567be3ca
commit ab922928de
3 changed files with 44 additions and 24 deletions

View File

@ -3,7 +3,6 @@ nodes:
type: D-Sub type: D-Sub
gender: female gender: female
pinout: [DCD, RX, TX, DTR, GND, DSR, RTS, CTS, RI] pinout: [DCD, RX, TX, DTR, GND, DSR, RTS, CTS, RI]
random: yes
X2: X2:
type: Molex KK 254 type: Molex KK 254
gender: female gender: female

View File

@ -57,11 +57,11 @@ class Harness:
self.nodes = {} self.nodes = {}
self.cables = {} self.cables = {}
def add_node(self, name, type=None, gender=None, show_name=True, num_pins=None, show_num_pins=True, pinout=None, ports_left=False, ports_right=False): def add_node(self, name, *args, **kwargs):
self.nodes[name] = Node(name, type, gender, show_name, num_pins, show_num_pins, pinout, ports_left, ports_right) self.nodes[name] = Node(name, *args, **kwargs)
def add_cable(self, name, mm2=None, awg=None, show_equiv=False, length=0, show_name=False, show_pinout=False, num_wires=None, show_num_wires=True, colors=None, color_code=None, shield=False): def add_cable(self, name, *args, **kwargs):
self.cables[name] = Cable(name, mm2, awg, show_equiv, length, show_name, show_pinout, num_wires, show_num_wires, colors, color_code, shield) self.cables[name] = Cable(name, *args, **kwargs)
def loop(self, node_name, from_pin, to_pin): def loop(self, node_name, from_pin, to_pin):
self.nodes[node_name].loop(from_pin, to_pin) self.nodes[node_name].loop(from_pin, to_pin)
@ -177,14 +177,22 @@ class Harness:
class Node: class Node:
def __init__(self, name, type=None, gender=None, show_name=True, num_pins=None, show_num_pins=True, pinout=None, ports_left=False, ports_right=False): def __init__(self, name,
type=None,
gender=None,
show_name=True,
num_pins=True,
show_num_pins=None,
pinout=None):
self.name = name self.name = name
self.type = type self.type = type
self.gender = gender self.gender = gender
self.show_name = show_name self.show_name = show_name
self.show_num_pins = show_num_pins self.show_num_pins = show_num_pins
self.ports_left = ports_left # self.pinout = []
self.ports_right = ports_right
self.ports_left = False
self.ports_right = False
self.loops = [] self.loops = []
if pinout is None: if pinout is None:
@ -193,17 +201,27 @@ class Node:
self.pinout = ('',) * num_pins self.pinout = ('',) * num_pins
else: else:
if num_pins is None: if num_pins is None:
if pinout is None: raise Exception('Must provide num_pins or pinout')
raise Exception('Must provide num_pins or pinout') else:
else: self.pinout = pinout
self.pinout = pinout
def loop(self, from_pin, to_pin): def loop(self, from_pin, to_pin):
self.loops.append((from_pin, to_pin)) self.loops.append((from_pin, to_pin))
class Cable: class Cable:
def __init__(self, name, mm2=None, awg=None, show_equiv=False, length=0, show_name=False, show_pinout=False, num_wires=None, show_num_wires=True, colors=None, color_code=None, shield=False): def __init__(self, name,
mm2=None,
awg=None,
show_equiv=False,
length=0,
show_name=False,
show_pinout=False,
num_wires=None,
show_num_wires=True,
colors=None,
color_code=None,
shield=False ):
self.name = name self.name = name
if mm2 is not None and awg is not None: if mm2 is not None and awg is not None:
raise Exception('You cannot define both mm2 and awg!') raise Exception('You cannot define both mm2 and awg!')

View File

@ -1,6 +1,8 @@
import yaml import yaml
import wireviz import wireviz
filename = '../examples/example1.yml'
filename = '../examples/example2.yml'
filename = '../examples/ferrules.yml' filename = '../examples/ferrules.yml'
def check_designators(what, where): def check_designators(what, where):
@ -49,10 +51,7 @@ h = wireviz.Harness()
if 'nodes' in input and type(input['nodes']) == dict: if 'nodes' in input and type(input['nodes']) == dict:
if len(input['nodes']) > 0: if len(input['nodes']) > 0:
for k, o in input['nodes'].items(): for k, o in input['nodes'].items():
h.add_node(k, type=o.get('type'), h.add_node(name=k, **o)
gender=o.get('gender'),
num_pins=o.get('num_pins'),
pinout=o.get('pinout'))
else: else:
print('Node list empty') print('Node list empty')
else: else:
@ -63,19 +62,22 @@ else:
if 'wires' in input and type(input['wires']) == dict: if 'wires' in input and type(input['wires']) == dict:
if len(input['wires']) > 0: if len(input['wires']) > 0:
for k, o in input['wires'].items(): for k, o in input['wires'].items():
h.add_cable(k, mm2=o.get('mm2'), h.add_cable(name=k, **o)
awg=o.get('awg'),
length=o.get('length'),
num_wires=o.get('num_wires'),
colors=o.get('colors'),
color_code=o.get('color_code'),
shield=o.get('shield'))
else: else:
print('Wire list empty') print('Wire list empty')
else: else:
print('No wire list found') print('No wire list found')
input['wires'] = {} input['wires'] = {}
if 'ferrules' in input and type(input['ferrules']) == dict:
if len(input['wires']) > 0:
pass
else:
print('Ferrule list empty')
else:
print('No ferrule list found')
input['ferrules'] = {}
# add connections # add connections
if 'connections' in input: if 'connections' in input:
if len(input['connections']) > 0: if len(input['connections']) > 0:
@ -129,6 +131,7 @@ if 'connections' in input:
w_n = check_designators([from_name, to_name],('wires','nodes')) w_n = check_designators([from_name, to_name],('wires','nodes'))
n_n = check_designators([from_name, to_name],('nodes','nodes')) n_n = check_designators([from_name, to_name],('nodes','nodes'))
f_w = check_designators([from_name, to_name],('ferrules','wires')) f_w = check_designators([from_name, to_name],('ferrules','wires'))
w_f = check_designators([from_name, to_name],('wires','ferrules')) w_f = check_designators([from_name, to_name],('wires','ferrules'))