|
| 1 | +'''This module can be create tables in python to better represent the data. This cant be used as database. You can create organise date in table formed of +, |, and -. |
| 2 | +made by jimmy kumar ahalpara''' |
| 3 | +class Pytable: |
| 4 | + def __init__(self,table_name='Table'): |
| 5 | + '''initiator method, table name should be a string''' |
| 6 | + if type(table_name)==str: |
| 7 | + self.__max_row=0 |
| 8 | + self.table_name=table_name |
| 9 | + self.__main_dict={} |
| 10 | + self.__sequence=[] |
| 11 | + else: |
| 12 | + raise TypeError('table_name must be a string') |
| 13 | + def insert_column(self,heading): |
| 14 | + '''used to insert column, enter column heading''' |
| 15 | + heading=str(heading) |
| 16 | + self.__main_dict[heading]=[len(heading),[]] |
| 17 | + self.__sequence.append(heading) |
| 18 | + def swap_column_position(self,first_col_name,second_col_name): |
| 19 | + '''swap two column using their name''' |
| 20 | + if (first_col_name in self.__sequence) and (second_col_name in self.__sequence): |
| 21 | + first_col_name,second_col_name=str(first_col_name),str(second_col_name) |
| 22 | + i1=self.__sequence.index(first_col_name) |
| 23 | + i2=self.__sequence.index(second_col_name) |
| 24 | + else: |
| 25 | + raise KeyError('column name not found') |
| 26 | + self.__sequence[i1],self.__sequence[i2]=self.__sequence[i2],self.__sequence[i1] |
| 27 | + def swap_cells(self,column_name,cel1,cel2): |
| 28 | + '''swap cells using their value''' |
| 29 | + column_name,cel1,cel2=str(column_name),str(cel1),str(cel2) |
| 30 | + if (cel1 in self.__main_dict[column_name][1]) and (cel2 in self.__main_dict[column_name][1]): |
| 31 | + i1=self.__main_dict[column_name][1].index(cel1) |
| 32 | + i2=self.__main_dict[column_name][1].index(cel2) |
| 33 | + k=self.__main_dict[column_name][1][i1] |
| 34 | + self.__main_dict[column_name][1][i1]=self.__main_dict[column_name][1][i2] |
| 35 | + self.__main_dict[column_name][1][i2]=k |
| 36 | + else: |
| 37 | + raise KeyError('cell name not found') |
| 38 | + def insert_into_column(self,column_name,value): |
| 39 | + '''insert values into column''' |
| 40 | + column_name=str(column_name) |
| 41 | + value=str(value) |
| 42 | + if column_name in self.__sequence: |
| 43 | + self.__main_dict[column_name][1].append(value) |
| 44 | + if len(value)>self.__main_dict[column_name][0]: |
| 45 | + self.__main_dict[column_name][0]=len(value) |
| 46 | + if len(self.__main_dict[column_name][1])>self.__max_row: |
| 47 | + self.__max_row=len(self.__main_dict[column_name][1]) |
| 48 | + else: |
| 49 | + raise KeyError('column name not found') |
| 50 | + def insert_by_index(self,column_name,index,value): |
| 51 | + '''insert into column by their index''' |
| 52 | + value=str(value) |
| 53 | + if column_name in self.__sequence: |
| 54 | + if type(index)==int: |
| 55 | + self.__main_dict[column_name][1].insert(index,value) |
| 56 | + if len(value)>self.__main_dict[column_name][0]: |
| 57 | + self.__main_dict[column_name][0]=len(value) |
| 58 | + if len(self.__main_dict[column_name][1])>self.__max_row: |
| 59 | + self.__max_row=len(self.__main_dict[column_name][1]) |
| 60 | + else: |
| 61 | + raise TypeError('index must be a number') |
| 62 | + else: |
| 63 | + raise KeyError('column name not found') |
| 64 | + def return_sheet(self): |
| 65 | + '''will return a string, printing that string will result in formation of table using + and -''' |
| 66 | + st=self.table_name+'\n' |
| 67 | + st+='+' |
| 68 | + for a in self.__sequence: |
| 69 | + st+='-'*self.__main_dict[a][0]+'+' |
| 70 | + st+='\n|' |
| 71 | + for a in self.__sequence: |
| 72 | + st+=a+' '*(self.__return_diff(len(a),self.__main_dict[a][0]))+'|' |
| 73 | + st+='\n+' |
| 74 | + for a in self.__sequence: |
| 75 | + st+='-'*self.__main_dict[a][0]+'+' |
| 76 | + for a in range(self.__max_row): |
| 77 | + st+='\n|' |
| 78 | + for b in self.__sequence: |
| 79 | + st+=self.__return_from_list(b,a)+(' '*(self.__return_diff(len(self.__return_from_list(b,a)),self.__main_dict[b][0])))+'|' |
| 80 | + st+='\n+' |
| 81 | + for b in self.__sequence: |
| 82 | + st+=('-'*self.__main_dict[b][0])+'+' |
| 83 | + return st |
| 84 | + def __return_diff(self,a,b): |
| 85 | + if a>=b: |
| 86 | + return a-b |
| 87 | + elif b>a: |
| 88 | + return b-a |
| 89 | + def __return_max(self,a,b): |
| 90 | + if a>b: |
| 91 | + return a |
| 92 | + else: |
| 93 | + return b |
| 94 | + |
| 95 | + def __return_from_list(self,column,ind): |
| 96 | + try: |
| 97 | + value=self.__main_dict[column][1][ind] |
| 98 | + except IndexError: |
| 99 | + value=' ' |
| 100 | + return value |
| 101 | + def delete_column(self,column_name): |
| 102 | + '''delete column using their column name''' |
| 103 | + column_name=str(column_name) |
| 104 | + if column_name in self.__sequence: |
| 105 | + self.__sequence.remove(column_name) |
| 106 | + del self.__main_dict[column_name] |
| 107 | + else: |
| 108 | + raise KeyError('column name not found') |
| 109 | + def delete_cell(self,column_name,value): |
| 110 | + '''delete of a collumn using their cell value of index number''' |
| 111 | + column_name=str(column_name) |
| 112 | + if column_name in self.__sequence: |
| 113 | + if type(value)==str: |
| 114 | + if value in self.__main_dict[column_name][1]: |
| 115 | + self.__main_dict[column_name][1].remove(value) |
| 116 | + elif type(value)==int: |
| 117 | + if value < len(self.__main_dict[column_name][1]): |
| 118 | + del self.__main_dict[column_name][1][value] |
| 119 | + else: |
| 120 | + raise TypeError('value must be a string or integer') |
| 121 | + else: |
| 122 | + raise KeyError('column name not found') |
| 123 | + ma=0 |
| 124 | + for a in self.__main_dict: |
| 125 | + if len(self.__main_dict[a][1])>ma: |
| 126 | + ma=len(self.__main_dict[a][1]) |
| 127 | + self.__max_row=ma |
| 128 | + |
| 129 | + |
0 commit comments