Skip to content

Commit

Permalink
Remove brackets arround if statements and shorten bom dict opperatiions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Ward committed Oct 17, 2020
1 parent b20c98c commit 187adfd
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def create_graph(self) -> Graph:
wirehtml.append(' </table>')
wirehtml.append(' </td>')
wirehtml.append(' </tr>')
if(cable.category == 'bundle'): # for bundles individual wires can have part information
if cable.category == 'bundle': # for bundles individual wires can have part information
# create a list of wire parameters
wireidentification = []
if isinstance(cable.pn, list):
Expand All @@ -216,7 +216,7 @@ def create_graph(self) -> Graph:
if manufacturer_info:
wireidentification.append(html_line_breaks(manufacturer_info))
# print parameters into a table row under the wire
if(len(wireidentification) > 0):
if len(wireidentification) > 0 :
wirehtml.append(' <tr><td colspan="3">')
wirehtml.append(' <table border="0" cellspacing="0" cellborder="0"><tr>')
for attrib in wireidentification:
Expand Down Expand Up @@ -340,7 +340,7 @@ def get_additional_component_table(self, component: Union[Connector, Cable]) ->
rows.append(["Additional components"])
for extra in component.additional_components:
qty = extra.qty * component.get_qty_multiplier(extra.qty_multiplier)
if(self.mini_bom_mode):
if self.mini_bom_mode:
id = self.get_bom_index(extra.description, extra.unit, extra.manufacturer, extra.mpn, extra.pn)
rows.append(component_table_entry(f'#{id} ({extra.type.capitalize()})', qty, extra.unit))
else:
Expand All @@ -351,17 +351,15 @@ def get_additional_component_bom(self, component: Union[Connector, Cable]) -> Li
bom_entries = []
for part in component.additional_components:
qty = part.qty * component.get_qty_multiplier(part.qty_multiplier)
bom_entries.append(
{
'item': part.description,
'qty': qty,
'unit': part.unit,
'manufacturer': part.manufacturer,
'mpn': part.mpn,
'pn': part.pn,
'designators': component.name if component.show_name else None
}
)
bom_entries.append({
'item': part.description,
'qty': qty,
'unit': part.unit,
'manufacturer': part.manufacturer,
'mpn': part.mpn,
'pn': part.pn,
'designators': component.name if component.show_name else None
})
return(bom_entries)

def bom(self):
Expand Down Expand Up @@ -427,7 +425,6 @@ def bom(self):
bom_types_group = lambda bt: (bt['item'], bt['unit'], bt['manufacturer'], bt['mpn'], bt['pn'])
for group in Counter([bom_types_group(v) for v in bom_entries]):
group_entries = [v for v in bom_entries if bom_types_group(v) == group]
shared = group_entries[0]
designators = []
for group_entry in group_entries:
if group_entry.get('designators'):
Expand All @@ -437,24 +434,19 @@ def bom(self):
designators.append(group_entry['designators'])
designators = list(dict.fromkeys(designators)) # remove duplicates
designators.sort()
total_qty = sum(i['qty'] for i in group_entries)
self._bom.append({
'item': shared['item'], 'qty': round(total_qty, 3), 'unit': shared['unit'], 'designators': designators,
'manufacturer': shared['manufacturer'], 'mpn': shared['mpn'], 'pn': shared['pn']
})
total_qty = sum(entry['qty'] for entry in group_entries)
self._bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': designators})

self._bom = sorted(self._bom, key=lambda k: k['item']) # sort list of dicts by their values (https://stackoverflow.com/a/73050)
# add index
index = 1
for item in self._bom:
item["id"] = index
index += 1

# add an incrementing id to each bom item
self._bom = [{**entry, 'id': index} for index, entry in enumerate(self._bom, 1)]
return self._bom

def get_bom_index(self, item, unit, manufacturer, mpn, pn):
for bom_item in self.bom():
if((bom_item['item'], bom_item['unit'], bom_item['manufacturer'], bom_item['mpn'], bom_item['pn']) == (item, unit, manufacturer, mpn, pn)):
return bom_item['id']
for entry in self.bom():
if(entry['item'], entry['unit'], entry['manufacturer'], entry['mpn'], entry['pn']) == (item, unit, manufacturer, mpn, pn):
return entry['id']
return None

def bom_list(self):
Expand Down

0 comments on commit 187adfd

Please sign in to comment.