fixing support for dicts of lists and lists of dicts

This commit is contained in:
evilchili 2023-12-08 14:52:40 -08:00
parent 3fbcb57aca
commit fd917bb59b
2 changed files with 18 additions and 9 deletions

View File

@ -135,15 +135,21 @@ class RollTable:
values.append([option]) values.append([option])
continue continue
if hasattr(ds.data[option], 'keys'): if hasattr(ds.data[option], 'keys'):
rand_key = random.choice(list(ds.data[option].keys())) k, v = random.choice(list(ds.data[option].items()))
choice = [rand_key, *ds.data[option][rand_key]] choice = [k] + v
else: else:
choice = random.choice(ds.data[option]) choice = random.choice(ds.data[option])
if hasattr(choice, 'keys'): if hasattr(choice, 'keys'):
c = [option] c = [option]
for (k, v) in choice.items(): for (k, v) in choice.items():
if type(v) is list:
c.extend([k, *v])
else:
c.extend([k, v]) c.extend([k, v])
values.append(c) values.append(c)
else:
if type(choice) is list:
values.append([option, *choice])
else: else:
values.append([option, choice]) values.append([option, choice])
return sorted(values) return sorted(values)

View File

@ -1,3 +1,5 @@
import pytest
from rolltable import tables from rolltable import tables
fixture_metadata = """ fixture_metadata = """
@ -104,7 +106,7 @@ metadata:
- two - two
- three - three
- four - four
Category: list:
- foo: - foo:
- bar - bar
- baz - baz
@ -121,7 +123,7 @@ metadata:
- two - two
- three - three
- four - four
Category: dict:
foo: foo:
- bar - bar
- baz - baz
@ -133,9 +135,10 @@ Category:
"""] """]
def test_lists_and_dicts(): @pytest.mark.parametrize('fixture', fixture_lists_and_dicts)
t = tables.RollTable(fixture_lists_and_dicts, die=1) def test_lists_and_dicts(fixture):
assert str(t) t = tables.RollTable([fixture], die=1)
assert(str(t))
def test_combined_tables(): def test_combined_tables():