Skip to content

Commit 954cd27

Browse files
committed
Added dump function to compare and fixed bugs, version bump to 0.2.2
1 parent b468833 commit 954cd27

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

cyaron/compare.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
22
from cyaron.consts import *
33
from cyaron.graders import CYaRonGraders
44
import subprocess
5+
import sys
6+
import io
57

68

79
class Compare:
810
@staticmethod
9-
def __compare_two(name, content, std, grader):
11+
def __compare_two(name, content, std, grader, **kwargs):
1012
(result, info) = CYaRonGraders.invoke(grader, content, std)
1113

1214
info = info if info is not None else ""
1315
status = "Correct" if result else "!!!INCORRECT!!!"
1416
print("%s: %s %s" % (name, status, info))
1517

18+
stop_on_incorrect = kwargs.get("stop_on_incorrect", False)
19+
custom_dump_data = kwargs.get("dump_data", None)
20+
if stop_on_incorrect and not result:
21+
if custom_dump_data:
22+
(dump_name, dump_lambda) = custom_dump_data
23+
with open(dump_name, "w") as f:
24+
f.write(dump_lambda())
25+
26+
with open("std.out", "w") as f:
27+
f.write(std)
28+
with open("%s.out" % name, "w") as f:
29+
f.write(content)
30+
31+
print("Relevant files dumped.")
32+
33+
sys.exit(0)
34+
35+
1636
@staticmethod
1737
def __process_file(file):
1838
if isinstance(file, IO):
@@ -33,10 +53,11 @@ def output(*args, **kwargs):
3353
(_, std) = Compare.__process_file(kwargs["std"])
3454

3555
grader = kwargs.get("grader", DEFAULT_GRADER)
56+
stop_on_incorrect = kwargs.get("stop_on_incorrect", False)
3657

3758
for file in args:
3859
(file_name, content) = Compare.__process_file(file)
39-
Compare.__compare_two(file_name, content, std, grader)
60+
Compare.__compare_two(file_name, content, std, grader, stop_on_incorrect=stop_on_incorrect)
4061

4162
@staticmethod
4263
def program(*args, **kwargs):
@@ -61,9 +82,15 @@ def program(*args, **kwargs):
6182
(_, std) = Compare.__process_file(kwargs["std"])
6283

6384
grader = kwargs.get("grader", DEFAULT_GRADER)
85+
stop_on_incorrect = kwargs.get("stop_on_incorrect", False)
6486

6587
for program_name in args:
88+
input.input_file.seek(0)
6689
content = subprocess.check_output(program_name, shell=True, stdin=input.input_file)
67-
Compare.__compare_two(program_name, content, std, grader)
6890

91+
input.input_file.seek(0)
92+
Compare.__compare_two(program_name, content, std, grader,
93+
stop_on_incorrect=stop_on_incorrect,
94+
dump_data=("error_input.in", lambda: input.input_file.read())) # Lazy dump
6995

96+
input.input_file.seek(0, 2)

cyaron/io.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ class IO(object):
88
"""Class IO: IO tool class. It will process the input and output files."""
99
def __init__(self, *args, **kwargs):
1010
"""__init__(self, *args, **kwargs) -> None
11-
(str,str) args -> The file names of input file and output file. Index 0 is the name of input file, and index 1 is for ouput file
11+
(str,str) args -> The file names of input file and output file. Index 0 is the name of input file, and index 1 is for output file
1212
**kwargs:
13-
str file_perfix -> the perfix for the input and output files
13+
str file_prefix -> the prefix for the input and output files
1414
int data_id -> the id of the data. if it's None, the file names will not contain the id.
1515
str input_suffix = ".in" -> the suffix of the input file
1616
str output_suffix = ".out" -> the suffix of the output file
1717
Examples:
1818
IO("a","b") -> create input file "a" and output file "b"
1919
IO("a.in","b.out") -> create input file "a.in" and output file "b.out"
20-
IO(file_perfix="data") -> create input file "data.in" and output file "data.out"
21-
IO(file_perfix="data",data_id=1) -> create input file "data1.in" and output file "data1.out"
22-
IO(file_perfix="data",input_suffix=".input") -> create input file "data.input" and output file "data.out"
23-
IO(file_perfix="data",output_suffix=".output") -> create input file "data.in" and output file "data.output"
24-
IO(file_perfix="data",data_id=2,input_suffix=".input") -> create input file "data2.input" and output file "data2.out"
20+
IO(file_prefix="data") -> create input file "data.in" and output file "data.out"
21+
IO(file_prefix="data",data_id=1) -> create input file "data1.in" and output file "data1.out"
22+
IO(file_prefix="data",input_suffix=".input") -> create input file "data.input" and output file "data.out"
23+
IO(file_prefix="data",output_suffix=".output") -> create input file "data.in" and output file "data.output"
24+
IO(file_prefix="data",data_id=2,input_suffix=".input") -> create input file "data2.input" and output file "data2.out"
2525
"""
2626
if len(args) == 0:
2727
if not "file_prefix" in kwargs:

examples/test_compare.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from cyaron import *
33

44
io = IO()
5-
io.input_write("1\n")
5+
io.input_write("1111\n")
66

7-
Compare.program("echo 1", input=io, std_program="cat")
7+
Compare.program("echo 1111", input=io, std_program="cat")
88
Compare.program("echo 2", input=io, std_program="cat")
99

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='cyaron',
5-
version='0.2.1',
5+
version='0.2.2',
66
keywords='olympic informatics luogu aqours cyaron lovelive sunshine online judge',
77
description='CYaRon: Yet Another Random Olympic-iNformatics test data generator, A library for automatically generating test data for Online Judge, Olympic Informatics or automatic application testing',
88
license='LGPLv3',

0 commit comments

Comments
 (0)