| Conditions | 4 |
| Total Lines | 56 |
| Code Lines | 34 |
| 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 csv |
||
| 109 | def fn_run_create_hyper_file_from_csv(input_csv_file, |
||
| 110 | csv_field_separator, |
||
| 111 | output_hyper_file, |
||
| 112 | verbose): |
||
| 113 | detected_csv_structure = TypeDetermination.fn_detect_csv_structure(TypeDetermination, |
||
| 114 | input_csv_file, |
||
| 115 | csv_field_separator, |
||
| 116 | verbose) |
||
| 117 | hyper_table_columns = TableauHyperApiExtraLogic.fn_build_hyper_columns_for_csv(input_csv_file, |
||
| 118 | csv_field_separator, |
||
| 119 | detected_csv_structure, |
||
| 120 | verbose) |
||
| 121 | # Starts the Hyper Process with telemetry enabled/disabled to send data to Tableau or not |
||
| 122 | # To opt in, simply set telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU. |
||
| 123 | # To opt out, simply set telemetry=Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU. |
||
| 124 | with HyperProcess(telemetry=Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper: |
||
| 125 | # Creates new Hyper file <output_hyper_file> |
||
| 126 | # Replaces file with CreateMode.CREATE_AND_REPLACE if it already exists. |
||
| 127 | with Connection(endpoint=hyper.endpoint, |
||
| 128 | database=output_hyper_file, |
||
| 129 | create_mode=CreateMode.CREATE_AND_REPLACE) as hyper_connection: |
||
| 130 | hyper_connection.catalog.create_schema("Extract") |
||
| 131 | hyper_table = TableDefinition( |
||
| 132 | name=TableName("Extract", "Extract"), |
||
| 133 | columns=hyper_table_columns |
||
| 134 | ) |
||
| 135 | hyper_connection.catalog.create_table(table_definition=hyper_table) |
||
| 136 | print("The connection to the Hyper engine file has been created.") |
||
| 137 | ''' |
||
| 138 | VERDICT: does not work as DOUBLE or INT are not accepting empty values... :-( |
||
| 139 | print("I am about to execute command: " |
||
| 140 | + f"COPY {hyper_table.table_name} from {escape_string_literal(input_csv_file)} with " |
||
| 141 | f"(format csv, NULL 'NULL', delimiter '{csv_field_separator}', header)") |
||
| 142 | # Load all rows into "Customers" table from the CSV file. |
||
| 143 | # `execute_command` executes a SQL statement and returns the impacted row count. |
||
| 144 | count_in_target_table = hyper_connection.execute_command( |
||
| 145 | command=f"COPY {hyper_table.table_name} from {escape_string_literal(input_csv_file)} with " |
||
| 146 | f"(format csv, NULL 'NULL', delimiter '{csv_field_separator}', header)") |
||
| 147 | print(f"The number of rows in table {hyper_table.table_name} is {count_in_target_table}.") |
||
| 148 | ''' |
||
| 149 | # The rows to insert into the <hyper_table> table. |
||
| 150 | data_to_insert = TableauHyperApiExtraLogic.fn_rebuild_csv_content_for_hyper(input_csv_file, |
||
| 151 | csv_field_separator, |
||
| 152 | detected_csv_structure, |
||
| 153 | verbose) |
||
| 154 | # Execute the actual insert |
||
| 155 | with Inserter(hyper_connection, hyper_table) as hyper_inserter: |
||
| 156 | hyper_inserter.add_rows(rows=data_to_insert) |
||
| 157 | hyper_inserter.execute() |
||
| 158 | # Number of rows in the <hyper_table> table. |
||
| 159 | # `execute_scalar_query` is for executing a query that returns exactly one row with one column. |
||
| 160 | row_count = hyper_connection.\ |
||
| 161 | execute_scalar_query(query=f"SELECT COUNT(*) FROM {hyper_table.table_name}") |
||
| 162 | print(f"The number of rows in table {hyper_table.table_name} is {row_count}.") |
||
| 163 | print("The connection to the Hyper file has been closed.") |
||
| 164 | print("The Hyper process has been shut down.") |