diff --git a/rolltable/tables.py b/rolltable/tables.py index 8ddc4b9..df5f191 100644 --- a/rolltable/tables.py +++ b/rolltable/tables.py @@ -86,7 +86,19 @@ class DataSource: self.get_entries(option, rand=True) for option in self.random_frequencies(count) ] - def get_entries(self, option, rand: bool = True) -> list: + def as_dict(self) -> dict: + data = dict() + for name in self.data.keys(): + entries = self.get_entries(name, rand=False) + headers = self.headers + for i in range(0, len(self.headers) - len(entries[0])): + headers.append(f"{i:3f}", i) + items = {(k, v) for k, v in zip(self.headers, self.get_entries(name))} + print(items) + data[name] = dict(items) + return data + + def get_entries(self, option, rand: bool = False) -> list: """ For a random item or each item in the specified option in the data source, return a flattened list of the option, the select item, and the item's value (if any). diff --git a/tests/test_tables.py b/tests/test_tables.py index b094014..3b745ea 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -202,3 +202,8 @@ def test_text(): assert repr(tables.RollTable([fixture_one_choice])) assert repr(tables.RollTable([fixture_metadata + fixture_source])) assert repr(tables.RollTable([fixture_source])) + + +def test_as_dict(): + ds = tables.RollTable([fixture_metadata + fixture_source]).datasources[0].as_dict() + assert ds['Option 1']['Header 1'] == 'Option 1'