Passed
Pull Request — master (#2)
by Yang
01:44
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
#This file create a SQLite local database for CNBP requirement.
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
#Creation: 2018-07-11T160228EST
3
#Author: Yang Ding
4
5
import sys
6
import sqlite3
7
import logging
8
from pathlib import Path
9
from LocalDB.schema import *
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...
Coding Style introduced by
The usage of wildcard imports like LocalDB.schema should generally be avoided.
Loading history...
10
11
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
12
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...
13
14
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...
15
    """
16
    Create the local database based on sceham.
17
    :param PathString:
18
    :param TableName:
19
    :param KeyFieldString:
20
    :param ColumnsNameTypeList:
21
    :return:
22
    """
23
24
25
    # if SQL already exist, quit script.
26
    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...
27
28
    # check if path is a fiela nd exist.
29
    if SQLPath.is_file():
30
        logger.info('SQLite database file already exist. Not gonna mess with it!')
31
        return False
32
        '''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...
33
        '''os.remove(sqliteFile)
34
        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...
35
36
    #Create the PRIMARY KEY column.
37
    KeyFieldType = CNBP_schema_keyfield_type  # column data type
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable CNBP_schema_keyfield_type does not seem to be defined.
Loading history...
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...
Comprehensibility Best Practice introduced by
Undefined variable 'CNBP_schema_keyfield_type'
Loading history...
38
39
    #Try to connect the database to start the process:
40
41
    try:
42
        # Create on Connecting to the database file
43
        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...
44
45
        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...
46
47
        logger.info('Creating PRIMARY KEY DBKEY column in database.')
48
49
        # 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...
50
        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...
51
52
        logger.info('PRIMARY KEY DBKEY column successfully created in database.')
53
54
        logger.info('Creating secondary columns in database.')
55
56
        # Adding accessory columns via a loop
57
        for column in ColumnsNameTypeList:
58
            if (column[1] != "TEXT" and
59
                column[1] != "REAL" and
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
60
                column[1] != "BLOB" and
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
61
                #column[1] != "NULL" and
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
62
                column[1] != "INTEGER"):
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
63
                continue  # skip iteration is the data type is not specified properly.
64
            else:
65
                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...
66
67
        logger.info('Secondary columns created in database.')
68
69
        # Committing changes and closing the connection to the database file
70
        ConnectedDatabase.commit()
71
        ConnectedDatabase.close()
72
    except:
73
        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...
74
        raise IOError
75
    return True
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...