I am going to try and explain this the best way I can, but I expect that I will have to reword a few times.
So what I have is a list of diagrams that have certain specs. I have built a table with the various diagrams and their specs in Excel. I have built a GUI using PySimpleGui for the users to interact with. There are drop down boxes with the specs in them that they will use as inputs for the code to use to search for these diagrams. Here is the code for the GUI
#To create the GUI, first the layout of the window must be set up.
#Set the theme
psg.theme('LightBlue')
#Create a list that will be used for the listbox
diag_num = []
Torq = []
#Set up the layout
left_col = [[psg.Text('Choose the Type: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option','CSIR','CSCR', 'PSC', 'Poly', 'SP'], font='Helvetica',
default_value='Select Option', key='type')],
[psg.Text('Choose the TORQ Switch: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option','282070101', '282070111', '282070117', '282070118', '282070125', '282070201','282070205','282070251',
'282070261', '282070303', '282070304', '282070352', '282070403', '282070451', '282070600', '282070601', '282070602', '282070701', '282070702', '282070901', '282070902', '282070903', '282080101', 'None'],
font='Helvetica', default_value='Select Option', key='switch')],
[psg.Text('Is there a Terminal Board: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option', 'Yes', 'No'], font ='Helvetica', default_value='Select Option',
key='board')],
[psg.Text('Choose the Voltage: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option','Single','Double'], font='Helvetica',
default_value='Select Option', key='volt')],
[psg.Text('Choose the Rotation: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option','Non-Reversible','Reversible'], font='Helvetica',
default_value='Select Option', key='rot')],
[psg.Text('Choose the Start: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option','Total','Half'], font='Helvetica', default_value='Select Option',
key='start')],
[psg.Text('Choose the Overload: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option','Disc','Lace-On','None'], font='Helvetica',
default_value='Select Option', key='overl')],
[psg.Text('Choose the Assembly Voltage: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option', 'HI', 'LO', 'FX', 'None'], font='Helvetica',
default_value='Select Option', key='assymv')],
[psg.Text('Choose the Lead End: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option','CW','CCW', 'REV', 'None'], font='Helvetica', default_value='Select Option',
key='lead')],
[psg.Text('Choose an Auxiliary Switch: ', size=(25,2), font='Helvetica', justification='left'), psg.Combo(['Select Option', 'Air', 'Line', 'Mech', 'On/Off', 'Pull Chain',
'Relay', 'Rocker', 'SIMPAC', 'SSS', 'TRIAC', 'Volt', 'Multiple', 'None'], font='Helvetica', default_value='Select Option', key='auxsw')],
[psg.Button('Search', font=('Helvetica')), psg.Button('Reset', font=('Helvetica')), psg.Exit(font=('Helvetica'))]]
right_col = [[psg.Listbox(values = diag_num, enable_events=True, font='Helvetica', select_mode = 'LISTBOX_SELECT_MODE_SINGLE', key='ConnDiag', size=(20,22))],
[psg.Text('', size=(25,0))]]
layout = [[psg.Column(left_col, element_justification='c'), psg.Column(right_col, element_justification='c')]]
#Define the window
win = psg.Window('Connection Diagram Picker', layout, resizable=True)
The program then converts these inputs into 1s and 0s in order to handle whether the user left the drop down box as simple "Select Option" rather than giving an input. That way they can search for diagrams without being exact in their search. Here is the code for that:
if event == 'Search': #Searches based on combo values
'''
This event happens when the user clicks on the Search button in the GUI.
When this happens, the program assigns the user inputs a Boolean value based on 1s and 0s.
It then uses these values to perform basic logical tests to search through the Excel documents,
to find the Connection diagram that matches that criteria input by the user.
'''
win['ConnDiag'].update([''])
if v['switch'] != 'None' and v['switch'] != 'Select Option':
switch = int(v['switch'])
else:
switch = v['switch']
# Convert the inputs into Boolean logic assigned 1s and 0s
if v['type'] == 'Select Option': typeBool = 0
else: typeBool = 1
if v['switch'] == 'Select Option': switchBool = 0
else: switchBool = 1
if v['board'] == 'Select Option': boardBool = 0
else: boardBool = 1
if v['volt'] == 'Select Option': voltBool = 0
else: voltBool = 1
if v['rot'] == 'Select Option': rotBool = 0
else: rotBool = 1
if v['start'] == 'Select Option': startBool = 0
else: startBool = 1
if v['overl'] == 'Select Option': overlBool = 0
else: overlBool = 1
if v['assymv'] == 'Select Option': assymvBool = 0
else: assymvBool = 1
if v['lead'] == 'Select Option': leadBool = 0
else: leadBool = 1
if v['auxsw'] == 'Select Option': auxswBool = 0
else:auxswBool = 1
From there, the code interacts with the Excel table use Pandas. It will take the values in the drop down boxes and perform a search through the Excel table looking for the diagrams that have those specs input by the user. It will then pull up a list of those diagrams and display them in the list box.
# ---------------------------------------------------------------- All True ---------------------------------------------------------------- #
if typeBool == 1 and switchBool == 1 and boardBool == 1 and voltBool == 1 and rotBool == 1 and startBool == 1 and overlBool == 1 and assymvBool == 1 and leadBool == 1 and auxswBool == 1:
#Declare looping variables
i = 0
diag_num.clear()
Torq.clear()
while i < len(workbook):
if workbook['Type:'][i] == v['type'] and workbook['TORQ:'][i] == switch and workbook['Voltage:'][i] == v['volt'] and workbook['Rotation:'][i] == v['rot'] and workbook['Start:'][i] == v['start'] and workbook['Overload:'][i] == v['overl'] and workbook['Assembly Voltage:'][i] == v['assymv'] and workbook['Lead:'][i] == v['lead'] and workbook['Aux Switch:'][i] == v['auxsw']:
#Read through the Excel file to find the matching diagrams and append the list.
diag_num.append(workbook['Connection Diagram:'][i])
Torq.append(workbook['TORQ:'][i])
i += 1
diagNum, torq = DelDupl(diag_num,Torq)
if len(diagNum) == 0:
psg.popup('ERROR!', 'No connection diagram found!')
win['ConnDiag'].update(values = diagNum)
if v['ConnDiag']:
UserChoice = v['ConnDiag'][0]
UserChoice = BuildFile(UserChoice)
readFile(UserChoice)
# ---------------------------------------------------------------- One True ---------------------------------------------------------------- #
if typeBool == 1 and switchBool == 0 and boardBool == 0 and voltBool == 0 and rotBool == 0 and startBool == 0 and overlBool == 0 and assymvBool == 0 and leadBool == 0 and auxswBool == 0:
#Declare looping variables
i = 0
diag_num.clear()
Torq.clear()
while i < len(workbook):
if workbook['Type:'][i] == v['type']:
#Read through the Excel file to find the matching diagrams and append the list.
diag_num.append(workbook['Connection Diagram:'][i])
Torq.append(workbook['TORQ:'][i])
i += 1
diagNum, torq = DelDupl(diag_num,Torq)
win['ConnDiag'].update(values = diagNum)
if v['ConnDiag']:
UserChoice = v['ConnDiag'][0]
UserChoice = BuildFile(UserChoice)
readFile(UserChoice)
This is just a snippet of the code. It keeps going onwards, alternating between 1s and 0s, basically creating a massive truth table that have things happen based on whether the list boxes had inputs by the user or not. The code then checks to see what those inputs are then looks through an Excel table to find the diagrams that have specs that match the input by the user. The functions in this should not make a big difference because they just remove duplicates and search for the pdfs of the diagrams.
So, here is the thing, my code works. It has no problems working so it doesn't need fixed. What I am running into though is that with the number of connection diagrams I have and their specs and me wanting to make sure that I can easily add or subtract more diagrams in the future without messing too much with the code, I have a little over 1000 different combinations these 1s and 0s can take. Each block here takes about 30 lines of code so I will have nearly 40,000 lines of code when I am done. I am about 11,000 lines deep and it is just taking way too long to make. The code runs fine and isn't slow, yet. But I want to find a way to condense the code so I don't go insane trying to copy an paste and edit so much code. I know there has to be a way, but I cannot seem to figure out what to do.
I ended up figuring out what I needed to do. I ended up using lists and dumping information into them based on the user input values. Then from there, the code checks to see if there are items in each of the lists that exists in all of them, then returns those.
i = 0
j = 0
while i < len(workbook):
l_conn.append(workbook['Connection Diagram:'][i])
l_TORQ.append(workbook['TORQ:'][i])
if v['type'] == 'Select Option':
l_type.append(workbook['Connection Diagram:'][i])
else:
if workbook['Type:'][i] == v['type']:
l_type.append(workbook['Connection Diagram:'][i])
if v['switch'] == 'Select Option':
l_torq.append(workbook['Connection Diagram:'][i])
else:
if workbook['TORQ:'][i] == int(v['switch']):
l_torq.append(workbook['Connection Diagram:'][i])
if v['board'] == 'Select Option':
l_board.append(workbook['Connection Diagram:'][i])
else:
if workbook['Board:'][i] == v['board']:
l_board.append(workbook['Connection Diagram:'][i])
if v['volt'] == 'Select Option':
l_volt.append(workbook['Connection Diagram:'][i])
else:
if workbook['Voltage:'][i] == v['volt']:
l_volt.append(workbook['Connection Diagram:'][i])
if v['rot'] == 'Select Option':
l_rot.append(workbook['Connection Diagram:'][i])
else:
if workbook['Rotation:'][i] == v['rot']:
l_rot.append(workbook['Connection Diagram:'][i])
if v['start'] == 'Select Option':
l_start.append(workbook['Connection Diagram:'][i])
else:
if workbook['Start:'][i] == v['start']:
l_start.append(workbook['Connection Diagram:'][i])
if v['overl'] == 'Select Option':
l_overl.append(workbook['Connection Diagram:'][i])
else:
if workbook['Overload:'][i] == v['overl']:
l_overl.append(workbook['Connection Diagram:'][i])
if v['assymv'] == 'Select Option':
l_assymv.append(workbook['Connection Diagram:'][i])
else:
if workbook['Assembly Voltage:'][i] == v['assymv']:
l_assymv.append(workbook['Connection Diagram:'][i])
if v['lead'] == 'Select Option':
l_lead.append(workbook['Connection Diagram:'][i])
else:
if workbook['Lead:'][i] == v['lead']:
l_lead.append(workbook['Connection Diagram:'][i])
if v['auxsw'] == 'Select Option':
l_auxsw.append(workbook['Connection Diagram:'][i])
else:
if workbook['Aux Switch:'][i] == v['auxsw']:
l_auxsw.append(workbook['Connection Diagram:'][i])
i += 1
while j < len(l_conn):
if l_conn[j] in l_type and l_conn[j] in l_torq and l_conn[j] in l_board and l_conn[j] in l_volt and l_conn[j] in l_rot and l_conn[j] in l_start and l_conn[j] in l_overl and l_conn[j] in l_assymv and l_conn[j] in l_lead and l_conn[j] in l_auxsw:
diag_num.append(l_conn[j])
Torq.append(l_TORQ[j])
j += 1