ListDict always returned now.... hybrid list & dictionary

Now all return values are through a new class called ListDict. It's an ordered dictionary that allows access like a dictionary and a list.
This commit is contained in:
MikeTheWatchGuy 2018-08-10 16:16:16 -04:00
parent 9d3d3f4bc1
commit 5abcd7c546
1 changed files with 27 additions and 5 deletions

View File

@ -4,6 +4,7 @@ from tkinter import filedialog
from tkinter import ttk from tkinter import ttk
import tkinter.scrolledtext as tkst import tkinter.scrolledtext as tkst
import tkinter.font import tkinter.font
from collections import OrderedDict
import datetime import datetime
import sys import sys
import textwrap import textwrap
@ -1142,6 +1143,27 @@ def EncodeRadioRowCol(row, col):
RadValue = row * 1000 + col RadValue = row * 1000 + col
return RadValue return RadValue
#===== ListDict - New data type for returning values =====
class ListDict(OrderedDict):
def __iter__(self):
for v in self.values():
yield v
def __getitem__(self, item):
if isinstance(item, slice):
return list(self.values())[item]
else:
return super().__getitem__(item)
def __str__(self):
return str(self.ToList())
def ToList(self):
output = []
for item in self.values():
output.append(item)
return output
# ------- FUNCTION BuildResults. Form exiting so build the results to pass back ------- # # ------- FUNCTION BuildResults. Form exiting so build the results to pass back ------- #
# format of return values is # format of return values is
# (Button Pressed, input_values) # (Button Pressed, input_values)
@ -1155,7 +1177,8 @@ def BuildResults(form):
results=form.Results results=form.Results
button_pressed_text = None button_pressed_text = None
input_values = [] input_values = []
input_values_dictionary = {} # input_values_dictionary = {}
input_values_dictionary = ListDict()
key_counter = 0 key_counter = 0
for row_num,row in enumerate(form.Rows): for row_num,row in enumerate(form.Rows):
for col_num, element in enumerate(row): for col_num, element in enumerate(row):
@ -1255,10 +1278,9 @@ def BuildResults(form):
input_values_dictionary.pop(None, None) # clean up dictionary include None was included input_values_dictionary.pop(None, None) # clean up dictionary include None was included
except: pass except: pass
if not form.UseDictionary: # return values are always a list dictionary now (ordered dict with added features)
form.ReturnValues = button_pressed_text, input_values form.ReturnValues = button_pressed_text, input_values_dictionary
else:
form.ReturnValues = button_pressed_text, input_values_dictionary
form.ReturnValuesDictionary = button_pressed_text, input_values_dictionary form.ReturnValuesDictionary = button_pressed_text, input_values_dictionary
form.ResultsBuilt = True form.ResultsBuilt = True
return form.ReturnValues return form.ReturnValues