upgrade to python3;add some validator examples

This commit is contained in:
2025-10-11 12:33:24 +08:00
parent 65632f0e60
commit 487c041148
25 changed files with 1492 additions and 317 deletions

View File

@@ -16,21 +16,21 @@ class Problem:
self.id = row['id']
self.timemodified = row['timemodified']
self.vcode = row['validator_code']
if not isinstance(self.vcode, (str, unicode)):
if not isinstance(self.vcode, str):
self.vcode = self.vcode.__str__()
self.vtype = row['validator_type']
self.vlang = row['validator_lang']
self.gcode = row['generator_code']
if not isinstance(self.gcode, (str, unicode)):
if not isinstance(self.gcode, str):
self.gcode = self.vcode.__str__()
self.gtype = row['generator_type']
self.standard_code = row['standard_code']
if row.has_key('input_filename') and row['input_filename']:
if 'input_filename' in row and row['input_filename']:
self.input_filename = row['input_filename']
else:
self.input_filename = None
if row.has_key('output_filename') and row['output_filename']:
if 'output_filename' in row and row['output_filename']:
self.output_filename = row['output_filename']
else:
self.output_filename = None
@@ -60,7 +60,7 @@ class Problem:
return self.output_filename
def get_presetcodes(self, lang):
if not self.presetcodes.has_key(lang):
if not self.presetcodes or lang not in self.presetcodes:
codes = self.datasource.get_presetcodes(self.id, lang)
for code in codes: code.problem = self
self.presetcodes[lang] = codes
@@ -119,13 +119,11 @@ class Submit:
newresults = []
for r in results:
# stdout
f = file(r[4], 'r')
r[4] = f.read(config.output_sendback_size_limit)
f.close()
with open(r[4], 'r') as f:
r[4] = f.read(config.output_sendback_size_limit)
# stderr
f = file(r[5], 'r')
r[5] = f.read(config.output_sendback_size_limit)
f.close()
with open(r[5], 'r') as f:
r[5] = f.read(config.output_sendback_size_limit)
# strip stdout and stderr send back to datasource
# preventing post data too big
@@ -163,8 +161,8 @@ class TestCase:
self.memlimit = row['memlimit']
if not self.memlimit:
self.memlimit = config.maxmem
if row.has_key('nproc') and row['nproc']:
self.nproc = string.atoi(row['nproc'])
if 'nproc' in row and row['nproc']:
self.nproc = int(row['nproc'])
else:
self.nproc = 0
@@ -192,15 +190,15 @@ class TestCase:
input = string.replace(row['input'], '\r\n', '\n')
output = string.replace(row['output'], '\r\n', '\n')
f = file(self.infile, 'w')
f.write(input)
if len(input) > 0 and input[-1] != '\n': f.write('\n')
f.close()
with open(self.infile, 'w') as f:
f.write(input)
if len(input) > 0 and input[-1] != '\n':
f.write('\n')
f = file(self.outfile, 'w')
f.write(output)
if len(output) > 0 and output[-1] != '\n': f.write('\n')
f.close()
with open(self.outfile, 'w') as f:
f.write(output)
if len(output) > 0 and output[-1] != '\n':
f.write('\n')
logger.debug('Finished')
else:
logger.debug('Skip input/output file creation')
@@ -255,13 +253,11 @@ class DataFile:
data = datasource.get_datafile_data(self.id)
data = bz2.decompress(data)
if self.type == 'text':
f = open(self.absolute_path, 'w')
f.write(string.replace(data, '\r\n', '\n'))
f.close()
with open(self.absolute_path, 'w') as f:
f.write(string.replace(data, '\r\n', '\n'))
else:
f = open(self.absolute_path, 'wb')
f.write(data)
f.close()
with open(self.absolute_path, 'wb') as f:
f.write(data)
DataFile.write_lock.release()