Test Failed
Push — master ( 206750...bcacd1 )
by Daniel
02:10
created

tableau_hyper_management.TableauHyperApiExtraLogic   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 121
Duplicated Lines 88.43 %

Importance

Changes 0
Metric Value
wmc 16
eloc 97
dl 107
loc 121
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A TableauHyperApiExtraLogic.fn_run_hyper_creation() 7 7 2
A TableauHyperApiExtraLogic.fn_convert_to_hyper_types() 20 20 2
A TableauHyperApiExtraLogic.fn_create_hyper_file_from_csv() 36 36 4
A TableauHyperApiExtraLogic.fn_rebuild_csv_content_for_hyper() 15 15 5
A TableauHyperApiExtraLogic.fn_build_hyper_columns_for_csv() 23 23 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import pandas as pd
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
3
from . import BasicNeeds as ClassBN
4
from . import TypeDetermination as ClassTD
5
6
from tableauhyperapi import HyperProcess, Telemetry, \
7
    Connection, CreateMode, \
8
    NOT_NULLABLE, NULLABLE, SqlType, TableDefinition, \
9
    Inserter, \
10
    TableName, \
11
    HyperException
12
13
14 View Code Duplication
class TableauHyperApiExtraLogic:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
15
16
    def fn_build_hyper_columns_for_csv(self, detected_csv_structure, verbose):
17
        list_hyper_table_columns_to_return = []
18
        for current_field_structure in detected_csv_structure:
19
            list_hyper_table_columns_to_return.append(current_field_structure['order'])
20
            current_column_type = self.fn_convert_to_hyper_types(current_field_structure['type'])
21
            ClassBN.fn_optional_print(ClassBN, verbose, 'Column '
22
                                      + str(current_field_structure['order']) + ' having name "'
23
                                      + current_field_structure['name'] + '" and type "'
24
                                      + current_field_structure['type'] + '" will become "'
25
                                      + str(current_column_type) + '"')
26
            if current_field_structure['nulls'] == 0:
27
                list_hyper_table_columns_to_return[current_field_structure['order']] = TableDefinition.Column(
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (110/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
28
                    name = current_field_structure['name'],
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
29
                    type = current_column_type,
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
30
                    nullability = NOT_NULLABLE
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
31
                )
32
            else:
33
                list_hyper_table_columns_to_return[current_field_structure['order']] = TableDefinition.Column(
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (110/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
34
                    name = current_field_structure['name'],
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
35
                    type = current_column_type,
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
36
                    nullability = NULLABLE
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
37
                )
38
        return list_hyper_table_columns_to_return
39
40
    @staticmethod
41
    def fn_convert_to_hyper_types(given_type):
42
        switcher = {
43
            'empty': SqlType.text(),
44
            'int': SqlType.big_int(),
45
            'float-USA': SqlType.double(),
46
            'date-iso8601': SqlType.date(),
47
            'date-USA': SqlType.date(),
48
            'time-24': SqlType.time(),
49
            'time-24-micro-sec': SqlType.time(),
50
            'time-USA': SqlType.time(),
51
            'time-USA-micro-sec': SqlType.time(),
52
            'datetime-iso8601': SqlType.timestamp(),
53
            'datetime-iso8601-micro-sec': SqlType.timestamp(),
54
            'str': SqlType.text()
55
        }
56
        identified_type = switcher.get(given_type)
57
        if identified_type is None:
58
            identified_type = SqlType.text()
59
        return identified_type
60
61
    def fn_create_hyper_file_from_csv(self, input_csv_data_frame, formats_to_evaluate, output_hyper_file, verbose):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (115/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
62
        detected_csv_structure = ClassTD.fn_detect_csv_structure(ClassTD, input_csv_data_frame,
63
                                                                 formats_to_evaluate, verbose)
64
        hyper_table_columns = self.fn_build_hyper_columns_for_csv(self, detected_csv_structure, verbose)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (104/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
65
        # Starts the Hyper Process with telemetry enabled/disabled to send data to Tableau or not
66
        # To opt in, simply set telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU.
67
        # To opt out, simply set telemetry=Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU.
68
        with HyperProcess(telemetry = Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper:
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
69
            # Creates new Hyper file <output_hyper_file>
70
            # Replaces file with CreateMode.CREATE_AND_REPLACE if it already exists.
71
            with Connection(endpoint = hyper.endpoint,
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
72
                            database = output_hyper_file,
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
73
                            create_mode = CreateMode.CREATE_AND_REPLACE) as hyper_connection:
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
74
                print(f'Connection to the Hyper engine file "{output_hyper_file}" has been created.')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
75
                hyper_connection.catalog.create_schema("Extract")
76
                print('Hyper schema "Extract" has been created.')
77
                hyper_table = TableDefinition(
78
                    TableName("Extract", "Extract"),
79
                    columns = hyper_table_columns
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
80
                )
81
                hyper_connection.catalog.create_table(table_definition = hyper_table)
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
82
                print('Hyper table "Extract" has been created.')
83
                # The rows to insert into the <hyper_table> table.
84
                data_to_insert = self.fn_rebuild_csv_content_for_hyper(self, input_csv_data_frame,
85
                                                                       detected_csv_structure, verbose)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
86
                # Execute the actual insert
87
                with Inserter(hyper_connection, hyper_table) as hyper_inserter:
88
                    hyper_inserter.add_rows(rows = data_to_insert)
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
89
                    hyper_inserter.execute()
90
                # Number of rows in the <hyper_table> table.
91
                # `execute_scalar_query` is for executing a query that returns exactly one row with one column.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (111/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
92
                row_count = hyper_connection.\
93
                    execute_scalar_query(query = f'SELECT COUNT(*) FROM {hyper_table.table_name}')
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
94
                print(f'Number of rows in table {hyper_table.table_name} is {row_count}.')
95
            print('Connection to the Hyper engine file has been closed.')
96
        print('Hyper engine process has been shut down.')
97
98
    def fn_rebuild_csv_content_for_hyper(self, input_csv_data_frame, detected_fields_type, verbose):
99
        input_csv_data_frame.replace(to_replace = [pd.np.nan], value = [None], inplace = True)
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
100
        # Cycle through all found columns
101
        for current_field in detected_fields_type:
102
            fld_nm = current_field['name']
103
            ClassBN.fn_optional_print(ClassBN, verbose, 'Column ' + fld_nm + ' '
104
                                      + 'has panda_type = ' + str(current_field['panda_type']) + ' '
105
                                      + 'and ' + str(current_field['type']))
106
            if current_field['panda_type'] == 'float64' and current_field['type'] == 'int':
107
                input_csv_data_frame[fld_nm] = input_csv_data_frame[fld_nm].replace(to_replace = [pd.np.nan, '.0'],
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (115/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
108
                                                                                    value = [None, ''],
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
109
                                                                                    inplace = True)
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
110
            elif current_field['type'] in ('datetime-iso8601', 'datetime-iso8601-micro-sec'):
111
                input_csv_data_frame[fld_nm] = pd.to_datetime(input_csv_data_frame[fld_nm])
112
        return input_csv_data_frame.values
113
114
    def fn_run_hyper_creation(self, input_csv_data_frame, formats_to_evaluate, output_hyper_file, verbose):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (107/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
115
        try:
116
            self.fn_create_hyper_file_from_csv(self, input_csv_data_frame, formats_to_evaluate, output_hyper_file,
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (114/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
117
                                               verbose)
118
        except HyperException as ex:
119
            print(ex)
120
            exit(1)
121