-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
62 lines (48 loc) · 1.98 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from typing import List
import exact_real_program
import os
import numpy as np
import subprocess
from tabulate import tabulate
from datetime import timedelta
from timeit import default_timer as timer
from multiprocessing import Process, Pipe
def time_wrap(f, args):
"""Apply the arguments to the function and return the time and result. """
start_time = timer()
res = f(*args)
end_time = timer()
return timedelta(seconds=end_time - start_time), res
def multiprocess(f):
"""A decorator for executing f in its own_process. """
def multiprocess_wrap(conn, *args, **kwargs):
"""Wrap f so that the result of the computation is sent through the connection. """
conn.send(f(*args, **kwargs))
conn.close()
def wrapper(*args, **kwargs):
"""Make the multiprocessed request, passing through the child process."""
parent_conn, child_conn = Pipe()
p = Process(target=multiprocess_wrap, args=(child_conn, *args), kwargs=kwargs)
p.start()
result = parent_conn.recv()
p.join()
return result
return wrapper
def cast_input(to_cast):
return exact_real_program.ExactConstant(to_cast) if isinstance(to_cast, (int, float)) else to_cast
def create_pdf_from_table(filename: str, table_info: np.array, headers: List):
table = tabulate(table_info, tablefmt="latex", headers=headers)
# print(table)
latex = "\\documentclass{article}\n\\pdfpageheight=11in\n\\pdfpagewidth=8.5in\n\\begin{document}\n" + table + "\\end{document}"
with open(filename + ".tex", 'w') as f:
f.write(latex)
cmd = ['pdflatex', '-interaction', 'nonstopmode', filename + ".tex"]
proc = subprocess.Popen(cmd)
proc.communicate()
retcode = proc.returncode
if not retcode == 0:
os.unlink(filename + ".pdf")
raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd)))
os.unlink(filename + ".tex")
os.unlink(filename + ".log")
os.unlink(filename + ".aux")