Conditions | 4 |
Total Lines | 59 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import pandas as pd |
||
108 | database = output_hyper_file, |
||
109 | create_mode = CreateMode.CREATE_AND_REPLACE) as hyper_connection: |
||
110 | print("The connection to the Hyper engine file has been created.") |
||
111 | hyper_connection.catalog.create_schema("Extract") |
||
112 | print("Hyper schema Extract has been created.") |
||
113 | hyper_table = TableDefinition( |
||
114 | TableName("Extract", "Extract"), |
||
115 | columns = hyper_table_columns |
||
116 | ) |
||
117 | hyper_connection.catalog.create_table(table_definition = hyper_table) |
||
118 | print("Hyper table Extract has been created.") |
||
119 | # The rows to insert into the <hyper_table> table. |
||
120 | data_to_insert = self.fn_rebuild_csv_content_for_hyper(self, |
||
121 | input_csv_data_frame, |
||
122 | detected_csv_structure, |
||
123 | verbose) |
||
124 | # Execute the actual insert |
||
125 | with Inserter(hyper_connection, hyper_table) as hyper_inserter: |
||
126 | hyper_inserter.add_rows(rows = data_to_insert) |
||
127 | hyper_inserter.execute() |
||
128 | # Number of rows in the <hyper_table> table. |
||
129 | # `execute_scalar_query` is for executing a query that returns exactly one row with one column. |
||
130 | row_count = hyper_connection.\ |
||
131 | execute_scalar_query(query = f'SELECT COUNT(*) FROM {hyper_table.table_name}') |
||
132 | print(f'The number of rows in table {hyper_table.table_name} is {row_count}.') |
||
133 | print('The connection to the Hyper file has been closed.') |
||
134 | print('The Hyper process has been shut down.') |
||
135 | |||
136 | def fn_rebuild_csv_content_for_hyper(self, input_csv_data_frame, detected_fields_type, verbose): |
||
137 | input_csv_data_frame.replace(to_replace = [pd.np.nan], value = [None], inplace = True) |
||
138 | # Cycle through all found columns |
||
139 | for current_field in detected_fields_type: |
||
140 | fld_nm = current_field['name'] |
||
141 | if current_field['panda_type'] == 'float64' and current_field['type'] == 'int': |
||
142 | #input_csv_data_frame[fld_nm] = input_csv_data_frame[fld_nm].apply(lambda x: None if x is None else round(x, 0)) |
||
143 | input_csv_data_frame[fld_nm] = input_csv_data_frame[fld_nm].replace(to_replace = [pd.np.nan, '.0'], |
||
144 | value = [None, ''], |
||
145 | inplace = True) |
||
146 | elif current_field['type'] == 'datetime-iso8601': |
||
147 | input_csv_data_frame[fld_nm] = pd.to_datetime(input_csv_data_frame[fld_nm]) |
||
148 | _cls_bn.fn_optional_print(_cls_bn, verbose, 'Column ' + fld_nm + ' ' |
||
|
|||
149 | + 'has panda_type = ' + str(current_field['panda_type']) + ' ' |
||
150 | + 'and ' + str(current_field['type'])) |
||
151 | return input_csv_data_frame.values |
||
152 | |||
153 | def fn_run_hyper_creation(self, input_csv_data_frame, output_hyper_file, verbose): |
||
154 | try: |
||
155 | self.fn_create_hyper_file_from_csv(self, input_csv_data_frame, output_hyper_file, verbose) |
||
156 | except HyperException as ex: |
||
157 | print(ex) |
||
158 | exit(1) |
||
159 |