Skip to content

Commit

Permalink
Added dump function to compare and fixed bugs, version bump to 0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
lin-toto committed May 17, 2017
1 parent b468833 commit 954cd27
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
33 changes: 30 additions & 3 deletions cyaron/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@
from cyaron.consts import *
from cyaron.graders import CYaRonGraders
import subprocess
import sys
import io


class Compare:
@staticmethod
def __compare_two(name, content, std, grader):
def __compare_two(name, content, std, grader, **kwargs):
(result, info) = CYaRonGraders.invoke(grader, content, std)

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

stop_on_incorrect = kwargs.get("stop_on_incorrect", False)
custom_dump_data = kwargs.get("dump_data", None)
if stop_on_incorrect and not result:
if custom_dump_data:
(dump_name, dump_lambda) = custom_dump_data
with open(dump_name, "w") as f:
f.write(dump_lambda())

with open("std.out", "w") as f:
f.write(std)
with open("%s.out" % name, "w") as f:
f.write(content)

print("Relevant files dumped.")

sys.exit(0)


@staticmethod
def __process_file(file):
if isinstance(file, IO):
Expand All @@ -33,10 +53,11 @@ def output(*args, **kwargs):
(_, std) = Compare.__process_file(kwargs["std"])

grader = kwargs.get("grader", DEFAULT_GRADER)
stop_on_incorrect = kwargs.get("stop_on_incorrect", False)

for file in args:
(file_name, content) = Compare.__process_file(file)
Compare.__compare_two(file_name, content, std, grader)
Compare.__compare_two(file_name, content, std, grader, stop_on_incorrect=stop_on_incorrect)

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

grader = kwargs.get("grader", DEFAULT_GRADER)
stop_on_incorrect = kwargs.get("stop_on_incorrect", False)

for program_name in args:
input.input_file.seek(0)
content = subprocess.check_output(program_name, shell=True, stdin=input.input_file)
Compare.__compare_two(program_name, content, std, grader)

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

input.input_file.seek(0, 2)
14 changes: 7 additions & 7 deletions cyaron/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ class IO(object):
"""Class IO: IO tool class. It will process the input and output files."""
def __init__(self, *args, **kwargs):
"""__init__(self, *args, **kwargs) -> None
(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
(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
**kwargs:
str file_perfix -> the perfix for the input and output files
str file_prefix -> the prefix for the input and output files
int data_id -> the id of the data. if it's None, the file names will not contain the id.
str input_suffix = ".in" -> the suffix of the input file
str output_suffix = ".out" -> the suffix of the output file
Examples:
IO("a","b") -> create input file "a" and output file "b"
IO("a.in","b.out") -> create input file "a.in" and output file "b.out"
IO(file_perfix="data") -> create input file "data.in" and output file "data.out"
IO(file_perfix="data",data_id=1) -> create input file "data1.in" and output file "data1.out"
IO(file_perfix="data",input_suffix=".input") -> create input file "data.input" and output file "data.out"
IO(file_perfix="data",output_suffix=".output") -> create input file "data.in" and output file "data.output"
IO(file_perfix="data",data_id=2,input_suffix=".input") -> create input file "data2.input" and output file "data2.out"
IO(file_prefix="data") -> create input file "data.in" and output file "data.out"
IO(file_prefix="data",data_id=1) -> create input file "data1.in" and output file "data1.out"
IO(file_prefix="data",input_suffix=".input") -> create input file "data.input" and output file "data.out"
IO(file_prefix="data",output_suffix=".output") -> create input file "data.in" and output file "data.output"
IO(file_prefix="data",data_id=2,input_suffix=".input") -> create input file "data2.input" and output file "data2.out"
"""
if len(args) == 0:
if not "file_prefix" in kwargs:
Expand Down
4 changes: 2 additions & 2 deletions examples/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from cyaron import *

io = IO()
io.input_write("1\n")
io.input_write("1111\n")

Compare.program("echo 1", input=io, std_program="cat")
Compare.program("echo 1111", input=io, std_program="cat")
Compare.program("echo 2", input=io, std_program="cat")

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='cyaron',
version='0.2.1',
version='0.2.2',
keywords='olympic informatics luogu aqours cyaron lovelive sunshine online judge',
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',
license='LGPLv3',
Expand Down

0 comments on commit 954cd27

Please sign in to comment.