refine yaml output format

This commit is contained in:
evilchili 2022-08-06 21:19:59 -07:00
parent b43e972d1b
commit 9e7101945c
3 changed files with 10 additions and 9 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = 'dnd-rolltable' name = 'dnd-rolltable'
version = '1.1.1' version = '1.1.3'
license = 'The Unlicense' license = 'The Unlicense'
authors = ['Greg Boyington <evilchili@gmail.com>'] authors = ['Greg Boyington <evilchili@gmail.com>']
description = 'Generate roll tables using weighted random distributions' description = 'Generate roll tables using weighted random distributions'

View File

@ -34,7 +34,7 @@ def create(
rt = tables.RollTable([Path(s).read_text() for s in sources], frequency=frequency, die=die) rt = tables.RollTable([Path(s).read_text() for s in sources], frequency=frequency, die=die)
if yaml: if yaml:
print(rt.as_yaml) print(rt.as_yaml())
return return
rows = rt.rows if collapsed else rt.expanded_rows rows = rt.rows if collapsed else rt.expanded_rows

View File

@ -93,6 +93,14 @@ class RollTable:
self._generated_values = None self._generated_values = None
self._config() self._config()
def as_yaml(self, expanded=False) -> dict:
struct = {}
for row in self.rows[1:]:
struct[row[0]] = {}
for idx, col in enumerate(row[1:]):
struct[row[0]][self.headers[idx]] = col
return yaml.dump(struct)
@property @property
def die(self) -> int: def die(self) -> int:
return self._die return self._die
@ -170,13 +178,6 @@ class RollTable:
def as_markdown(self) -> str: def as_markdown(self) -> str:
return '' return ''
@property
def as_yaml(self) -> dict:
struct = {'headers': self.rows[0]}
for row in self.rows[1:]:
struct[row[0]] = row[1:]
return yaml.dump(struct)
def _config(self): def _config(self):
""" """
Parse data sources, generate headers, and create the column filters Parse data sources, generate headers, and create the column filters