diff --git a/pyproject.toml b/pyproject.toml index 69d29e0..9bd50e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = 'dnd-rolltable' -version = '1.1' +version = '1.1.1' license = 'The Unlicense' authors = ['Greg Boyington '] description = 'Generate roll tables using weighted random distributions' diff --git a/rolltable/cli.py b/rolltable/cli.py index 33ffe75..4b32417 100644 --- a/rolltable/cli.py +++ b/rolltable/cli.py @@ -22,7 +22,10 @@ def create( help='The size of the die for which to create a table'), collapsed: bool = typer.Option( True, - help='If True, collapse multiple die values with the same option.') + help='If True, collapse multiple die values with the same option.'), + yaml: bool = typer.Option( + False, + help='Render output as yaml.') ): """ CLI for creating roll tables. @@ -30,6 +33,10 @@ def create( rt = tables.RollTable([Path(s).read_text() for s in sources], frequency=frequency, die=die) + if yaml: + print(rt.as_yaml) + return + rows = rt.rows if collapsed else rt.expanded_rows table = Table(*rows[0]) for row in rows[1:]: diff --git a/rolltable/tables.py b/rolltable/tables.py index 07b2f46..1a91341 100644 --- a/rolltable/tables.py +++ b/rolltable/tables.py @@ -113,7 +113,10 @@ class RollTable: freqs = random.choices(options, weights=weights, k=self.die) values = [] for option in freqs: - choice = random.choice(ds.data[option]) if ds.data[option] else '' + if not ds.data[option]: + values.append([option]) + continue + choice = random.choice(ds.data[option]) if hasattr(choice, 'keys'): c = [option] for (k, v) in choice.items(): @@ -169,11 +172,9 @@ class RollTable: @property def as_yaml(self) -> dict: - struct = [{'headers': self.rows[0]}] + struct = {'headers': self.rows[0]} for row in self.rows[1:]: - struct.append({ - row[0]: row[1:] - }) + struct[row[0]] = row[1:] return yaml.dump(struct) def _config(self):