Passed
Pull Request — master (#2)
by Yang
02:09
created

Python.LocalDB.create.LocalDBCreate()   B

Complexity

Conditions 8

Size

Total Lines 62
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 62
rs 7.3333
c 0
b 0
f 0
cc 8
nop 4

How to fix   Long Method   

Long Method

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:

1
import sys
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
import sqlite3
3
import logging
4
from pathlib import Path
5
6
from LocalDB.schema import CNBP_schema_keyfield_type
0 ignored issues
show
Bug introduced by
The name schema does not seem to exist in module LocalDB.
Loading history...
introduced by
Unable to import 'LocalDB.schema'
Loading history...
7
8
# This file create a SQLite local database for CNBP requirement.
9
# Creation: 2018-07-11T160228EST
10
# Author: Yang Ding
11
12
13
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
14
logger = logging.getLogger('LocalDBCreate')
0 ignored issues
show
Coding Style Naming introduced by
The name logger does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
15
16
def LocalDBCreate(PathString, TableName, KeyFieldString, ColumnsNameTypeList):
0 ignored issues
show
Coding Style Naming introduced by
The name LocalDBCreate does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name PathString does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name TableName does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name KeyFieldString does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name ColumnsNameTypeList does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
17
    """
18
    Create the local database based on sceham.
19
    :param PathString:
20
    :param TableName:
21
    :param KeyFieldString:
22
    :param ColumnsNameTypeList:
23
    :return:
24
    """
25
26
27
    # if SQL already exist, quit script.
28
    SQLPath = Path(PathString)
0 ignored issues
show
Coding Style Naming introduced by
The name SQLPath does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
29
30
    # check if path is a fiela nd exist.
31
    if SQLPath.is_file():
32
        logger.info('SQLite database file already exist. Not gonna mess with it!')
33
        return False
34
        '''Delete current database! During testing only'''
0 ignored issues
show
Unused Code introduced by
This code does not seem to be reachable.
Loading history...
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
35
        '''os.remove(sqliteFile)
36
        logger.info('DEBUG: database file already exist. Deleted it!')'''
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
37
38
    #Create the PRIMARY KEY column.
39
    KeyFieldType = CNBP_schema_keyfield_type  # column data type
0 ignored issues
show
Coding Style Naming introduced by
The name KeyFieldType does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
40
41
    #Try to connect the database to start the process:
42
43
    try:
44
        # Create on Connecting to the database file
45
        ConnectedDatabase = sqlite3.connect(PathString)
0 ignored issues
show
Coding Style Naming introduced by
The name ConnectedDatabase does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
46
47
        c = ConnectedDatabase.cursor()
0 ignored issues
show
Coding Style Naming introduced by
The name c does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
48
49
        logger.info('Creating PRIMARY KEY DBKEY column in database.')
50
51
        # Creating a new SQLite table with DBKey column (inspired by: https://sebastianraschka.com/Articles/2014_sqlite_in_python_tutorial.html)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (144/100).

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

Loading history...
52
        c.execute('CREATE TABLE {tn} ({nf} {ft} PRIMARY KEY)'.format(tn=TableName, nf=KeyFieldString, ft=KeyFieldType))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (119/100).

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

Loading history...
53
54
        logger.info('PRIMARY KEY DBKEY column successfully created in database.')
55
56
        logger.info('Creating secondary columns in database.')
57
58
        # Adding accessory columns via a loop
59
        for column in ColumnsNameTypeList:
60
            if (column[1] != "TEXT" and
61
                column[1] != "REAL" and
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
62
                column[1] != "BLOB" and
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
63
                #column[1] != "NULL" and
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
64
                column[1] != "INTEGER"):
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
65
                continue  # skip iteration is the data type is not specified properly.
66
            else:
67
                c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}".format(tn=TableName, cn=column[0], ct=column[1]))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

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

Loading history...
68
69
        logger.info('Secondary columns created in database.')
70
71
        # Committing changes and closing the connection to the database file
72
        ConnectedDatabase.commit()
73
        ConnectedDatabase.close()
74
    except Exception as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Unused Code introduced by
The variable e seems to be unused.
Loading history...
75
        logger.info('SQLite database creation/update issue, suspect schema non-compliant SQLite database. Did you corrupt this SQLite database somehow?')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (153/100).

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

Loading history...
76
        raise IOError
77
    return True
78