-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
106 lines (86 loc) · 3.11 KB
/
Copy pathtest.py
File metadata and controls
106 lines (86 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import time
import random
import string
import json
import os
CORRECTION_FILE = '/tmp/exercise'
def load_variables_from_file(file_path):
variables = {}
try:
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if line and '=' in line:
variable, value = line.split('=')
variables[variable.strip()] = value.strip()
except FileNotFoundError:
pass
return variables
def introduce_error(prob):
if random.random() < prob:
raise RuntimeError("Error introduced with probability {:.2f}".format(prob))
def delay_execution(delay):
time.sleep(delay/1000)
def complete_length(response, length):
if length == 0: return response
# 4 is the length of the comma, the space and the quotes for the
# new element in the list
EXTRA_CHARACTERS_FOR_ITEM = 4
PADDING = (response_size - len(json.dumps(response)) - EXTRA_CHARACTERS_FOR_ITEM)
new_response = dict(response)
new_response['comments'].append('*'*PADDING)
return new_response
def generate_random_text(num_lines = random.randint(0, 10), line_length = random.randint(2, 50)):
random_text = []
for _ in range(num_lines):
line = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation + ' ', k=line_length))
random_text.append(line)
return '\n'.join(random_text)
def generate_response_with_separators(json_text):
START_SEPARATOR = '-----BEGIN CORRECTOMATIC RESPONSE-----'
END_SEPARATOR = '-----END CORRECTOMATIC RESPONSE-----'
response = [ generate_random_text() ]
response.append(START_SEPARATOR)
response.append(json_text)
response.append(END_SEPARATOR)
response.append(generate_random_text())
return '\n'.join(response)
def generate_response_text(response, type='json'):
json_text = json.dumps(response)
if type == 'json':
return json_text
else:
return generate_response_with_separators(json_text)
# Main
response = {}
try:
configuration = load_variables_from_file(CORRECTION_FILE)
delay = int(configuration.get('DELAY', 0))
error_probability = float(configuration.get('ERROR_PROBABILITY', 0))
response_size = int(configuration.get('RESPONSE_SIZE', 0))
response_type = configuration.get('RESPONSE_TYPE', 'json')
introduce_error(error_probability)
delay_execution(delay)
except RuntimeError as e:
response = {
'success': False,
'error': str(e)
}
print(json.dumps(response))
exit(1)
response = {
'success': True,
'grade': 100,
'comments': [
f'DELAY: {delay}',
f'ERROR_PROBABILITY: {error_probability}'
]
}
def addParamsToResponse(response, *param_names):
for param_name in param_names:
param_value = os.environ.get(param_name)
if param_value:
response['comments'].append(f'{param_name}: {param_value}')
addParamsToResponse(response, 'correctomatic_param_1', 'correctomatic_param_2')
completed_response = complete_length(response, response_size)
print(generate_response_text(completed_response, response_type))