def make_executable_Java (): if not util.file_exists("solution.java"): raise Exception("solution.java does not exist") util.del_file("Main.java") util.system("javac solution.java") if not util.file_exists("Main.class"): raise Exception("error in Java compilation")
def load(cls): if not file_exists(_base_path): os.mkdir(_base_path) path=cls._config_path() if file_exists(path): return cls.from_file(path) s=cls() s.save() return s
def record(self, file): '''Record this script file as executed. Or you can pass a list of file names''' if type(file) is list: for f in file: if not file_exists(f): raise InvalidPathException('File ' + file + ' does not exist.') self._writer.record(f) else: if not file_exists(file): raise InvalidPathException('File ' + file + ' does not exist.') self._writer.record(file)
def test_file_exists(self): self.assertRaises(TypeError, util.file_exists, self.ione) self.assertRaises(TypeError, util.file_exists, self.fone) self.assertRaises(TypeError, util.file_exists, self.btrue) self.assertRaises(TypeError, util.file_exists, self.tsimple) self.assertRaises(TypeError, util.file_exists, self.lsimple) self.assertFalse( util.file_exists("temp") ) f = open("temp","w") f.write("") f.close() self.assertTrue( util.file_exists("temp") ) self.delete_file("temp")
def make_executable_C (): handler = util.read_yml("handler.yml") if handler["handler"] != "std": raise Exception("unknown handler") if not util.file_exists("solution.c"): raise Exception("solution.c does not exist") util.del_file("solution.exe") util.system("%s %s solution.c -o solution.exe" % (cc, ccflags)) if not util.file_exists("solution.exe"): raise Exception("error in C compilation")
def make_executable_GHC (): handler = util.read_yml("handler.yml") if handler["handler"] != "std": raise Exception("unknown handler") if not util.file_exists("solution.hs"): raise Exception("solution.hs does not exist") util.del_file("solution.exe") util.system("ghc solution.hs -o solution.exe") if not util.file_exists("solution.exe"): raise Exception("error in GHC compilation")
def make_executable (): """Compiles the solution in thw cwd.""" if not util.file_exists("handler.yml"): raise Exception("handler.yml does not exist") handler = util.read_yml("handler.yml") if handler.get('compilers', '') == 'PRO2': make_executable_PRO2() elif handler.get('compilers', '') == 'MakePRO2': make_executable_MakePRO2() elif handler.get('compilers', '') == 'RunHaskell': make_executable_Haskell() elif handler.get('compilers', '') == 'RunPython': make_executable_RunPython() elif handler.get('solution', '') == 'GHC': make_executable_GHC() elif handler.get('solution', '') == 'Python3': make_executable_Python3() elif handler.get('solution', '') == 'Java': make_executable_Java() elif handler.get('solution', '') == 'C': make_executable_C() elif handler.get('solution', '') == 'C++': make_executable_CPP() else: make_executable_CPP()
def __init__(self, filename="", json_fields=None): if json_fields is not None: self.__validate_json_contents(json_fields) elif util.file_exists(filename): self.__parse_config(filename) else: raise error.FileDoesNotExistException(filename)
def calc_vsm_perform(similarity_func=calc_inner_product): if similarity_func.__name__ not in [ calc_cosine.__name__, calc_inner_product.__name__, calc_jaccard.__name__ ]: print('错误的输入相似度计算函数...') return print('正在加载训练集的预处理文件...') if file_exists(preprocess_path): res_lst = read_json(preprocess_path) # 加载训练集初步处理后的文件 else: res_lst = read_json(train_path) # 加载训练集源文件 for question in res_lst: question['question'] = seg_line(question['question']) write_json(preprocess_path, res_lst) print('正在计算相似度...') res = {} for item in res_lst: q_words, pid = {}, item['pid'] for word in item['question']: q_words[word] = q_words.get(word, 0) + 1 query_dic = { word: idf.get(word, 0) * (1 + log(tf, 10)) for word, tf in q_words.items() } pred_pid = similarity_func(query_dic)[0][0] res[item['qid']] = int(pred_pid) == pid print('进度: %.2f%%' % (len(res) / len(res_lst) * 100)) return len(list(filter(lambda val: res[val], res))) / len(res)
def vsm_init(): # 从JSON文件中加载权重矩阵;若文件不存在,则重新初始化矩阵,并写入JSON文件, global weight, idf if file_exists(model_path): model = load(model_path) weight, idf = model['weight'], model['idf'] else: res_dic = load_seg_passages() for pid, passage in res_dic.items(): passage_words, weight[pid] = [ word for word_lst in passage for word in word_lst ], {} for word in passage_words: # 计算每一个篇章中每一个词项的tf,结果保存在weight中 if word not in weight[pid]: weight[pid][word] = 0 idf[word] = idf[ word] + 1 if word in idf else 1 # 计算每一个词项的df,保存在words中 weight[pid][word] += 1 for word, df in idf.items(): # 计算log(N/df) idf[word] = log(len(res_dic) / df, 10) for pid in weight: # 计算权重矩阵 for word, tf in weight[pid].items(): # 遍历每一个词项 weight[pid][word] = (1 + log(tf, 10)) * idf[word] dump({'weight': weight, 'idf': idf}, model_path, compress=3)
def get_children_subtrees(self): self.queued.append(self.root) children = 0 while children < len(self.queued): subgenre = self.queued[children] if not file_exists(RAW_DATA_PATH + subgenre + ".json"): wait = uniform(5.0, 10) print("Waiting", str(wait) + "s") sleep(wait) if subgenre not in self.parsed: subtree = WikiSubtree(endpoint=subgenre) subtree.generate_subtree() grand_children = list( set(subtree.get_subtree()["children"]) - set(self.queued)) if not grand_children: self.failed.add(subgenre) self.queued.extend(grand_children) # self.queued = list(set(self.queued) - self.failed) self.parsed.add(subgenre) children += 1 print("Current queue") pprint(self.queued[children:]) print("Processing child:", children, "of queue length:", len(self.queued)) print()
def _resolve_header(fname, ipaths): """_resolve_header resolves absolute file location""" for p in ipaths: hfile = util.file_exists(p, fname, 'defs') if hfile: return hfile return None
def tf_idf_init(x_train): if file_exists(tf_idf_path): return joblib.load(tf_idf_path) else: tf_idf_vec = TfidfVectorizer(token_pattern=r"(?u)\b\w+\b") tf_idf_vec.fit_transform(x_train) joblib.dump(tf_idf_vec, tf_idf_path) return tf_idf_vec
def make_executable_Haskell (): if not util.file_exists("solution.hs"): raise Exception("solution.hs does not exist") util.del_file("work") util.del_file("work.hi") util.del_file("work.o") util.copy_file("solution.hs", "work.hs") f = open("work.hs", "a") print >>f, """main = do print "OK" """ f.close() util.system("ghc -O3 work.hs") if not util.file_exists("work"): raise Exception("error in haskell compilation") util.del_file("work") util.del_file("work.hi") util.del_file("work.o")
def make_recursive_2 (): sys.stdout.flush() if util.file_exists("handler.yml"): print "------------------------------------------" print os.getcwd() print "------------------------------------------" if util.file_exists("solution.cc") or util.file_exists("solution.hs"): try: if 1: make_executable() make_corrects() make_prints() except Exception, e: print("\a") print(e) errors.append((e, os.getcwd()))
def diff(self, file): path = self._record_path(file) if not file_exists(path): return HistoryManager.NEW proc = subp.Popen(['diff', file, path], stdout=subp.PIPE) diff = proc.communicate()[0] if diff: return diff else: return HistoryManager.NO_DIFF
def diff(self, file): '''Returns a tuple (status, diff), where status NEW, NO_DIFF or the results of a 'diff' command if the file have changed.''' if not file_exists(file): raise InvalidPathException('File ' + file + ' does not exist.') diff = self._writer.diff(file) if diff == HistoryManager.NEW or diff == HistoryManager.NO_DIFF: return (diff, None) else: return (HistoryManager.DIFF, diff)
def make_executable_PRO2 (): util.del_file("solution.exe") util.del_dir('compilation') os.mkdir('compilation') if util.file_exists("solution.cc"): util.system('cp solution.cc compilation/program.cc') elif util.file_exists("solution.hh"): util.system('cp solution.hh compilation/program.hh') else: print "There is no solution.cc nor solution.hh" util.system('cp public/* compilation') util.system('cp private/* compilation') os.chdir('compilation') util.system("%s %s *.cc -o ../solution.exe" % (cxx, cxxflags)) os.chdir('..') util.del_dir('compilation') if not util.file_exists("solution.exe"): raise Exception("solution.exe not created") util.system("(cd public && tar cf ../public.tar *)") util.system("(cd private && tar cf ../private.tar *)")
def __parse_template_file(self, template_file): """ Parses the template file if it exists, this is just a helper method for error checking. If the file exists, the contents of the file is stored into an instance variable :param template_file: the path to the template file of interest """ if util.file_exists(template_file): self.template_contents = util.read_file_str(template_file) else: raise error.FileDoesNotExistException(template_file)
def load_train_dev(dev=0.1, update=False): # 生成训练集和验证集,并将其按照rank-svm数据格式要求写入到文件中 if file_exists(train_feature_path) and file_exists( dev_feature_path) and not update: return else: seg_passages, res_lst, feature_lst = load_seg_passages(), read_json( train_path), [] for item in res_lst: # 遍历train.json文件中的每一行query信息 qid, pid, q_words, ans_words_lst, features = item['qid'], item['pid'], seg_line(item['question']), \ [seg_line(line) for line in item['answer_sentence']], [] tf_idf_vec = TfidfVectorizer(token_pattern=r"(?u)\b\w+\b") tf_idf_vec.fit_transform(' '.join(word_lst) for word_lst in seg_passages[str(pid)]) for word_lst in seg_passages[str(pid)]: value = 3 if word_lst in ans_words_lst else 0 # 排序用的值 todo feature = ' '.join(get_features(q_words, word_lst, tf_idf_vec)) features.append('%d qid:%d %s' % (value, qid, feature)) feature_lst.append(features) feature_lst.sort( key=lambda lst: int(lst[0].split()[1].split(':')[1])) # 按照qid排序 dev_num = int(dev * len(feature_lst)) train_features, test_features = feature_lst[:-dev_num], feature_lst[ -dev_num:] # 导出训练集和测试集 with open(train_feature_path, 'w', encoding='utf-8') as f1, open(dev_feature_path, 'w', encoding='utf-8') as f2: f1.write('\n'.join([ feature for feature_lst in train_features for feature in feature_lst ])) f2.write('\n'.join([ feature for feature_lst in test_features for feature in feature_lst ])) return train_features, test_features
def verify_program (program): """Verify that program compiles and gets AC for each test.""" # This implementation is not yet very functional, but works well in basic cases # compile program = os.path.splitext(program)[0] if not util.file_exists(program+".cc"): raise Exception("%s.cc does not exist" % program) if not util.file_exists("handler.yml"): raise Exception("handler.yml does not exist") handler = util.read_yml("handler.yml") if handler["handler"] != "std": raise Exception("unknown handler") util.del_file(program+".exe") if util.file_exists("main.cc"): raise Exception("not implemented yet") if handler["source_modifier"] == "structs": util.system("cat solution.cc main.cc > temporal.cc ; %s %s temporal.cc -o solution.exe ; rm temporal.cc" % (cxx, cxxflags)) else: util.system("%s %s solution.cc main.cc -o solution.exe" % (cxx, cxxflags)) else: if util.file_exists("solution.fallback"): raise Exception("not implemented yet") util.system("%s %s solution.cc -o solution.exe" % (cxx, cxxflags_fallback)) else: util.system("%s %s %s.cc -o %s.exe" % (cxx, cxxflags, program, program)) if not util.file_exists(program+".exe"): raise Exception(program+".exe not created") # execute on tests tests = sorted(glob.glob("*.inp")) for test in tests: test = os.path.splitext(test)[0] os.system("./%s.exe < %s.inp > %s.out" % (program, test, test)) r = subprocess.call(["cmp", test+".out", test+".cor"]) if r: msg = "WA" else: msg = "OK" print "%s:\t\t%s" % (test, msg)
def make_executable_MakePRO2 (): if not util.file_exists("solution"): raise Exception("There is no solution directory") if not util.file_exists("public"): raise Exception("There is no public directory") if not util.file_exists("private"): raise Exception("There is no private directory") util.del_file("solution.exe") util.del_dir('compilation') os.mkdir('compilation') util.system('cp solution/* public/* private/* compilation') os.chdir('compilation') util.system("make") util.system('cp program.exe ../solution.exe') os.chdir('..') util.del_dir('compilation') if not util.file_exists("solution.exe"): raise Exception("solution.exe not created") util.system("(cd public && tar cf ../public.tar *)") util.system("(cd private && tar cf ../private.tar *)") util.system("(cd solution && tar cf ../solution.tar *)")
def load_model(update=False): if file_exists(model_path) and not update: return else: print('*' * 100 + '\n正在构造训练集和开发集特征文件...') load_train_dev(update=update) print('构造训练集和开发集特征文件完成...\n' + '*' * 100 + '\n开始训练svm-rank模型并对验证集进行预测...') exe_rank_svm() print('预测完成\n' + '*' * 100) right_predict, num = evaluate() print('验证集正确答案数目:{};总数:{};准确率:{}%'.format(right_predict, num, (right_predict / num) * 100))
def __init__(self, endpoint): self.endpoint = endpoint self.file_path = RAW_DATA_PATH + self.endpoint + ".json" self.raw_subtree = {key: [] for key in RAW_CATEGORIES} self.exists = False if not file_exists(self.file_path): self.html = send_request(BASE_URL + self.endpoint, HEADERS) self.subtree = {} else: print(self.endpoint, "exists") self.exists = True self.subtree = read_json(self.file_path)
def make_executable_CPP (): handler = util.read_yml("handler.yml") if handler["handler"] != "std": raise Exception("unknown handler") if not util.file_exists("solution.cc"): raise Exception("solution.cc does not exist") util.del_file("solution.exe") if util.file_exists("main.cc"): if handler["source_modifier"] == "structs": util.system("cat solution.cc main.cc > temporal.cc ; %s %s temporal.cc -o solution.exe ; rm temporal.cc" % (cxx, cxxflags)) else: util.system("%s %s solution.cc main.cc -o solution.exe" % (cxx, cxxflags)) else: if util.file_exists("solution.fallback"): util.system("%s %s solution.cc -o solution.exe" % (cxx, cxxflags_fallback)) else: util.system("%s %s solution.cc -o solution.exe" % (cxx, cxxflags)) if not util.file_exists("solution.exe"): raise Exception("error in C++ compilation")
def handle(event: dict, context: dict) -> dict: print(f"Received event: {json.dumps(event)}") bucket_name = getenv("BUCKET") file_name = event["body"] if "body" in event else "-missing-" found = "" if file_exists(bucket_name, file_name) else "NOT " return { "statusCode": 200, "body": json.dumps(f"File {file_name} has {found}been found in {bucket_name}!") }
def lr_init(x_train, y_train): # solver选用默认的lbfgs, multi_class选用多分类问题中的multinomial if file_exists(lr_model_path): return joblib.load(lr_model_path) else: print('正在通过网格搜索获取最佳模型参数...') lr = LogisticRegression(max_iter=400, n_jobs=-1) param_grid = [{ 'C': [1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000] }] grid_search = GridSearchCV(lr, param_grid, cv=3, n_jobs=-1).fit(x_train, y_train) joblib.dump(grid_search.best_estimator_, lr_model_path, compress=3) # 导出模型 print('合理的超参数为', grid_search.best_params_) return grid_search.best_estimator_
def __validate_json_contents(self, contents): """ Validates the contents of the json file passed in as config. Ensures that there is a questions layout that is passed in. :param contents: the json contents to be validated """ if FIELDS in contents: self._fields = contents[FIELDS] else: self._fields = None if QUESTIONS in contents or util.file_exists(contents[QUESTIONS]): self._question_layout_file = contents[QUESTIONS] else: raise error.MissingQuestionLayoutFile(contents[QUESTIONS])
def diff_all(self, files): ''' Returns a dictionary with lists. {'NEW': ['file1.txt'], 'DIFF': [('file2.txt', 'the diff results')], 'NO_DIFF': ['file3.txt', 'file4.txt']}''' result = {HistoryManager.NEW: [], HistoryManager.DIFF: [], HistoryManager.NO_DIFF: []} for file in files: if not file_exists(file): raise InvalidPathException('File ' + file + ' does not exist.') diff = self._writer.diff(file) if diff == HistoryManager.NEW: result[HistoryManager.NEW].append(file) elif diff == HistoryManager.NO_DIFF: result[HistoryManager.NO_DIFF].append(file) else: result[HistoryManager.DIFF].append((file, diff)) return result
def load_session(cls, session_expire=20): file_name = Session.fn_session # If the file exists and was modified less than <session_expire> minutes ago if util.file_exists(file_name) and util.get_modified_ago( file_name).in_minutes() <= session_expire: print("Attempting to load session...") with open(file_name, 'rb') as pf: session = pickle.load(pf) session.logged_in_from_cache = True else: print("Attempting to create session...") session = Session() print("Attempting to login...") session() # execute __call__() function to login session.logged_in_from_cache = False return session
def main(argv): '''Where magick happens.''' args = parse_args(argv[1:]) setup_logging(args.logfile, args.log_level) log('\n### SQL_USER.PY: dbname=' + args.database + ' user='******' sqlfile=' + args.sql_file + ' host=' + args.host + ' ###') #log('### Arguments: ' + ' '.join(argv[1:])) # make sure main file exists if not util.file_exists(args.sql_file): log('ERROR: The file ' + args.sql_file + ' does not exist. Exiting.', logging.ERROR) sys.exit(EXIT_FAIL) # make sure all subscripts exist try: subs = util.find_subscripts(args.sql_file, args.dbms) except IOError, e: log('ERROR: One of the subscripts does not exist. Opening it threw and exception: ', logging.ERROR) log(e, logging.ERROR) sys.exit(EXIT_FAIL)
def make_corrects_RunHaskell (): for f in glob.glob("*.cor"): util.del_file(f) inps = sorted(glob.glob("*.inp")) for inp in inps: tst = os.path.splitext(inp)[0] util.copy_file("solution.hs", "work.hs") if util.file_exists("judge.hs"): os.system("cat judge.hs >> work.hs") f = open("work.hs", "a") print >>f, "main = do" for line in open(tst+".inp").readlines(): line = line.rstrip() if line.startswith("let "): print >>f, " %s" % line # elif line.startswith("deb "): # print >>f, ' hPutStrLn stderr "%s"' % line else: print >>f, " print (%s)" % line f.close() util.system("runhaskell work.hs >%s.cor" % (tst, ))
def load_test_data(update=False): if file_exists(test_feature_path) and not update: pass else: seg_passages, res_lst, feature_lst = load_seg_passages(), read_json( test_path), [] for item in res_lst: # 遍历文件中的每一行query信息 qid, pid, q_words, features = item['qid'], item['pid'], item[ 'question'], [] tf_idf_vec = TfidfVectorizer(token_pattern=r"(?u)\b\w+\b") tf_idf_vec.fit_transform(' '.join(word_lst) for word_lst in seg_passages[str(pid)]) for word_lst in seg_passages[str(pid)]: feature = ' '.join(get_features(q_words, word_lst, tf_idf_vec)) features.append('0 qid:%d %s' % (qid, feature)) feature_lst.append(features) feature_lst.sort( key=lambda lst: int(lst[0].split()[1].split(':')[1])) # 按照qid排序 with open(test_feature_path, 'w', encoding='utf-8') as f: f.write('\n'.join( [feature for features in feature_lst for feature in features]))
def test_run(args): errors = False for handler in logging.getLogger('console').handlers: handler.setLevel(logging.DEBUG) log('## Doing a test run. ##') if not util.file_exists(args.sql_file): errors = True log('ERROR: SQL script file ' + args.sql_file + ' does not exist.', logging.ERROR) log('Exiting...') sys.exit(EXIT_FAIL) else: log('Found script file ' + args.sql_file) # make sure all subscripts exist log('## Looking for subscripts') try: subs = util.find_subscripts(args.sql_file, args.dbms) except IOError, e: errors = True log('ERROR: One of the subscripts does not exist. Opening it threw and exception: ', logging.ERROR) log(e, logging.ERROR) sys.exit(EXIT_FAIL)
def make_corrects (): """Makes all correct files in the cwd.""" make_executable() handler = util.read_yml("handler.yml") if handler.get('compilers', '') == 'RunHaskell': make_corrects_RunHaskell() elif handler.get('compilers', '') == 'RunPython': make_corrects_RunPython() elif handler.get('solution', '') == 'Python3': make_corrects_Python3() elif handler.get('solution', '') == 'Java': make_corrects_Java() else: if not util.file_exists("solution.exe"): raise Exception("solution.exe does not exist") for f in glob.glob("*.cor"): util.del_file(f) inps = sorted(glob.glob("*.inp")) for inp in inps: tst = os.path.splitext(inp)[0] util.system("./solution.exe < %s.inp > %s.cor" % (tst, tst))
def __init__(self): if file_exists("temp/logfile.txt"): shutil.copyfile("temp/logfile.txt", "temp/logfile_old.txt") self.terminal = sys.stdout self.log = open("temp/logfile.txt", "w")
def __init__(self, expense_file): self.expenses_file = expense_file self.expenses = [] if util.file_exists(self.expenses_file): self.expenses = util.read_from_json(self.expenses_file)
from util import file_exists, create_dirs class Logger(): def __init__(self): if file_exists("temp/logfile.txt"): shutil.copyfile("temp/logfile.txt", "temp/logfile_old.txt") self.terminal = sys.stdout self.log = open("temp/logfile.txt", "w") def write(self, message): self.terminal.write(message) if not "\r" in message: self.log.write(message) self.log.flush() if __name__ == '__main__': if file_exists("mem/data_temp"): print("Danger: corrupted data file!") else: #Create directories create_dirs("mem/backup/") create_dirs("mem/important/") create_dirs("temp/") #Logging sys.stdout = Logger() #Check Python version print("Using Python version " +\ str(sys.version_info.major) + "." +\ str(sys.version_info.minor) + "." +\ str(sys.version_info.micro) + " " +\ sys.version_info.releaselevel + " " +\ str(int(round(log(sys.maxint * 2 + 2, 2)))) + "bit")
f2 = f.replace('lent', 'slow') util.move_file(f, f2) for f in glob.glob('*rapid*'): f2 = f.replace('rapid', 'fast') util.move_file(f, f2) for f in glob.glob('*exemple*'): f2 = f.replace('exemple', 'sample') util.move_file(f, f2) for f in glob.glob('*buit*'): f2 = f.replace('buit', 'empty') util.move_file(f, f2) if util.file_exists('enunciat.tex'): util.move_file('enunciat.tex', 'problem.es.tex') if util.file_exists('*entrades*'): util.move_file('entrades', 'inputs') if util.file_exists('*entrada*'): util.move_file('entrada', 'input') if util.file_exists('puntets.txt'): util.move_file('puntets.txt', 'scores.yml') print " !!! AFEGEIX ELS sample-*/CANVIA ELS prova-* AL FITXER scores.yml !!!" util.del_file('a.out') for f in glob.glob('*.exe'): util.del_file(f)
def make_prints_3 (lang, ori): ori = os.path.realpath(ori) dat = util.current_time() usr = util.get_username() hst = util.get_hostname() src = "%s@%s:%s" % (usr, hst, ori) sample2 = "" sample1 = "" tsts = sorted(glob.glob("*sample*.inp")) i = 0 for j in tsts: i += 1 jj = os.path.splitext(j)[0] if len(tsts)==1: num = "" else: num = str(i) sample2 += r"\SampleTwoColInputOutput{%s}{%s}" % (jj,num) sample1 += r"\SampleOneColInputOutput{%s}{%s}" % (jj,num) scores = "" if util.file_exists("scores.yml"): scores = "scores.yml: \\verbatimtabinput{scores.yml}" t = r""" \documentclass[11pt]{article} \usepackage{vanilla} \usepackage{vanilla.%s} \lstMakeShortInline@ \begin{document} \newcommand{\SampleTwoCol}{%s} \newcommand{\SampleOneCol}{%s} \DoProblem{%s} \subsection*{Metadata} \begin{verbatim} language: %s source: %s generation-time: %s\end{verbatim} problem.%s.yml: \verbatimtabinput{problem.%s.yml} handler.yml: \verbatimtabinput{handler.yml} %s \end{document} """ % (lang, sample2, sample1, lang, lang, src, dat, lang, lang, scores) util.write_file("main.tex", t) print "latex" r = os.system("latex -interaction scrollmode main > main.err") #r = os.system("latex main") if r != 0: os.system('cat main.err') raise Exception("latex error") print "dvips" r = os.system("dvips main -o 1> /dev/null 2>/dev/null") if r != 0: raise Exception("dvips error") print "ps2pdf" r = os.system("ps2pdf main.ps main.pdf 1> /dev/null 2>/dev/null") if r != 0: raise Exception("ps2pdf error") os.system("mv main.ps %s/problem.%s.ps " % (ori, lang)) os.system("mv main.pdf %s/problem.%s.pdf" % (ori, lang))
def resolve_hdr_file(fname): for p in self.bundle.inc_paths: hfile = util.file_exists(p, fname, 'defs') if hfile: return hfile return None
class Logger(): def __init__(self): if file_exists("temp/logfile.txt"): shutil.copyfile("temp/logfile.txt", "temp/logfile_old.txt") self.terminal = sys.stdout self.log = open("temp/logfile.txt", "w") def write(self, message): self.terminal.write(message) if not "\r" in message: self.log.write(message) self.log.flush() if __name__ == '__main__': if file_exists("mem/data_temp"): print("Danger: corrupted data file!") else: #Create directories create_dirs("mem/backup/") create_dirs("mem/important/") create_dirs("temp/") #Logging sys.stdout = Logger() #Check Python version print("Using Python version " +\ str(sys.version_info.major) + "." +\ str(sys.version_info.minor) + "." +\ str(sys.version_info.micro) + " " +\ sys.version_info.releaselevel + " " +\ str(int(round(log(sys.maxint * 2 + 2, 2)))) + "bit")
import util def first(input_path: str): with open(input_path) as file: lines = file.read().splitlines() numbers = [int(i) for i in lines] for idx, i in enumerate(numbers[:-1]): for _, j in enumerate(numbers[idx + 1:]): if i + j == 2020: print(f'The answer is: {i * j}') def second(input_path: str): with open(input_path) as file: lines = file.read().splitlines() numbers = [int(i) for i in lines] for idx, i in enumerate(numbers[:-2]): for idy, j in enumerate(numbers[idx + 1:-1]): for idz, k in enumerate(numbers[idy + 1:]): if i + j + k == 2020: print(f'The answer is: {i * j * k}. {idx} {idy} {idz}') if __name__ == '__main__': path = util.get_input_path(__file__) if util.file_exists(path): first(path) if util.file_exists(path): second(path)
def make_executable_Python3 (): if not util.file_exists("solution.py"): raise Exception("solution.py does not exist")
def record(self, file): dest = self._record_path(file) path = os.path.split(dest)[0] if not file_exists(path): os.makedirs(path) os.system('cp ' + file + ' ' + dest)