Advertisement
GBH007

CodeForce Task Tester (2016) v2

Sep 29th, 2016 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import subprocess
  4. from urllib.request import urlopen
  5. import sys
  6. import os
  7. import re
  8.  
  9. def io_t_l(c_id=714,pr='b'):
  10.     data=urlopen('http://codeforces.com/contest/{0}/problem/{1}'.format(c_id,pr)).read().decode()
  11.     data=re.sub('<br\s*/>','\n',data)
  12.     io_data=re.findall('(?:<pre>((?s).*?)</pre>)',data)
  13.     data=[i for i in zip(io_data[::2],io_data[1::2])]
  14.     return data
  15.     return data
  16.  
  17. class Tester:
  18.     def __init__(self,fname,cid,pid,out=sys.stdout,flog=None):
  19.         self.out=out
  20.         self.fname=fname
  21.         self.cid=cid
  22.         self.pid=pid
  23.         self.flog=flog
  24.     def write(self,s):
  25.         if self.flog:
  26.             self.flog.write(s)
  27.         self.out.write(s)
  28.     def run(self):
  29.         tests=io_t_l(self.cid,self.pid)
  30.         for inx,(inp,out) in enumerate(tests):
  31.             fi=open('input','w')
  32.             fi.write(inp)
  33.             fi.close()
  34.             rc=subprocess.call('python3 {0}'.format(self.fname),shell=1,stdout=open('output','w'),stdin=open('input'))
  35.             fout=open('output').read()
  36.             d=self.cmp(out,fout)
  37.             self.write('test {0} answern {1} return code {2}\n'.format(inx,d,rc))
  38.             if not d:
  39.                 self.write('-'*8+'fout'+'-'*8+'\n')
  40.                 self.write(fout.strip())
  41.                 self.write('\n'+'-'*8+'out-'+'-'*8+'\n')
  42.                 self.write(out.strip())
  43.                 self.write('\n'+'-'*20+'\n')
  44.         os.remove('input')
  45.         os.remove('output')
  46.     def cmp(self,t1,t2):
  47.         t1=[i.strip() for i in t1.split('\n') if i.strip()]
  48.         t2=[i.strip() for i in t2.split('\n') if i.strip()]
  49.         if len(t1)!=len(t2):return False
  50.         else:
  51.             for s1,s2 in zip(t1,t2):
  52.                 if s1!=s2:return False
  53.         return True
  54.        
  55.        
  56.  
  57. def main():
  58.     cfg={
  59.         'file':None,
  60.         'cid':None,
  61.         'pid':None,
  62.         'kfn':'y',
  63.     }
  64.     for i in sys.argv:
  65.         if '=' in i:
  66.             k,v=i.split('=')[:2]
  67.             cfg[k]=v
  68.     if cfg['kfn']=='y':
  69.         f=cfg['file']
  70.         fname=os.path.split(f)[1]
  71.         tmpl1='\D*(\d+).*'
  72.         tmpl2='.*([a-zA-Z]).*\..*'
  73.         cfg['cid']=re.findall(tmpl1,fname)[0]
  74.         cfg['pid']=re.findall(tmpl2,fname)[0]
  75.     t=Tester(cfg['file'],cfg['cid'],cfg['pid'])
  76.     t.run()
  77.  
  78. if __name__=='__main__':
  79.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement