Mercurial > ~astiob > upreckon > hgweb
annotate 2.00/problem.py @ 27:dc4be35d17e0
Bug fixes
Bug fix: compat.py now works in Python 3 (module __builtin__ was renamed builtins).
Bug fix: random spaces in the indentation of testcases.py have been removed.
Bug fix: false dummy{in,out}name's are now again replaced with the values of testcase{in,out}name.
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Fri, 15 Oct 2010 22:17:31 +0000 |
parents | 5bbb68833868 |
children | 2b459f9743b4 |
rev | line source |
---|---|
21 | 1 #! /usr/bin/env python |
16 | 2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv> |
3 | |
21 | 4 from __future__ import division, with_statement |
5 | |
6 try: | |
7 from compat import * | |
8 import config, testcases | |
9 except ImportError: | |
10 import __main__ | |
11 __main__.import_error(sys.exc_info()[1]) | |
12 else: | |
22 | 13 from __main__ import clock, options |
21 | 14 |
22 | 15 import os, re, sys |
21 | 16 |
16 | 17 try: |
21 | 18 import signal |
19 except ImportError: | |
20 signalnames = () | |
21 else: | |
22 # Construct a cache of all signal names available on the current | |
23 # platform. Prefer names from the UNIX standards over other versions. | |
24 unixnames = frozenset(('HUP', 'INT', 'QUIT', 'ILL', 'ABRT', 'FPE', 'KILL', 'SEGV', 'PIPE', 'ALRM', 'TERM', 'USR1', 'USR2', 'CHLD', 'CONT', 'STOP', 'TSTP', 'TTIN', 'TTOU', 'BUS', 'POLL', 'PROF', 'SYS', 'TRAP', 'URG', 'VTALRM', 'XCPU', 'XFSZ')) | |
25 signalnames = {} | |
26 for name in dir(signal): | |
27 if re.match('SIG[A-Z]+$', name): | |
28 value = signal.__dict__[name] | |
22 | 29 if isinstance(value, int) and (value not in signalnames or name[3:] in unixnames): |
21 | 30 signalnames[value] = name |
31 del unixnames | |
16 | 32 |
21 | 33 __all__ = 'Problem', |
34 | |
26 | 35 def strerror(e): |
36 s = getattr(e, 'strerror') | |
37 if not s: s = str(e) | |
38 return ' (%s%s)' % (s[0].lower(), s[1:]) if s else '' | |
21 | 39 |
40 class Cache(object): | |
41 def __init__(self, mydict): | |
42 self.__dict__ = mydict | |
16 | 43 |
44 class Problem(object): | |
45 __slots__ = 'name', 'config', 'cache', 'testcases' | |
46 | |
47 def __init__(prob, name): | |
48 if not isinstance(name, basestring): | |
49 # This shouldn't happen, of course | |
21 | 50 raise TypeError('Problem() argument 1 must be string, not ' + type(name).__name__) |
16 | 51 prob.name = name |
21 | 52 prob.config = config.load_problem(name) |
53 if not getattr(prob.config, 'kind', None): prob.config.kind = 'batch' | |
54 prob.cache = Cache({'padoutput': 0, 'usegroups': False}) | |
55 prob.testcases = testcases.load_problem(prob) | |
56 | |
57 # TODO | |
58 def build(prob): | |
59 raise NotImplementedError | |
16 | 60 |
61 def test(prob): | |
23 | 62 case = None |
22 | 63 try: |
64 real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0 | |
65 for case in prob.testcases: | |
66 ntotal += 1 | |
67 max += case.points | |
68 if case.points: nvalued += 1 | |
69 granted = 0 | |
70 id = str(case.id) | |
71 if case.isdummy: | |
72 id = 'sample ' + id | |
73 say('%*s: ' % (prob.cache.padoutput, id), end='') | |
74 sys.stdout.flush() | |
75 try: | |
76 granted = case(lambda: (say('%7.3f%s s, ' % (case.time_stopped - case.time_started, case.time_limit_string), end=''), sys.stdout.flush())) | |
77 except testcases.CanceledByUser: | |
78 verdict = 'canceled by the user' | |
79 except testcases.TimeLimitExceeded: | |
80 verdict = 'time limit exceeded' | |
81 except testcases.WrongAnswer: | |
82 e = sys.exc_info()[1] | |
83 if e.comment: | |
84 verdict = 'wrong answer (%s)' % e.comment | |
85 else: | |
86 verdict = 'wrong answer' | |
87 except testcases.NonZeroExitCode: | |
88 e = sys.exc_info()[1] | |
89 if e.exitcode < 0: | |
90 if sys.platform == 'win32': | |
91 verdict = 'terminated with error 0x%X' % (e.exitcode + 0x100000000) | |
92 elif -e.exitcode in signalnames: | |
93 verdict = 'terminated by signal %d (%s)' % (-e.exitcode, signalnames[-e.exitcode]) | |
94 else: | |
95 verdict = 'terminated by signal %d' % -e.exitcode | |
21 | 96 else: |
22 | 97 verdict = 'non-zero return code %d' % e.exitcode |
98 except testcases.CannotStartTestee: | |
26 | 99 verdict = 'cannot launch the program to test%s' % strerror(sys.exc_info()[1].upstream) |
22 | 100 except testcases.CannotStartValidator: |
26 | 101 verdict = 'cannot launch the validator%s' % strerror(sys.exc_info()[1].upstream) |
22 | 102 except testcases.CannotReadOutputFile: |
26 | 103 verdict = 'cannot read the output file%s' % strerror(sys.exc_info()[1].upstream) |
22 | 104 except testcases.CannotReadInputFile: |
26 | 105 verdict = 'cannot read the input file%s' % strerror(sys.exc_info()[1].upstream) |
22 | 106 except testcases.CannotReadAnswerFile: |
26 | 107 verdict = 'cannot read the reference output file%s' % strerror(sys.exc_info()[1].upstream) |
22 | 108 except testcases.TestCaseNotPassed: |
26 | 109 verdict = 'unspecified reason [this may be a bug in test.py]%s' % strerror(sys.exc_info()[1]) |
22 | 110 #except Exception: |
26 | 111 # verdict = 'unknown error [this may be a bug in test.py]%s' % strerror(sys.exc_info()[1]) |
21 | 112 else: |
22 | 113 if hasattr(granted, '__iter__'): |
114 granted, comment = granted | |
115 if comment: | |
116 comment = ' (%s)' % comment | |
117 else: | |
118 comment = '' | |
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
119 if granted >= 1: |
22 | 120 ncorrect += 1 |
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
121 if case.points: ncorrectvalued += 1 |
22 | 122 verdict = 'OK' + comment |
123 elif not granted: | |
124 verdict = 'wrong answer' + comment | |
125 else: | |
126 verdict = 'partly correct' + comment | |
26 | 127 granted *= case.points |
22 | 128 say('%g/%g, %s' % (granted, case.points, verdict)) |
129 real += granted | |
130 weighted = real * prob.config.taskweight / max if max else 0 | |
131 if nvalued != ntotal: | |
132 say('Problem total: %d/%d tests (%d/%d valued); %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, ncorrectvalued, nvalued, real, max, weighted, prob.config.taskweight)) | |
21 | 133 else: |
22 | 134 say('Problem total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, real, max, weighted, prob.config.taskweight)) |
135 return weighted, prob.config.taskweight | |
136 finally: | |
23 | 137 if options.erase and (not prob.config.stdio or case and case.validator): |
22 | 138 for var in 'in', 'out': |
139 name = getattr(prob.config, var + 'name') | |
140 if name: | |
141 try: | |
142 os.remove(name) | |
143 except Exception: | |
144 pass | |
145 if case.validator and not callable(case.validator): | |
146 if prob.config.ansname: | |
147 try: | |
148 os.remove(prob.config.ansname) | |
149 except Exception: | |
150 pass |