Skip to content

Commit 1a9744c

Browse files
committed
Discard changes of compare.py
1 parent df0f379 commit 1a9744c

File tree

1 file changed

+103
-29
lines changed

1 file changed

+103
-29
lines changed

cyaron/compare.py

Lines changed: 103 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, name, mismatch):
1717
self.mismatch = mismatch
1818

1919
def __str__(self):
20-
return 'In program: \'{}\'. {}'.format(self.name,self.mismatch)
20+
return "In program: '{}'. {}".format(self.name, self.mismatch)
2121

2222

2323
class Compare:
@@ -37,7 +37,7 @@ def __process_file(file):
3737
file.output_file.seek(0)
3838
return file.output_filename, file.output_file.read()
3939
else:
40-
with open(file, "r", newline='\n') as f:
40+
with open(file, "r", newline="\n") as f:
4141
return file, f.read()
4242

4343
@staticmethod
@@ -50,26 +50,43 @@ def __normal_max_workers(workers):
5050

5151
@classmethod
5252
def output(cls, *files, **kwargs):
53-
kwargs = unpack_kwargs('output', kwargs, ('std', ('grader', DEFAULT_GRADER), ('max_workers', -1),
54-
('job_pool', None), ('stop_on_incorrect', None)))
55-
std = kwargs['std']
56-
grader = kwargs['grader']
57-
max_workers = kwargs['max_workers']
58-
job_pool = kwargs['job_pool']
59-
if kwargs['stop_on_incorrect'] is not None:
53+
kwargs = unpack_kwargs(
54+
"output",
55+
kwargs,
56+
(
57+
"std",
58+
("grader", DEFAULT_GRADER),
59+
("max_workers", -1),
60+
("job_pool", None),
61+
("stop_on_incorrect", None),
62+
),
63+
)
64+
std = kwargs["std"]
65+
grader = kwargs["grader"]
66+
max_workers = kwargs["max_workers"]
67+
job_pool = kwargs["job_pool"]
68+
if kwargs["stop_on_incorrect"] is not None:
6069
log.warn("parameter stop_on_incorrect is deprecated and has no effect.")
6170

6271
if (max_workers is None or max_workers >= 0) and job_pool is None:
6372
max_workers = cls.__normal_max_workers(max_workers)
6473
try:
6574
from concurrent.futures import ThreadPoolExecutor
75+
6676
with ThreadPoolExecutor(max_workers=max_workers) as job_pool:
67-
return cls.output(*files, std=std, grader=grader, max_workers=max_workers, job_pool=job_pool)
77+
return cls.output(
78+
*files,
79+
std=std,
80+
grader=grader,
81+
max_workers=max_workers,
82+
job_pool=job_pool
83+
)
6884
except ImportError:
6985
pass
7086

7187
def get_std():
7288
return cls.__process_file(std)[1]
89+
7390
if job_pool is not None:
7491
std = job_pool.submit(get_std).result()
7592
else:
@@ -86,61 +103,118 @@ def do(file):
86103

87104
@classmethod
88105
def program(cls, *programs, **kwargs):
89-
kwargs = unpack_kwargs('program', kwargs, ('input', ('std', None), ('std_program', None),
90-
('grader', DEFAULT_GRADER), ('max_workers', -1),
91-
('job_pool', None), ('stop_on_incorrect', None)))
92-
input = kwargs['input']
93-
std = kwargs['std']
94-
std_program = kwargs['std_program']
95-
grader = kwargs['grader']
96-
max_workers = kwargs['max_workers']
97-
job_pool = kwargs['job_pool']
98-
if kwargs['stop_on_incorrect'] is not None:
106+
kwargs = unpack_kwargs(
107+
"program",
108+
kwargs,
109+
(
110+
"input",
111+
("std", None),
112+
("std_program", None),
113+
("grader", DEFAULT_GRADER),
114+
("max_workers", -1),
115+
("job_pool", None),
116+
("stop_on_incorrect", None),
117+
),
118+
)
119+
input = kwargs["input"]
120+
std = kwargs["std"]
121+
std_program = kwargs["std_program"]
122+
grader = kwargs["grader"]
123+
max_workers = kwargs["max_workers"]
124+
job_pool = kwargs["job_pool"]
125+
if kwargs["stop_on_incorrect"] is not None:
99126
log.warn("parameter stop_on_incorrect is deprecated and has no effect.")
100127

