adding support for dicts of lists

This commit is contained in:
evilchili 2023-12-08 14:28:07 -08:00
parent 845dbe1f7a
commit 3fbcb57aca
2 changed files with 36 additions and 9 deletions

View File

@ -134,7 +134,11 @@ class RollTable:
if not ds.data[option]:
values.append([option])
continue
choice = random.choice(ds.data[option])
if hasattr(ds.data[option], 'keys'):
rand_key = random.choice(list(ds.data[option].keys()))
choice = [rand_key, *ds.data[option][rand_key]]
else:
choice = random.choice(ds.data[option])
if hasattr(choice, 'keys'):
c = [option]
for (k, v) in choice.items():

View File

@ -92,26 +92,49 @@ B2:
B3:
"""
fixture_lists = """
fixture_lists_and_dicts = ["""
#
# one two three four
# foo bar baz quz
# category one two three four
# Category foo bar baz quz
#
metadata:
headers:
- category
- one
- two
- three
- four
foo:
- bar:
Category:
- foo:
- bar
- baz
- quz
""", """
#
# category one two three four
# Category foo bar baz quz
#
metadata:
headers:
- category
- one
- two
- three
- four
Category:
foo:
- bar
- baz
- quz
"""
bar:
- a
- b
- c
"""]
def test_lists():
t = tables.RollTable([fixture_lists], die=1)
def test_lists_and_dicts():
t = tables.RollTable(fixture_lists_and_dicts, die=1)
assert str(t)