Completed
Push — master ( a5b230...649ac2 )
by Amresh
02:28
created

stack_imports()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import re
2
from fastest.constants import KEYS, PATTERNS
3
4
5
FUNCTION_CALL = 0
6
OUTPUT = 1
7
8
9
def stack_imports(import_statements):
10
    return [
11
        import_statement.strip() + '\n'
12
        for import_statement in import_statements
13
        if len(import_statement) > 0
14
    ]
15
16
17
def stack_variables(variable_string):
18
    if type(variable_string) is not str:
19
        return ''
20
    variables = [
21
        variable.strip()
22
        for variable in variable_string.split(PATTERNS.VAR_DEC)
23
    ]
24
    return variables
25
26
27
def get_imports_from_docstring(example_passage):
28
    needed_imports = re.findall(PATTERNS.NEED_IMPORT, example_passage, re.M)
29
    needed_imports = needed_imports if len(needed_imports) > 0 else None
30
    if needed_imports is None:
31
        return []
32
    needed_imports = ''.join(needed_imports).replace(PATTERNS.IMPORT_DEC, '').split('\n')
33
    return stack_imports(needed_imports)
34
35
36
def get_variables_from_docstring(example_passage):
37
    needed_variables = re.findall(r'@let[\s\S]+?(?=\d\))', example_passage)
38
    needed_variables = needed_variables if len(needed_variables) > 0 else []
39
    return stack_variables(''.join(needed_variables))
40
41
42
def stack_examples(examples_strings):
43
    example_stack = []
44
    for example in examples_strings:
45
        test_function, expectation = re.sub(PATTERNS.NUMBER_BULLET, '', example, 1).split(PATTERNS.TEST_SEP)
46
47
        example_stack.append({
48
            KEYS.FROM: test_function,
49
            KEYS.EXPECT: expectation
50
        })
51
    return example_stack
52
53
54
def get_test_case_examples(example_passage):
55
    examples_strings = re.findall(PATTERNS.TEST_CASE_EXAMPLE, example_passage, re.M)
56
    examples_strings = examples_strings if len(examples_strings) > 0 else None
57
    return stack_examples(examples_strings)
58
59
60
def get_test_from_example_passage(statements):
61
    example_passage = re.findall(PATTERNS.EXAMPLE_PASSAGE, statements, re.I)
62
    example_passage = example_passage[0] if len(example_passage) > 0 else None
63
    if example_passage is None:
64
        return None
65
    import_statements = get_imports_from_docstring(example_passage)
66
    variables = get_variables_from_docstring(example_passage)
67
    examples = get_test_case_examples(example_passage)
68
    return None \
69
        if examples is None \
70
        else {
71
            KEYS.IMPORTS: import_statements,
72
            KEYS.VARIABLES: variables,
73
            KEYS.EXAMPLES: examples
74
        }
75