| Conditions | 8 |
| Total Lines | 62 |
| Code Lines | 27 |
| 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 sys |
||
| 16 | def LocalDBCreate(PathString, TableName, KeyFieldString, ColumnsNameTypeList): |
||
| 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) |
||
| 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''' |
||
| 35 | '''os.remove(sqliteFile) |
||
| 36 | logger.info('DEBUG: database file already exist. Deleted it!')''' |
||
| 37 | |||
| 38 | #Create the PRIMARY KEY column. |
||
| 39 | KeyFieldType = CNBP_schema_keyfield_type # column data type |
||
| 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) |
||
| 46 | |||
| 47 | c = ConnectedDatabase.cursor() |
||
| 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) |
||
| 52 | c.execute('CREATE TABLE {tn} ({nf} {ft} PRIMARY KEY)'.format(tn=TableName, nf=KeyFieldString, ft=KeyFieldType)) |
||
| 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 |
||
| 62 | column[1] != "BLOB" and |
||
| 63 | #column[1] != "NULL" and |
||
| 64 | column[1] != "INTEGER"): |
||
| 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])) |
||
| 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: |
||
| 75 | logger.info('SQLite database creation/update issue, suspect schema non-compliant SQLite database. Did you corrupt this SQLite database somehow?') |
||
| 76 | raise IOError |
||
| 77 | return True |
||
| 78 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.