turinglab/turinglab/output.py

127 lines
3.5 KiB
Python
Raw Normal View History

2021-09-19 00:29:59 +03:00
from turinglab.image import get_image
2021-09-15 22:39:37 +03:00
from docx import Document
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
2021-09-19 00:29:59 +03:00
from docx.shared import Pt, Inches
2021-09-15 22:39:37 +03:00
from docx.enum.text import WD_LINE_SPACING
2021-09-18 19:45:25 +03:00
from docx.enum.text import WD_ALIGN_PARAGRAPH
2021-09-15 22:39:37 +03:00
2021-09-18 19:45:25 +03:00
def add_state(p, state):
p.add_run('q')
index_text = p.add_run(str('z' if state == -1 else state))
index_text.font.subscript = True
2021-09-15 22:39:37 +03:00
2021-09-19 09:20:51 +03:00
def create_paragraph(document):
p = document.add_paragraph()
style_paragraph(p)
return p
def style_paragraph(p):
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE
2021-09-18 19:45:25 +03:00
def to_docx(filename, program, data):
document = Document()
2021-09-15 22:39:37 +03:00
style = document.styles['Normal']
font = style.font
font.name = 'Times New Roman'
font.size = Pt(14)
2021-09-18 19:45:25 +03:00
rows = 0
cols = len(program.keys())
for x in program.keys():
2021-09-19 00:29:59 +03:00
if len(list(program[x].keys())) == 0:
continue
2021-09-18 19:45:25 +03:00
rows = max(list(program[x].keys())[-1] + 1, rows)
2021-09-18 21:08:06 +03:00
program_table = [None] * rows
for i in range(rows):
program_table[i] = [None] * cols
symbols = [None] * cols
for j, (symbol, value) in enumerate(program.items()):
symbols[j] = symbol
for i, (state, action) in enumerate(value.items()):
program_table[state][j] = action
for i in range(rows):
for j in range(cols):
action = program_table[i][j]
if action is None: continue
2021-09-19 09:20:51 +03:00
p = create_paragraph(document)
2021-09-18 21:08:06 +03:00
add_state(p, i)
symbol, direction, state = action
p.add_run(symbols[j] + '')
add_state(p, state)
2021-09-19 00:29:59 +03:00
p.add_run(symbol + ('R' if direction == '>' else 'L'))
2021-09-18 21:08:06 +03:00
2021-09-19 09:20:51 +03:00
p = create_paragraph(document)
2021-09-18 21:08:06 +03:00
2021-09-18 19:45:25 +03:00
table = document.add_table(rows + 1, cols + 1)
table.style = 'Table Grid'
table.autofit = True
table.allow_autofit = True
2021-09-18 21:08:06 +03:00
for i in range(0, rows):
p = table.rows[i + 1].cells[0].paragraphs[0]
2021-09-18 19:45:25 +03:00
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
2021-09-19 09:20:51 +03:00
style_paragraph(p)
2021-09-18 19:45:25 +03:00
add_state(p, i)
for i, x in enumerate(program.keys()):
p = table.rows[0].cells[i + 1].paragraphs[0]
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
2021-09-19 09:20:51 +03:00
style_paragraph(p)
2021-09-18 19:45:25 +03:00
p.add_run(x)
2021-09-18 21:08:06 +03:00
for i in range(rows):
for j in range(cols):
action = program_table[i][j]
if action is None: continue
p = table.rows[i + 1].cells[j + 1].paragraphs[0]
2021-09-18 19:45:25 +03:00
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
2021-09-19 09:20:51 +03:00
style_paragraph(p)
2021-09-18 19:45:25 +03:00
symbol, direction, state = action
add_state(p, state)
p.add_run(symbol)
p.add_run('R' if direction == '>' else 'L')
2021-09-19 09:20:51 +03:00
p = create_paragraph(document)
2021-09-18 21:08:06 +03:00
2021-09-19 09:20:51 +03:00
for i, test in enumerate(data):
p = create_paragraph(document)
p.add_run(f'Test {i + 1}:')
2021-09-15 22:39:37 +03:00
2021-09-19 09:20:51 +03:00
for j, data in enumerate(test):
p = create_paragraph(document)
p.add_run('K')
2021-09-15 22:39:37 +03:00
2021-09-19 09:20:51 +03:00
index_text = p.add_run(str(j))
index_text.font.subscript = True
2021-09-15 22:39:37 +03:00
2021-09-19 09:20:51 +03:00
p.add_run(': ')
2021-09-15 22:39:37 +03:00
2021-09-19 09:20:51 +03:00
head, state, tape = data
offset = list(tape.keys())[0]
head -= offset
tape_str = ''.join(tape.values())
2021-09-15 22:39:37 +03:00
2021-09-19 09:20:51 +03:00
p.add_run(tape_str[:head].lstrip('λ') + 'q')
2021-09-15 22:39:37 +03:00
2021-09-19 09:20:51 +03:00
index_text = p.add_run(str('z' if state == -1 else state))
index_text.font.subscript = True
2021-09-15 22:39:37 +03:00
2021-09-19 09:20:51 +03:00
p.add_run(tape_str[head] + tape_str[head + 1:].rstrip('λ'))
2021-09-15 22:39:37 +03:00
document.save(filename)