101128
if (max_workers is None or max_workers >= 0) and job_pool is None:
102129
max_workers = cls.__normal_max_workers(max_workers)
103130
try:
104131
from concurrent.futures import ThreadPoolExecutor
132+
105133
with ThreadPoolExecutor(max_workers=max_workers) as job_pool:
106-
return cls.program(*programs, input=input, std=std, std_program=std_program, grader=grader, max_workers=max_workers, job_pool=job_pool)
134+
return cls.program(
135+
*programs,
136+
input=input,
137+
std=std,
138+
std_program=std_program,
139+
grader=grader,
140+
max_workers=max_workers,
141+
job_pool=job_pool
142+
)
107143
except ImportError:
108144
pass
109145

110146
if not isinstance(input, IO):
111-
raise TypeError("expect {}, got {}".format(type(IO).__name__, type(input).__name__))
147+
raise TypeError(
148+
"expect {}, got {}".format(type(IO).__name__, type(input).__name__)
149+
)
112150
input.flush_buffer()
113151
input.input_file.seek(0)
114152

115153
if std_program is not None:
154+
116155
def get_std():
117-
with open(os.dup(input.input_file.fileno()), 'r', newline='\n') as input_file:
118-
content = make_unicode(subprocess.check_output(std_program, shell=(not list_like(std_program)), stdin=input.input_file, universal_newlines=True))
156+
with open(
157+
os.dup(input.input_file.fileno()), "r", newline="\n"
158+
) as input_file:
159+
content = make_unicode(
160+
subprocess.check_output(
161+
std_program,
162+
shell=(not list_like(std_program)),
163+
stdin=input.input_file,
164+
universal_newlines=True,
165+
)
166+
)
119167
input_file.seek(0)
120168
return content
169+
121170
if job_pool is not None:
122171
std = job_pool.submit(get_std).result()
123172
else:
124173
std = get_std()
125174
elif std is not None:
175+
126176
def get_std():
127177
return cls.__process_file(std)[1]
178+
128179
if job_pool is not None:
129180
std = job_pool.submit(get_std).result()
130181
else:
131182
std = get_std()
132183
else:
133-
raise TypeError('program() missing 1 required non-None keyword-only argument: \'std\' or \'std_program\'')
184+
raise TypeError(
185+
"program() missing 1 required non-None keyword-only argument: 'std' or 'std_program'"
186+
)
134187

135188
def do(program_name):
136189
timeout = None
137-
if list_like(program_name) and len(program_name) == 2 and int_like(program_name[-1]):
190+
if (
191+
list_like(program_name)
192+
and len(program_name) == 2
193+
and int_like(program_name[-1])
194+
):
138195
program_name, timeout = program_name
139-
with open(os.dup(input.input_file.fileno()), 'r', newline='\n') as input_file:
196+
with open(
197+
os.dup(input.input_file.fileno()), "r", newline="\n"
198+
) as input_file:
140199
if timeout is None:
141-
content = make_unicode(subprocess.check_output(program_name, shell=(not list_like(program_name)), stdin=input_file, universal_newlines=True))
200+
content = make_unicode(
201+
subprocess.check_output(
202+
program_name,
203+
shell=(not list_like(program_name)),
204+
stdin=input_file,
205+
universal_newlines=True,
206+
)
207+
)
142208
else:
143-
content = make_unicode(subprocess.check_output(program_name, shell=(not list_like(program_name)), stdin=input_file, universal_newlines=True, timeout=timeout))
209+
content = make_unicode(
210+
subprocess.check_output(
211+
program_name,
212+
shell=(not list_like(program_name)),
213+
stdin=input_file,
214+
universal_newlines=True,
215+
timeout=timeout,
216+
)
217+
)
144218
input_file.seek(0)
145219
cls.__compare_two(program_name, content, std, grader)
146220

0 commit comments

Comments
 (0)