[1332] | 1 | #------------------------------------------------------------------------------- |
---|
| 2 | # Name: DuchampParameterGUI_listPrinting |
---|
| 3 | # Purpose: |
---|
| 4 | # |
---|
| 5 | # Author: Kelvin |
---|
| 6 | # |
---|
| 7 | # Created: 4/12/2013 |
---|
| 8 | #------------------------------------------------------------------------------- |
---|
| 9 | |
---|
| 10 | # This function determines the longest key in a dictionary. |
---|
| 11 | def longestKeyLength(dictionary): |
---|
| 12 | return(len(max(dictionary.keys(), key = len))) |
---|
| 13 | |
---|
| 14 | # This function determines the longest value in a dictionary. |
---|
| 15 | def longestValueLength(dictionary): |
---|
| 16 | return(len(max(dictionary.values(), key = len))) |
---|
| 17 | |
---|
| 18 | # This is the minimum space between the a key and its value in a dictionary when we want to display it. |
---|
| 19 | minimumSpace = 5 |
---|
| 20 | |
---|
| 21 | # This appends the appropriate space after a single key in a dictionary. |
---|
| 22 | # Note: Can this be optimised? |
---|
| 23 | def tabbedDictionaryLine(dictionary, key, star): |
---|
| 24 | |
---|
| 25 | if key in dictionary: |
---|
| 26 | |
---|
| 27 | # Determine the residual space our word will have compared to the longest word. |
---|
| 28 | residualSpace = longestKeyLength(dictionary) - len(key) |
---|
| 29 | |
---|
| 30 | if star == 0: |
---|
| 31 | |
---|
| 32 | # Add extra space after the word based on the minimum separatio between the word and its definition. |
---|
| 33 | space = ' '*(minimumSpace + residualSpace) |
---|
| 34 | tabbedKey = key + space |
---|
| 35 | |
---|
| 36 | # Return the word with the appropriate spaces appended. |
---|
| 37 | return(tabbedKey) |
---|
| 38 | elif star == 1: |
---|
| 39 | |
---|
| 40 | star = ' (*) ' |
---|
| 41 | |
---|
| 42 | # Add extra space after the word based on the minimum separatio between the word and its definition. |
---|
| 43 | space = star + ' '*residualSpace + ' '*(minimumSpace - len(star)) |
---|
| 44 | tabbedKey = key + space |
---|
| 45 | |
---|
| 46 | # Return the word with the appropriate spaces appended. |
---|
| 47 | return(tabbedKey) |
---|
| 48 | |
---|
| 49 | else: |
---|
| 50 | print("Coding Error: 'Star' is not binary") |
---|
| 51 | |
---|
| 52 | # This exception is not needed, as the function will always be called with a word within the dictionary. |
---|
| 53 | else: |
---|
| 54 | print(key + "is not in the dictionary") |
---|
| 55 | return(' ') |
---|
| 56 | |
---|
| 57 | # This determines the longest line that will be printed in a dictionary given the above method. |
---|
| 58 | def longestLineLength(dictionary): |
---|
| 59 | |
---|
| 60 | maxLineLength = longestKeyLength(dictionary) + longestValueLength(dictionary) + minimumSpace |
---|
| 61 | return(maxLineLength) |
---|
| 62 | |
---|
| 63 | # Given an array of string, this returns an array of the same strings but in lower cases. |
---|
| 64 | def lower(stringArray): |
---|
| 65 | |
---|
| 66 | lowerArray = [] |
---|
| 67 | |
---|
| 68 | L = len(stringArray) |
---|
| 69 | |
---|
| 70 | for i in range(L): |
---|
| 71 | lowerArray += [stringArray[i].lower()] |
---|
| 72 | |
---|
| 73 | return lowerArray |
---|