findTimePointUpdateDatabase()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 54
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 54
rs 7.952
c 0
b 0
f 0
cc 7
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
2
import os
3
import json
4
import argparse
5
import getpass
6
import logging
7
from LocalDB_query import validateLocalTableAndSchema, check_value, update_entry, check_header_index
8
from LORIS_timepoint import findLatestTimePoint
9
from LocalDB_schema import *
10
11
12
def findTimePointUpdateDatabase(token, DCCID, database_path, table_name):
13
    """
14
    Find the timepoint of the subject, IF they exist, then update the given database by finding its relevant MRN.
15
    :param token:
16
    :param DCCID:
17
    :param database_path:
18
    :param table_name:
19
    :param ColumnName:
20
    :param ColumnValue:
21
    :return:
22
    """
23
24
    logger = logging.getLogger('Intermediate_findTimePointUpdateDatabase')
25
26
    MRN = -1
27
28
29
    # todo: permission must be checked to ensure we are not geting 401 error! which is an access issue.
30
    # todo: 2018-07-24 continue debug this code about entry creation.
31
    time_point = findLatestTimePoint(token, DCCID)
32
33
    # Timepoint Check
34
    if time_point is None:
35
        return False, "No timepoint found"
36
    else:
37
        logger.info("Timepoint retrieved okay:" + time_point)
38
39
    # Validate table and schema congruency
40
    success, reason = validateLocalTableAndSchema(database_path, table_name, "DCCID")
41
    if not success:
42
        return False, reason
43
44
    # Check local database for the rows with matching DCCID.
45
    success, subject_rows = check_value(database_path, table_name, "DCCID", DCCID)
46
    if not success:
47
        return False, "No entries with this DCCID!"
48
49
    # Recall each row WILL conform to schema.
50
    for row in subject_rows:
51
52
        # Get MRN:
53
        DCCID_table_index = check_header_index(database_path, table_name, "DCCID")
54
55
        assert (str(DCCID) == str(row[DCCID_table_index]))
56
57
        # Need to update these rows with the new value.
58
        MRN = row[0] # the identifier of the record.
59
60
        try:
61
            update_entry(database_path, table_name, CNBP_schema_keyfield, MRN, "Timepoint", time_point)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable CNBP_schema_keyfield does not seem to be defined.
Loading history...
62
        except IOError:
63
            return False, "Check the database is not read only. "
64
65
    return True, "Timepoint successfully updated in the local SQLite database."
66
67
68
if __name__ == '__main__':
69
70
    # Unit test
71
    from test_Intermediate_Server import test_updateLocalTimepoint
72
    test_updateLocalTimepoint()
73