From 25de2c8c3c56e600779be19b6f5f033e07397504 Mon Sep 17 00:00:00 2001 From: evilchili Date: Sat, 6 Aug 2022 13:32:34 -0700 Subject: [PATCH] adding yaml output --- rolltable/tables.py | 13 +++++++++++++ tests/test_tables.py | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/rolltable/tables.py b/rolltable/tables.py index d91fc17..b1bdb6b 100644 --- a/rolltable/tables.py +++ b/rolltable/tables.py @@ -163,6 +163,19 @@ class RollTable: self._rows.append(self._column_filter([f'd{face+1}'] + row)) return self._rows + @property + def as_markdown(self) -> str: + return '' + + @property + def as_yaml(self) -> dict: + struct = [{'headers': self.rows[0]}] + for row in self.rows[1:]: + struct.append({ + row[0]: row[1:] + }) + return yaml.dump(struct) + def _config(self): """ Parse data sources, generate headers, and create the column filters diff --git a/tests/test_tables.py b/tests/test_tables.py index 9723582..2cf8f70 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -122,3 +122,8 @@ def test_no_descriptions(): t = tables.RollTable([fixture_no_descriptions], die=1) assert 'd1' in str(t) assert 'option 1' in str(t) + + +def test_yaml(): + t = tables.RollTable([fixture_metadata + fixture_source]) + print(t.as_yaml)