#------------------------------------------------------------------------------- # Name: DuchampParameterGUI_listPrinting # Purpose: # # Author: Kelvin # # Created: 4/12/2013 #------------------------------------------------------------------------------- # This function determines the longest key in a dictionary. def longestKeyLength(dictionary): return(len(max(dictionary.keys(), key = len))) # This function determines the longest value in a dictionary. def longestValueLength(dictionary): return(len(max(dictionary.values(), key = len))) # This is the minimum space between the a key and its value in a dictionary when we want to display it. minimumSpace = 5 # This appends the appropriate space after a single key in a dictionary. # Note: Can this be optimised? def tabbedDictionaryLine(dictionary, key, star): if key in dictionary: # Determine the residual space our word will have compared to the longest word. residualSpace = longestKeyLength(dictionary) - len(key) if star == 0: # Add extra space after the word based on the minimum separatio between the word and its definition. space = ' '*(minimumSpace + residualSpace) tabbedKey = key + space # Return the word with the appropriate spaces appended. return(tabbedKey) elif star == 1: star = ' (*) ' # Add extra space after the word based on the minimum separatio between the word and its definition. space = star + ' '*residualSpace + ' '*(minimumSpace - len(star)) tabbedKey = key + space # Return the word with the appropriate spaces appended. return(tabbedKey) else: print("Coding Error: 'Star' is not binary") # This exception is not needed, as the function will always be called with a word within the dictionary. else: print(key + "is not in the dictionary") return(' ') # This determines the longest line that will be printed in a dictionary given the above method. def longestLineLength(dictionary): maxLineLength = longestKeyLength(dictionary) + longestValueLength(dictionary) + minimumSpace return(maxLineLength) # Given an array of string, this returns an array of the same strings but in lower cases. def lower(stringArray): lowerArray = [] L = len(stringArray) for i in range(L): lowerArray += [stringArray[i].lower()] return lowerArray