From 71bffa42436522afb49525669199a9e87d03a817 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko <36362599+Maks1mS@users.noreply.github.com> Date: Sat, 18 Sep 2021 21:08:06 +0300 Subject: [PATCH] add command list --- turinglab/output.py | 49 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/turinglab/output.py b/turinglab/output.py index 834fa9d..3ab0f78 100644 --- a/turinglab/output.py +++ b/turinglab/output.py @@ -24,13 +24,48 @@ def to_docx(filename, program, data): for x in program.keys(): rows = max(list(program[x].keys())[-1] + 1, rows) + 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 + + p = document.add_paragraph() + p.paragraph_format.space_after = Pt(0) + p.paragraph_format.space_before = Pt(0) + p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE + add_state(p, i) + + symbol, direction, state = action + + p.add_run(symbols[j] + ' → ') + add_state(p, state) + p.add_run(symbol + 'R' if direction == '>' else 'L') + + + p = document.add_paragraph() + p.paragraph_format.space_after = Pt(0) + p.paragraph_format.space_before = Pt(0) + p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE + table = document.add_table(rows + 1, cols + 1) table.style = 'Table Grid' table.autofit = True table.allow_autofit = True - for i in range(1, rows + 1): - p = table.rows[i].cells[0].paragraphs[0] + for i in range(0, rows): + p = table.rows[i + 1].cells[0].paragraphs[0] p.alignment = WD_ALIGN_PARAGRAPH.CENTER p.paragraph_format.space_after = Pt(0) p.paragraph_format.space_before = Pt(0) @@ -45,9 +80,12 @@ def to_docx(filename, program, data): p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE p.add_run(x) - for j, (value) in enumerate(program.values()): - for i, (state, action) in enumerate(value.items()): - p = table.rows[state + 1].cells[j + 1].paragraphs[0] + 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] p.alignment = WD_ALIGN_PARAGRAPH.CENTER p.paragraph_format.space_after = Pt(0) p.paragraph_format.space_before = Pt(0) @@ -58,6 +96,7 @@ def to_docx(filename, program, data): p.add_run(symbol) p.add_run('R' if direction == '>' else 'L') + p = document.add_paragraph() p.paragraph_format.space_after = Pt(0) p.paragraph_format.space_before = Pt(0)