Apr-09-2019, 08:00 AM
Hello everyone,
I've recently just started learning Python and programming as a whole and while tinkering with some MySQL and Python i noticed that MySQL outputs query results in a nice tabulated format and there was no such functionality in Python by default ( didn't know about modules like tabulate and pretty table and how to use them at that time
), so i was striven to make something myself and i've put together some hacky/ slashy code to the best of my current ability while trying to learn how things like classes etc. work
.
I know that the code looks bad and many things that could have been achieved using simpler/ better means were not. Please feel free to criticize the code as it will help me learn.
Cheers,
iMu
I've recently just started learning Python and programming as a whole and while tinkering with some MySQL and Python i noticed that MySQL outputs query results in a nice tabulated format and there was no such functionality in Python by default ( didn't know about modules like tabulate and pretty table and how to use them at that time


I know that the code looks bad and many things that could have been achieved using simpler/ better means were not. Please feel free to criticize the code as it will help me learn.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
class tablemaker: """ anylist : Any list, tuple, set or string to be printed in ascii enclosed boxes (two dimensional maximum) title : Optional title for the list to be printed on top of the data headers : Optional headers to be printed (Provide as a one dimensional list with each list item representing a column heading) """ def __init__( self , provideddata, title, providedheaders): self .anydata = list ( map ( list , self .__all_strings(provideddata[:]))) self .__addresses = self .__generate_addresses() self .title = title if isinstance ( self .title, str ) ! = True : raise TypeError( 'String expected for argument#2' ) self .headers = providedheaders if self .headers! = "": self .headers = self .__all_strings(providedheaders) self .table = [] self .columns = 0 self .rows = 0 self .__process( self .__all_strings( self .anydata)) def __generate_addresses( self ): addressrange = [] maincounter = 0 innercounter = 0 for each in self .anydata: if isinstance (each,( list , tuple , set )): for eachinner in each: addressrange.append([maincounter,innercounter]) innercounter = innercounter + 1 maincounter = maincounter + 1 innercounter = 0 else : addressrange.append([maincounter,innercounter]) maincounter = maincounter + 1 return addressrange def value( self ,r,c,value): if value! = "": if [r,c] in self .__addresses: try : self .anydata[r][c] = value except : self .anydata[r] = value else : columnfound = [ 0 , False ] for loop in range (c, - 1 , - 1 ): if [r,loop] in self .__addresses: columnfound = [loop, True ] break if columnfound[ 1 ] = = True : if not isinstance ( self .anydata[r], list ): self .anydata[r] = [ self .anydata[r]] for loop in range (columnfound[ 0 ] + 1 ,c): self .anydata[r].append("") self .anydata[r].append(value) else : rowfound = [ 0 , False ] for loop in range (r, - 1 , - 1 ): if [loop, 0 ] in self .__addresses: rowfound = [loop, True ] break if rowfound[ 1 ] = = True : for loop in range (rowfound[ 0 ] + 1 ,r + 1 ): self .anydata.append([]) for loop in range ( 1 ,c + 1 ): self .anydata[r].append("") self .anydata[r].append(value) self .__addresses = self .__generate_addresses() else : raise ValueError( 'Invalid data provided in argument#3' ) def update( self ): self .__process( self .__all_strings( self .anydata)) def print ( self ): print ( "\n" .join( self .table)) def __all_strings( self ,data): """Courtesy of Yoriz""" if type (data) = = list : items = [] for item in data: items.append( self .__all_strings(item)) return items elif type (data) = = tuple : return tuple ( self .__all_strings( list (data))) elif type (data) = = dict : new_dict = {} for key, value in data.items(): new_dict[ self .__all_strings(key)] = self .__all_strings(value) return new_dict elif type (data) = = set : return set ( self .__all_strings( list (data))) else : return str (data).strip() def __process( self ,receivedlist): anylist = [] anylist = receivedlist[:] anylist = [i if i! = [] else "" for i in anylist] if len (anylist)> 0 : if self .headers! = "": anylist.insert( 0 , self .headers) datalist = [] maxlen = len ( self .title) collen = [] maxsubentry = 1 for entries in anylist: if isinstance (entries,( list , tuple )): if len (entries)>maxsubentry: maxsubentry = len (entries) for subentry in entries: if len (subentry)>maxlen: maxlen = len (subentry) if len (entries)>maxlen: maxlen = len (entries) collen = [ 0 for each in range ( 0 ,maxsubentry)] for entries in anylist: if isinstance (entries,( list , tuple )): for subentry in entries: if len (subentry)>collen[entries.index(subentry)]: collen[entries.index(subentry)] = len (subentry) else : if len (entries)>collen[ 0 ]: collen[ 0 ] = len (entries) checklen = 0 for each in collen: checklen + = each if maxlen>checklen: collen[ - 1 ] = collen[ - 1 ] + (maxlen - checklen) else : maxlen = checklen if self .title! = "": printstring = "+" + "-" * (maxlen + maxsubentry - 1 ) + "+" datalist.append(printstring) printstring = "|" + self .title.upper() + " " * ((maxlen + maxsubentry - 1 ) - len ( self .title)) + "|" datalist.append(printstring) printstring = "|" + "-" * (maxlen + maxsubentry - 1 ) + "|" datalist.append(printstring) else : printstring = "+" + "-" * (maxlen + maxsubentry - 1 ) + "+" datalist.append(printstring) for entries in anylist: if isinstance (entries,( list , tuple )): printstring = "" counter = 0 for subentry in entries: printstring = printstring + "|" + subentry + " " * (collen[counter] - len (subentry)) counter + = 1 if len (entries)<maxsubentry: for count in range ( len (entries) + 1 , maxsubentry + 1 ): printstring = printstring + "|" + " " * (collen[counter]) counter + = 1 printstring = printstring + "|" datalist.append(printstring) else : printstring = "" printstring = printstring + "|" + entries + " " * (collen[ 0 ] - len (entries)) for count in range ( 0 ,maxsubentry - 1 ): printstring = printstring + "|" + " " * (collen[count + 1 ]) printstring = printstring + "|" datalist.append(printstring) printstring = "+" + "-" * (maxlen + maxsubentry - 1 ) + "+" datalist.append(printstring) if self .title! = "": rowlen = len (datalist) - 4 else : rowlen = len (datalist) - 2 if self .headers! = "": rowlen = rowlen - 1 if self .headers! = "": printstring = "+" + "-" * (maxlen + maxsubentry - 1 ) + "+" if self .title! = "": datalist.insert( 4 ,printstring) else : datalist.insert( 2 ,printstring) self .table = datalist self .columns = len (collen) self .rows = rowlen return [datalist, len (collen),rowlen] else : raise ValueError( 'Invalid data provided in argument#1' ) if __name__ = = "__main__" : citytable = tablemaker([[ "1" , "Tokyo" , "Japan" , "38,001,000" ], [ "2" , "Delhi" , "India" , "25,703,168" ], [ "3" , "Shanghai" , "China" , "23,704,778" ], [ "4" , "Sao Paulo" , "Brazil" , "21,066,245" ], [ "5" , "Mumbai" , "India" , "21,042,538" ], [ "6" , "Mexico City" , "Mexico" , "20,998,543" ], [ "7" , "Beijing" , "China" , "20,383,994" ], [ "8" , "Osaka" , "China" , "20,237,645" ], [ "9" , "Cairo" , "Egypt" , "18,771,769" ]], "Largest Cities by Population" , [ "Rank" , "City" , "Country" , "Population" ]) citytable. print () print ( "\nUpdating incorrect value at row 6, column 2 i.e. China to Japan\n" ) citytable.value( 6 , 2 , "Japan" ) citytable.update() citytable. print () #print(citytable.table) |
iMu