Package: pyspj
pyspjA light-weighted special support utils.InstallationYou can simply install it withpipcommand line from the official PyPI site.pip install pyspjFor more information about installation, you can refer to theinstallation guide.Quick StartUse with CLIYou can usepyspjCLI to judge the running result, the help information is as followsUsage: pyspj [OPTIONS]

Options:
  -v, --version                   Show package's version information.
  -i, --input TEXT                Input content of special judge.
  -o, --output TEXT               Output content of special judge
  -I, --input_file FILE           Input file of special judge (if -i is given,
                                  this will be ignored).

  -O, --output_file FILE          Output file of special judge (if -o is
                                  given, this will be ignored).

  -t, --type [free|simple|continuity]
                                  Type of the final result.  [default: free]
  -s, --spj TEXT                  Special judge script to be used.  [required]
  -p, --pretty                    Use pretty mode to print json result.
  -h, --help                      Show this message and exit.Here is a simple example, a spj file namedtest_spj.pyimportiodefspj_func(stdin:io.StringIO,stdout:io.StringIO):inputs=[int(item.strip())foriteminstdin.read().strip().split(' ')ifitem]_correct_sum=sum(inputs)outputs=stdout.read().strip().split(' ',maxsplit=2)iflen(outputs)>=1:_result=int(outputs[0])else:returnFalse,'No output found.'if_result==_correct_sum:returnTrue,'Correct result.','Oh yeah, well done ^_^.'else:returnFalse,'Result{correct}expected but{actual}found.'.format(correct=repr(_correct_sum),actual=repr(_result))__spj__=spj_funcYou can use CLI to judge the result# input: 1 2 3 4 5# output: 15pyspj-i'1 2 3 4 5'-o'15'-stest_spj:spj_funcIt means import special judge functionspj_funcfrom packagetest_spj, the output result should be{"correctness": true, "detail": "Oh yeah, well done ^_^.", "message": "Correct result."}Besides, a simpler command line can be used due to the definition of__spj__variablepyspj-i'1 2 3 4 5'-o'15'-stest_spjThis command is equivalent to the above command.Input & Output from FileWhen the scale of input or output is huge, you can load them from-Iand-Eoption.Here is an exmaple, a input file namedtest_input.txt1 2 3 4 5Output file namedtest_output.txt15You can use CLI to judge the result in the txt filespyspj-Itest_input.txt-Otest_output.txt-stest_spjThe output should be{"correctness": true, "detail": "Oh yeah, well done ^_^.", "message": "Correct result."}Actually,-Iand-ocan be used together, so are the-iand-Eoptions. There will be nothing different.Pretty Json OutputIf you need pretty print the result (especially when you are debugging), you can use-poption.pyspj-i'1 2 3 4 5'-o'15'-stest_spj-pThe output should be{"correctness":true,"detail":"Oh yeah, well done ^_^.","message":"Correct result."}Additional ArgumentsSometimes, you can use additional arguments to support more complex cases.Such as the special judge function belowimportiodefspj_func(stdin:io.StringIO,stdout:io.StringIO,fxxk=None):inputs=[int(item.strip())foriteminstdin.read().strip().split(' ')ifitem]_correct_sum=sum(inputs)ifnotfxxk:outputs=stdout.read().strip().split(' ',maxsplit=2)iflen(outputs)>=1:_result=int(outputs[0])else:returnFalse,'No output found.'if_result==_correct_sum:returnTrue,'Correct result.','Oh yeah, well done ^_^.'else:returnFalse,'Result{correct}expected but{actual}found.'.format(correct=repr(_correct_sum),actual=repr(_result))else:returnFalse,'Result error because{value}detected in fxxk.'.format(value=repr(fxxk))__spj__=spj_funcAn extrafxxkargument is used, so the following command line can be used to set the value of argumentfxxkto2(string)pyspj-i'1 2 3 4 5'-o'15'-stest_spj-p-Vfxxk=2The output should be{
    "correctness": false,
    "detail": "Result error because '2' detected in fxxk.",
    "message": "Result error because '2' detected in fxxk."
}ATTENTION:Addtional argument should has a default value, otherwise when the value is not provided by command line, it will raise error.The default type of addtional arguments are string, you need to convert it to the type you actually need in your special judge function.Use Special Judge in Pythonpyspjcan be used in native Python by importing packagefrompyspjimportexecute_spj_from_stringif__name__=='__main__':result=execute_spj_from_string('test_spj','1 2 3 4 5','15')print(result.to_json())The output should be{'correctness': True, 'message': 'Correct result.', 'detail': 'Oh yeah, well done ^_^.'}File input or file output are supported as well, like the following codefrompyspjimportexecute_spj_from_fileif__name__=='__main__':result=execute_spj_from_file('test_spj','test_input.txt','test_output.txt')print(result.to_json())The output should be the same as the abovementioned code.Addtional arguments are also supported like the CLI.importcodecsimportiofrompyspjimportexecute_spjif__name__=='__main__':withcodecs.open('test_input.txt')asstdin,\io.StringIO('15')asstdout:result=execute_spj('test_spj',stdin,stdout,arguments={'fxxk':2})print(result.to_json())The output should be{'correctness': False, 'message': 'Result error because 2 detected in fxxk.', 'detail': 'Result error because 2 detected in fxxk.'}Make My Special Judge RunnableYou can add entry for your special judge function by usingpyspj_entryfunction and call it in__main___importiofrompyspjimportpyspj_entrydefspj_func(stdin:io.StringIO,stdout:io.StringIO):inputs=[int(item.strip())foriteminstdin.read().strip().split(' ')ifitem]_correct_sum=sum(inputs)outputs=stdout.read().strip().split(' ',maxsplit=2)iflen(outputs)>=1:_result=int(outputs[0])else:returnFalse,'No output found.'if_result==_correct_sum:returnTrue,'Correct result.','Oh yeah, well done ^_^.'else:returnFalse,'Result{correct}expected but{actual}found.'.format(correct=repr(_correct_sum),actual=repr(_result))if__name__=='__main__':pyspj_entry('test_spj',spj_func,version='0.1.1')()Save the code above totest_spj.py, and then you can run it like pyspj cli.pythontest_spj.py-hThe output should beUsage: test_spj.py [OPTIONS]

  Test_spj - test a pair of given input and output.

Options:
  -v, --version           Show special judge's version information.
  -i, --input TEXT        Input content of special judge.
  -o, --output TEXT       Output content of special judge
  -I, --input_file FILE   Input file of special judge (if -i is given, this
                          will be ignored).
  -O, --output_file FILE  Output file of special judge (if -o is given, this
                          will be ignored).
  -V, --value TEXT        Attached values for special judge (do not named as
                          "stdin" or "stdout").
  -p, --pretty            Use pretty mode to print json result.
  -h, --help              Show this message and exit.And test the inputpythontest_spj.py-i'1 2 3 4 5'-o15{"correctness": true, "detail": "Oh yeah, well done ^_^.", "message": "Correct result."}Besides, if the target environment does not have python, you can build this script withpyinstallerpipinstallpyspj[build]pyinstaller-D-F-ntest_spj-ctest_spj.pyA standalone executable file will be created.Licensepyspjreleased under the Apache 2.0 license.