Generalize running scripts

This commit is contained in:
Oystein Kristoffer Tveit 2021-05-10 22:12:15 +02:00
parent 9358971583
commit 0cfd95ad59
5 changed files with 78 additions and 10 deletions

View File

@ -4,7 +4,12 @@
default: python
pdflatex main.tex
SRC_DIR := $(shell find graphics/src -type f -printf "%f\n" -name *.txt | cut -d '.' -f1)
python:
python python/Hasse.py graphics/src/example1.txt graphics/example1.tex
python python/FSA.py graphics/src/example2.txt graphics/example2.tex
python python/Truthtable.py graphics/src/example3.txt graphics/example3.tex
@for f in $(SRC_DIR); \
do \
echo -e "\033[33mCOMPILING $$f.tex\033[0m"; \
python ../exam_template/python/run.py graphics/src/$$f.txt graphics/$$f.tex; \
echo ""; \
done

View File

@ -62,6 +62,9 @@ def generate_latex(inputLines):
return '\n'.join(output)
def processFileContent(raw, template):
content = generate_latex(raw)
return template.replace('%CONTENT', content)
if __name__ == '__main__':
filename = argv[1]

View File

@ -65,6 +65,11 @@ def latex_hasse(hasse):
return '\n'.join(output)
def processFileContent(raw, template):
rels = getRels(raw)
content = latex_hasse(hasse_diagram(rels))
return template.replace("%CONTENT", content)
if __name__ == '__main__':
filename = argv[1]

View File

@ -1,5 +1,10 @@
from sys import argv
from pathlib import Path
try:
from tabulate import tabulate
except:
print('Couldn\'t find tabulate. Do you have it installed?')
def parseExpressionList(inputData):
return [e.strip() for e in inputData.split(',')]
@ -77,12 +82,27 @@ def latexify(exps, truthtable):
""".format(
'|'.join('e' if e else 'c' for e,_ in latex_expressions),
' & '.join(f'${exp}$' for _,exp in latex_expressions),
'\n'.join(' ' + ' & '.join('\\T' if b else '\\F' for b in line) for line in truthtable)
'\n'.join(' ' + ' & '.join('\\T' if b else '\\F' for b in line) + ' \\\\' for line in truthtable)
)
def printTruthtable(exps, truthtable):
stringTable = [ ['\033[32mT\033[0m ' if b else '\033[31mF\033[0m' for b in line] for line in truthtable]
print(tabulate(stringTable, headers=exps))
try:
print(tabulate(stringTable, headers=exps))
except:
pass
def processFileContent(raw, template):
exps = parseExpressionList(raw)
truthtable = generateTruthTable(exps)
printTruthtable(exps, generateTruthTable(exps))
content = latexify(exps, truthtable)
return template.replace('%CONTENT', content)
if __name__ == '__main__':
filename = argv[1]
@ -91,11 +111,11 @@ if __name__ == '__main__':
exps = parseExpressionList(file.read())
truthtable = generateTruthTable(exps)
try:
from tabulate import tabulate
printTruthtable(exps, generateTruthTable(exps))
except:
print('Couldn\'t find tabulate. Do you have it installed?')
# try:
from tabulate import tabulate
printTruthtable(exps, generateTruthTable(exps))
# except e:
# print(e)
content = latexify(exps, truthtable)

View File

@ -0,0 +1,35 @@
from sys import argv
from pathlib import Path
import FSA
import Graph
import Hasse
import Truthtable
def fetchContentType(content):
new_content = content.split('\n')
contentType = new_content.pop(0)[2:]
return (contentType, '\n'.join(new_content))
def processContent(content):
contentType, content = fetchContentType(content)
with open(str(Path(__file__).parent.absolute()) + f'/tex_templates/{contentType}.tex') as template:
if contentType == 'Hasse':
result = Hasse.processFileContent(content, template.read())
elif contentType == 'FSA':
result = FSA.processFileContent(content, template.read())
elif contentType == 'Truthtable':
result = Truthtable.processFileContent(content, template.read())
else:
print('DIDN\'T RECOGNIZE FILE TYPE')
exit(1)
return result
if __name__ == '__main__':
filename = argv[1]
with open(filename) as file:
content = processContent(file.read())
with open(argv[2], 'w') as destination_file:
destination_file.write(content)