|
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
|
|
|
""" |
|
11
|
|
|
----- |
|
12
|
|
|
examples: |
|
13
|
|
|
|
|
14
|
|
|
@let |
|
15
|
|
|
output = ['from datetime import datetime\\n', 'import numpy as np\\n'] |
|
16
|
|
|
@end |
|
17
|
|
|
1) stack_imports(['from datetime import datetime ', ' import numpy as np ']) -> output |
|
18
|
|
|
----- |
|
19
|
|
|
:param import_statements: |
|
20
|
|
|
:return: |
|
21
|
|
|
""" |
|
22
|
|
|
return [ |
|
23
|
|
|
import_statement.strip() + '\n' |
|
24
|
|
|
for import_statement in import_statements |
|
25
|
|
|
if len(import_statement) > 0 |
|
26
|
|
|
] |
|
27
|
|
|
|
|
28
|
|
|
def get_imports_from_docstring(example_passage): |
|
29
|
|
|
""" |
|
30
|
|
|
---- |
|
31
|
|
|
examples: |
|
32
|
|
|
|
|
33
|
|
|
@let |
|
34
|
|
|
no_import_in_docstring = 'nothing to see here' |
|
35
|
|
|
docstring_with_imports = 'import numpy as np' |
|
36
|
|
|
@end |
|
37
|
|
|
|
|
38
|
|
|
1) get_imports_from_docstring(no_import_in_docstring) -> [] |
|
39
|
|
|
2) get_imports_from_docstring(docstring_with_imports) -> [] |
|
40
|
|
|
---- |
|
41
|
|
|
:param example_passage: |
|
42
|
|
|
:return: |
|
43
|
|
|
""" |
|
44
|
|
|
needed_imports = re.findall(PATTERNS.NEED_IMPORT, example_passage, re.M) |
|
45
|
|
|
needed_imports = needed_imports if len(needed_imports) > 0 else None |
|
46
|
|
|
if needed_imports is None: |
|
47
|
|
|
return [] |
|
48
|
|
|
needed_imports = ''.join(needed_imports).replace(PATTERNS.IMPORT_DEC, '').split('\n') |
|
49
|
|
|
return stack_imports(needed_imports) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def get_variables_from_docstring(example_passage): |
|
53
|
|
|
""" |
|
54
|
|
|
---- |
|
55
|
|
|
examples: |
|
56
|
|
|
1) get_variables_from_docstring("@let\\na = 1\\n@end") -> ['a = 1'] |
|
57
|
|
|
---- |
|
58
|
|
|
|
|
59
|
|
|
:param example_passage: |
|
60
|
|
|
:return: |
|
61
|
|
|
""" |
|
62
|
|
|
needed_variables = re.findall(PATTERNS.NEEDED_VARIABLES, example_passage) |
|
63
|
|
|
if len(needed_variables) == 0: |
|
64
|
|
|
return '' |
|
65
|
|
|
needed_variables = needed_variables[0] |
|
66
|
|
|
needed_variables = needed_variables.replace('@let', '') |
|
67
|
|
|
return needed_variables.split('\n') |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def stack_examples(examples_strings): |
|
71
|
|
|
""" |
|
72
|
|
|
----- |
|
73
|
|
|
examples: |
|
74
|
|
|
|
|
75
|
|
|
@let |
|
76
|
|
|
output = [{ |
|
77
|
|
|
'from': 'stack_examples("1) some_fn() -> 1")', |
|
78
|
|
|
'expect': 1 |
|
79
|
|
|
}] |
|
80
|
|
|
@end |
|
81
|
|
|
1) stack_examples([]) -> [] |
|
82
|
|
|
2) stack_examples("1) some_fn() -> 1") -> output |
|
83
|
|
|
----- |
|
84
|
|
|
:param examples_strings: |
|
85
|
|
|
:return: |
|
86
|
|
|
""" |
|
87
|
|
|
example_stack = [] |
|
88
|
|
|
for example in examples_strings: |
|
89
|
|
|
test_function, expectation = re.sub(PATTERNS.NUMBER_BULLET, '', example, 1)\ |
|
90
|
|
|
.rsplit(PATTERNS.TEST_SEP, 1) |
|
91
|
|
|
|
|
92
|
|
|
example_stack.append({ |
|
93
|
|
|
KEYS.FROM: test_function, |
|
94
|
|
|
KEYS.EXPECT: expectation |
|
95
|
|
|
}) |
|
96
|
|
|
return example_stack |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
def get_test_case_examples(example_passage): |
|
100
|
|
|
""" |
|
101
|
|
|
---- |
|
102
|
|
|
examples: |
|
103
|
|
|
|
|
104
|
|
|
@let |
|
105
|
|
|
example_passage = '1) some_fn() -> 1' |
|
106
|
|
|
output = [{ |
|
107
|
|
|
'from': 'stack_examples("1) some_fn() -> 1")', |
|
108
|
|
|
'expect': 1 |
|
109
|
|
|
}] |
|
110
|
|
|
@end |
|
111
|
|
|
1) get_test_case_examples(example_passage) -> output |
|
112
|
|
|
---- |
|
113
|
|
|
:param example_passage: |
|
114
|
|
|
:return: |
|
115
|
|
|
""" |
|
116
|
|
|
examples_strings = re.findall(PATTERNS.TEST_CASE_EXAMPLE, example_passage, re.M) |
|
117
|
|
|
examples_strings = examples_strings if len(examples_strings) > 0 else [] |
|
118
|
|
|
return stack_examples(examples_strings) |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
def get_test_from_example_passage(statements): |
|
122
|
|
|
""" |
|
123
|
|
|
---- |
|
124
|
|
|
examples: |
|
125
|
|
|
|
|
126
|
|
|
@let |
|
127
|
|
|
some_fn = lambda: 1 |
|
128
|
|
|
output = { |
|
129
|
|
|
'imports': ['import numpy as np'] |
|
130
|
|
|
'variables': ['a = 1'], |
|
131
|
|
|
'examples': [{ |
|
132
|
|
|
'from': 'some_fn()', |
|
133
|
|
|
'expect': 1 |
|
134
|
|
|
}] |
|
135
|
|
|
} |
|
136
|
|
|
@end |
|
137
|
|
|
|
|
138
|
|
|
1) get_test_from_example_passage('---\\n@needs\\nimport numpy as np\\n@end\\n\\n@let\\na = 1\\n1) some_fn() -> 1') -> output |
|
139
|
|
|
---- |
|
140
|
|
|
:param statements: |
|
141
|
|
|
:return: |
|
142
|
|
|
""" |
|
143
|
|
|
if statements is None: |
|
144
|
|
|
return None |
|
145
|
|
|
|
|
146
|
|
|
example_passage = re.findall(PATTERNS.EXAMPLE_PASSAGE, statements, re.I) |
|
147
|
|
|
example_passage = example_passage[0] if len(example_passage) > 0 else None |
|
148
|
|
|
if example_passage is None: |
|
149
|
|
|
return None |
|
150
|
|
|
import_statements = get_imports_from_docstring(example_passage) |
|
151
|
|
|
variables = get_variables_from_docstring(example_passage) |
|
152
|
|
|
examples = get_test_case_examples(example_passage) |
|
153
|
|
|
return None \ |
|
154
|
|
|
if examples is None \ |
|
155
|
|
|
else { |
|
156
|
|
|
KEYS.IMPORTS: import_statements, |
|
157
|
|
|
KEYS.VARIABLES: variables, |
|
158
|
|
|
KEYS.EXAMPLES: examples |
|
159
|
|
|
} |
|
160
|
|
|
|