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

Python.LORIS.timepoint   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 54
dl 0
loc 112
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A findLatestTimePoint() 0 20 4
A visit_number_extraction() 0 16 2
A createTimepoint() 0 15 1
A increaseTimepoint() 0 31 5
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 os
3
import json
4
import logging
5
from dotenv import load_dotenv
6
from LORIS.helper import number_extraction
0 ignored issues
show
introduced by
Unable to import 'LORIS.helper'
Loading history...
7
from LORIS.query import getCNBP, putCNBP, login
0 ignored issues
show
introduced by
Unable to import 'LORIS.query'
Loading history...
8
from LORIS.candidates import check_DCCID, checkDCCIDExist
0 ignored issues
show
introduced by
Unable to import 'LORIS.candidates'
Loading history...
9
10
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
11
12
13
def visit_number_extraction(string):
14
    """
15
    A wrapper for number_extraction by calling it on a string and then return the latest one.
16
    Used to deal with visitnumber list.
17
    :param string:
18
    :return:
19
    """
20
    logger = logging.getLogger('visit_number_extraction')
0 ignored issues
show
Unused Code introduced by
The variable logger seems to be unused.
Loading history...
21
22
    number_extracted = number_extraction(string)
23
24
    #return last number from the timepoint string: usually it should be V2 or T3 things like that.
25
    if len(number_extracted) > 1:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
26
        return number_extracted[len(number_extracted)-1]
27
    else:
28
        return number_extracted[0]
29
30
31
def findLatestTimePoint(token, DCCID):
0 ignored issues
show
Coding Style Naming introduced by
The name findLatestTimePoint 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 DCCID 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...
Comprehensibility Bug introduced by
token is re-defining a name which is already available in the outer-scope (previously defined on line 107).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
32
    '''
33
    Find and return the latest timepoint. Note that since DCCID exist, the record MUST ALREADY exist within the local SQLite database!
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (134/100).

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

Loading history...
34
    :param DCCID:
35
    :return:
36
    '''
37
    logger = logging.getLogger('')
0 ignored issues
show
Unused Code introduced by
The variable logger seems to be unused.
Loading history...
38
    assert(check_DCCID(DCCID)) # Ensure it actually first exist.
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after assert.
Loading history...
39
40
    response_success, candidate_json = getCNBP(token, r"candidates/" + str(DCCID)) # should exist as earlier check revealed.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (124/100).

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

Loading history...
41
42
    # preliminary exit condition
43
    if not response_success:
44
        return response_success, None
45
46
    candidate_visits_list = candidate_json.get("Visits")
47
48
    if len(candidate_visits_list) > 0:
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) as condition value
Loading history...
49
        return candidate_visits_list[len(candidate_visits_list)-1] # return the LAST TIME POINT!
50
    return None
51
52
def increaseTimepoint(token, DCCID):
0 ignored issues
show
Coding Style Naming introduced by
The name increaseTimepoint 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 DCCID 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...
Comprehensibility Bug introduced by
token is re-defining a name which is already available in the outer-scope (previously defined on line 107).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
53
    """
54
    Increment the existing timepoint by check if DCCID existed, then get the latest one, then increment its number by creating a new timepoint.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (143/100).

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

Loading history...
55
    :param token: auth token
56
    :param DCCID: the DCCID of the existing subject.
57
    :return: if creation request is successful, and what label is actually created.
58
    """
59
    #todo: must handle the special edge case where timepoint reach V10 etc where two or three more digits are there!
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (116/100).

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

Loading history...
60
61
    # ensure valid input and subject actually exist.
62
    assert (check_DCCID(DCCID))
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after assert.
Loading history...
63
    success, subject_exist = checkDCCIDExist(token, DCCID)
0 ignored issues
show
Comprehensibility Bug introduced by
success is re-defining a name which is already available in the outer-scope (previously defined on line 109).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
64
    if not subject_exist or not success:
65
        return False, None
66
67
    latest_timepoint = findLatestTimePoint(token, DCCID)
0 ignored issues
show
Comprehensibility Bug introduced by
latest_timepoint is re-defining a name which is already available in the outer-scope (previously defined on line 109).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
68
69
    if latest_timepoint is None:
70
        success = createTimepoint(token, DCCID, "V1")
71
    else:
72
        visit_number = visit_number_extraction(latest_timepoint)
73
        new_visit_number = int(visit_number) + 1
74
75
        load_dotenv()
76
        prefix = os.getenv("timepoint_prefix")
77
78
        timepoint_label = prefix + str(new_visit_number)
79
80
        success = createTimepoint(token, DCCID, timepoint_label)
81
82
    return success, timepoint_label
0 ignored issues
show
introduced by
The variable timepoint_label does not seem to be defined for all execution paths.
Loading history...
83
84
85
def createTimepoint(token, DCCID, time_point):
0 ignored issues
show
Coding Style Naming introduced by
The name createTimepoint 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 DCCID 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...
Comprehensibility Bug introduced by
token is re-defining a name which is already available in the outer-scope (previously defined on line 107).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
86
    """
87
    Create a timepoint of the given DCCID subject based on the timepoint provided.
88
    :param token:
89
    :param DCCID:
90
    :param time_point:
91
    :return:
92
    """
93
    endpoint = r"/candidates/"+str(DCCID)+r"/"+time_point
94
    MetaData = {"CandID": DCCID, "Visit": time_point, "Battery":"CNBP"} # default to CNBP for NOW
0 ignored issues
show
Coding Style Naming introduced by
The name MetaData 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...
95
    Meta = {"Meta": MetaData}
0 ignored issues
show
Coding Style Naming introduced by
The name Meta 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...
96
    JSON = json.dumps(Meta)
0 ignored issues
show
Coding Style Naming introduced by
The name JSON 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...
97
    success, response = putCNBP(token, endpoint, JSON)
0 ignored issues
show
Comprehensibility Bug introduced by
success is re-defining a name which is already available in the outer-scope (previously defined on line 109).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Unused Code introduced by
The variable response seems to be unused.
Loading history...
98
    # response should be null!
99
    return success
100
101
102
# Only executed when running directly.
103
if __name__ == '__main__':
104
    # print(login())
105
    # getCNBP("projects")
106
    # assert(checkPSCIDExist("CNBP0020002"))
107
    Success, token = login()
0 ignored issues
show
Coding Style Naming introduced by
The name Success 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...
Coding Style Naming introduced by
The name token 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...
108
    # createTimePoint(token, 559776, "V9")
109
    success, latest_timepoint = increaseTimepoint(token, 635425)
0 ignored issues
show
Coding Style Naming introduced by
The name success 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...
Coding Style Naming introduced by
The name latest_timepoint 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...
110
    print (success)
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
Loading history...
111
    print (latest_timepoint)
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
Loading history...
112
    # print("Test complete")
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...