Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import subprocess
- from urllib.request import urlopen
- import tkinter
- import tkinter.scrolledtext
- import pickle
- import sys
- import os
- #~ print(sys.argv)
- try:
- _config=pickle.load(open('cfg.bin','rb'))
- except (FileNotFoundError,pickle.UnpicklingError):
- _config={
- 'contest_id':714,
- 'problem_id':'a',
- 'file':'/home/gbh007/CodeForces/main.py',
- }
- def io_t_l(c_id=714,pr='b'):
- data=urlopen('http://codeforces.com/contest/{0}/problem/{1}'.format(c_id,pr)).read().decode()
- data=data.split('<div class="sample-test">')[1]
- io_data=[]
- for i in data.split('<pre>'):
- if '</pre>' in i:
- io_data.append(i.split('</pre>')[0].replace('<br />','\n'))
- data=[]
- for i in range(len(io_data)//2):
- data.append((io_data[i*2],io_data[i*2+1]))
- return data
- class Tester:
- def __init__(self,fname,cid,pid,out=sys.stdout,flog=None):
- self.out=out
- self.fname=fname
- self.cid=cid
- self.pid=pid
- self.flog=flog
- def write(self,s):
- if self.flog:
- self.flog.write(s)
- self.out.write(s)
- def run(self):
- tests=io_t_l(self.cid,self.pid)
- for inx,(inp,out) in enumerate(tests):
- fi=open('input','w')
- fi.write(inp)
- fi.close()
- rc=subprocess.call('python3 {0}'.format(self.fname),shell=1,stdout=open('output','w'),stdin=open('input'))
- fout=open('output').read()
- d=self.cmp(out,fout)
- self.write('test {0} answern {1} return code {2}\n'.format(inx,d,rc))
- if not d:
- self.write('-'*8+'fout'+'-'*8+'\n')
- self.write(fout.strip())
- self.write('\n'+'-'*8+'out'+'-'*9+'\n')
- self.write(out.strip())
- self.write('\n'+'-'*20+'\n')
- def cmp(self,t1,t2):
- t1=[i.strip() for i in t1.split('\n') if i.strip()]
- t2=[i.strip() for i in t2.split('\n') if i.strip()]
- if len(t1)!=len(t2):return False
- else:
- for s1,s2 in zip(t1,t2):
- if s1!=s2:return False
- return True
- class TestResult(tkinter.Toplevel):
- def __init__(self):
- tkinter.Toplevel.__init__(self)
- self.title('test result')
- self.st=tkinter.scrolledtext.ScrolledText(self)
- self.st.pack(fill=tkinter.BOTH,expand=tkinter.YES)
- def write(self,s):
- self.st.insert(tkinter.END,s)
- def clear(self):
- self.st.delete(0,tkinter.END)
- class Root(tkinter.Tk):
- def __init__(self):
- tkinter.Tk.__init__(self)
- self.ci=tkinter.IntVar(value=_config['contest_id'])
- self.pi=tkinter.StringVar(value=_config['problem_id'])
- self.fl=tkinter.StringVar(value=_config['file'])
- tkinter.Label(self,text='contest id').pack(fill=tkinter.X)
- tkinter.Entry(self,textvariable=self.ci).pack(fill=tkinter.X)
- tkinter.Label(self,text='problem id').pack(fill=tkinter.X)
- tkinter.Entry(self,textvariable=self.pi).pack(fill=tkinter.X)
- tkinter.Label(self,text='file').pack(fill=tkinter.X)
- tkinter.Entry(self,textvariable=self.fl).pack(fill=tkinter.X)
- tkinter.Button(self,text='test',command=self.run).pack(fill=tkinter.X)
- def run(self):
- t=Tester(self.fl.get(),self.ci.get(),self.pi.get(),TestResult())
- t.run()
- def destructor(self):
- global _config
- _config['contest_id']=self.ci.get()
- _config['problem_id']=self.pi.get()
- _config['file']=self.fl.get()
- def main1():
- obj=subprocess.call('python3 a.py',shell=1,stdout=open('output','w'),stdin=open('input'))
- print(obj)
- #~ obj.write(open('input').read())
- #~ open('output','w').write(obj.read())
- def main_gui():
- root=Root()
- root.mainloop()
- root.destructor()
- def main_ts(cfg):
- if cfg['kfn']=='y':
- f=cfg['file']
- fname=os.path.split(f)[1]
- ci=fname.split('_')[0]
- pi=fname.split('_')[1].split('.')[0]
- t=Tester(f,ci,pi)
- t.run()
- if __name__=='__main__':
- cfg={
- 'file':None,
- 'gui':'y',
- 'kfn':'y',
- }
- for i in sys.argv:
- if '=' in i:
- k,v=i.split('=')[:2]
- cfg[k]=v
- if cfg['gui']=='y':
- main_gui()
- else:
- main_ts(cfg)
- pickle.dump(_config,open('cfg.bin','wb'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement