2022-08-13 12:32:52 -07:00
|
|
|
from collections.abc import Iterable
|
2024-02-17 17:05:33 -08:00
|
|
|
from typing import List, Optional, Union
|
2023-12-22 13:36:45 -08:00
|
|
|
|
2024-01-06 12:21:30 -08:00
|
|
|
import rich.table
|
2024-02-17 17:05:33 -08:00
|
|
|
import yaml
|
|
|
|
from csv2md.table import Table
|
|
|
|
from random_sets.datasources import DataSource
|
2024-01-06 12:21:30 -08:00
|
|
|
|
2023-12-22 23:22:17 -08:00
|
|
|
|
2022-07-31 15:03:19 -07:00
|
|
|
class RollTable:
|
|
|
|
"""
|
|
|
|
Generate a roll table using weighted distributions of random options.
|
2022-07-30 09:33:03 -07:00
|
|
|
|
2022-07-31 15:03:19 -07:00
|
|
|
Instance Attributes:
|
2022-07-30 09:33:03 -07:00
|
|
|
|
2022-07-31 15:03:19 -07:00
|
|
|
sources - One or more yaml strings to parse as data sources
|
|
|
|
frequency - The frequency distribution to apply when populating the table
|
|
|
|
die - The size of the die for which to create a table (default: 20)
|
|
|
|
headers - An array of header strings
|
|
|
|
rows - An array of table headers and rows
|
|
|
|
expanded_rows - An array of table headers and rows, one per die roll value
|
2022-07-30 09:33:03 -07:00
|
|
|
|
2022-07-31 15:03:19 -07:00
|
|
|
Usage:
|
2022-07-30 09:33:03 -07:00
|
|
|
|
2022-07-31 15:03:19 -07:00
|
|
|
table = RollTable(['source.yaml'], die=4)
|
|
|
|
print(table)
|
|
|
|
>>> Roll Item
|
|
|
|
d1 Foo
|
|
|
|
d2-d4 Bar
|
2022-07-30 20:44:16 -07:00
|
|
|
"""
|
|
|
|
|
2024-02-17 17:05:33 -08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
sources: Union[List[str], List[DataSource]],
|
|
|
|
frequency: str = "default",
|
|
|
|
die: Optional[int] = 20,
|
|
|
|
hide_rolls: bool = False,
|
|
|
|
) -> None:
|
2022-07-31 15:03:19 -07:00
|
|
|
self._sources = sources
|
2024-02-17 17:05:33 -08:00
|
|
|
self.frequency = frequency
|
2024-01-17 21:15:39 -08:00
|
|
|
self.die = die
|
|
|
|
self.hide_rolls = hide_rolls
|
|
|
|
self.data = None
|
2022-07-30 20:44:16 -07:00
|
|
|
self._rows = None
|
|
|
|
self._headers = None
|
2022-07-31 15:03:19 -07:00
|
|
|
self._header_excludes = None
|
|
|
|
self._generated_values = None
|
|
|
|
self._config()
|
2022-07-30 20:44:16 -07:00
|
|
|
|
2023-04-12 11:26:17 -07:00
|
|
|
@property
|
|
|
|
def datasources(self) -> List:
|
|
|
|
return self._data
|
|
|
|
|
2022-07-30 20:44:16 -07:00
|
|
|
@property
|
2022-07-31 15:03:19 -07:00
|
|
|
def headers(self) -> List:
|
|
|
|
return self._headers
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _values(self) -> List:
|
2024-01-16 22:05:06 -08:00
|
|
|
"""
|
2024-02-17 17:05:33 -08:00
|
|
|
For each data source, select N random values, where N is the size of the die.
|
|
|
|
we then zip those random values so that each member of the generated list
|
|
|
|
contains one value from each data source. So if _data is:
|
2024-01-16 22:05:06 -08:00
|
|
|
|
2024-02-17 17:05:33 -08:00
|
|
|
[
|
|
|
|
['axe', 'shortsword', 'dagger'],
|
|
|
|
['fire', 'ice', 'poison'],
|
|
|
|
]
|
2024-01-16 22:05:06 -08:00
|
|
|
|
2024-02-17 17:05:33 -08:00
|
|
|
and the die is 2, the resulting generated values might be:
|
2024-01-16 22:05:06 -08:00
|
|
|
|
2024-02-17 17:05:33 -08:00
|
|
|
[
|
|
|
|
['axe', 'fire'],
|
|
|
|
['dagger', 'ice'],
|
|
|
|
]
|
2024-01-16 22:05:06 -08:00
|
|
|
"""
|
2022-07-31 15:03:19 -07:00
|
|
|
if not self._generated_values:
|
2024-02-17 17:05:33 -08:00
|
|
|
self._generated_values = list(zip(*[t.random_values(self.die) for t in self._data]))
|
2022-07-31 15:03:19 -07:00
|
|
|
return self._generated_values
|
2022-07-30 20:44:16 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def rows(self) -> List:
|
2022-07-31 15:03:19 -07:00
|
|
|
def formatted(lastrow, offset, row, i):
|
2024-02-17 17:05:33 -08:00
|
|
|
thisrow = [f"d{i}" if offset + 1 == i else f"d{offset+1}-d{i}"]
|
2022-08-13 12:32:52 -07:00
|
|
|
thisrow += self._flatten(lastrow)
|
|
|
|
return self._column_filter(thisrow)
|
2022-07-31 15:03:19 -07:00
|
|
|
|
|
|
|
lastrow = None
|
|
|
|
offset = 0
|
2024-02-17 17:05:33 -08:00
|
|
|
self._rows = [self._column_filter(["Roll"] + self.headers)]
|
2023-04-12 10:37:31 -07:00
|
|
|
|
2024-01-17 21:15:39 -08:00
|
|
|
for face in range(self.die):
|
2022-07-31 15:03:19 -07:00
|
|
|
row = self._values[face]
|
|
|
|
if not lastrow:
|
|
|
|
lastrow = row
|
|
|
|
offset = face
|
|
|
|
continue
|
|
|
|
if row != lastrow:
|
|
|
|
self._rows.append(formatted(lastrow, offset, row, face))
|
|
|
|
lastrow = row
|
|
|
|
offset = face
|
2024-02-17 17:05:33 -08:00
|
|
|
self._rows.append(formatted(lastrow, offset, row, face + 1))
|
2022-07-31 15:03:19 -07:00
|
|
|
return self._rows
|
|
|
|
|
|
|
|
@property
|
|
|
|
def expanded_rows(self) -> List:
|
2024-02-17 17:05:33 -08:00
|
|
|
self._rows = [self._column_filter(["Roll"] + self.headers)]
|
2024-01-17 21:15:39 -08:00
|
|
|
for face in range(self.die):
|
2022-07-31 15:03:19 -07:00
|
|
|
row = self._values[face]
|
2024-02-17 17:05:33 -08:00
|
|
|
self._rows.append(self._column_filter([f"d{face+1}"] + row))
|
2022-07-31 15:03:19 -07:00
|
|
|
return self._rows
|
|
|
|
|
2024-01-18 22:48:58 -08:00
|
|
|
def reset(self) -> None:
|
|
|
|
self._generated_values = None
|
2024-02-17 17:05:33 -08:00
|
|
|
self._config()
|
2024-01-18 22:48:58 -08:00
|
|
|
|
2022-08-06 13:32:34 -07:00
|
|
|
def as_markdown(self) -> str:
|
2023-04-12 10:37:31 -07:00
|
|
|
return Table(self.rows).markdown()
|
2022-08-06 13:32:34 -07:00
|
|
|
|
2024-01-06 12:21:30 -08:00
|
|
|
def as_yaml(self, expanded: bool = False) -> dict:
|
|
|
|
struct = {}
|
|
|
|
for row in self.rows[1:]:
|
|
|
|
struct[row[0]] = {}
|
|
|
|
# pad rows with empty cols as necessary
|
2024-02-17 17:05:33 -08:00
|
|
|
cols = row[1:] + [""] * (len(self.headers) - len(row[1:]))
|
2024-01-06 12:21:30 -08:00
|
|
|
for idx, col in enumerate(cols):
|
2024-02-17 17:05:33 -08:00
|
|
|
struct[row[0]][self.headers[idx] if idx < len(self.headers) else "_"] = col
|
2024-01-06 12:21:30 -08:00
|
|
|
return yaml.dump(struct, sort_keys=False)
|
|
|
|
|
|
|
|
def as_table(self, width: int = 120, expanded: bool = False) -> str:
|
|
|
|
rows = self.expanded_rows if expanded else self.rows
|
|
|
|
table = rich.table.Table(*rows[0], width=width)
|
|
|
|
for row in rows[1:]:
|
|
|
|
table.add_row(*row)
|
|
|
|
return table
|
|
|
|
|
2024-01-17 20:08:01 -08:00
|
|
|
def set_headers(self, *headers) -> None:
|
|
|
|
self._headers = list(headers)
|
|
|
|
|
|
|
|
# identify which columns to hide in the output by recording where a
|
|
|
|
# None header appears
|
|
|
|
self._header_excludes = []
|
|
|
|
for i in range(len(self._headers)):
|
|
|
|
if self.headers[i] is None:
|
2024-02-17 17:05:33 -08:00
|
|
|
self._header_excludes.append(i + 1)
|
2024-01-17 20:08:01 -08:00
|
|
|
|
2022-07-31 15:03:19 -07:00
|
|
|
def _config(self):
|
2022-07-30 20:44:16 -07:00
|
|
|
"""
|
2022-07-31 15:03:19 -07:00
|
|
|
Parse data sources, generate headers, and create the column filters
|
2022-07-30 20:44:16 -07:00
|
|
|
"""
|
|
|
|
|
2022-07-31 15:03:19 -07:00
|
|
|
# create the datasource objects
|
|
|
|
self._data = []
|
|
|
|
for src in self._sources:
|
2023-12-19 22:03:59 -08:00
|
|
|
if type(src) is str:
|
2023-12-22 23:09:43 -08:00
|
|
|
src = [src]
|
|
|
|
for one_source in src:
|
2024-02-17 17:05:33 -08:00
|
|
|
ds = DataSource(one_source, frequency=self.frequency)
|
2023-12-19 22:03:59 -08:00
|
|
|
ds.load_source()
|
|
|
|
self._data.append(ds)
|
2022-07-31 15:03:19 -07:00
|
|
|
|
|
|
|
# merge the headers
|
2024-01-17 20:08:01 -08:00
|
|
|
headers = []
|
2022-07-31 15:03:19 -07:00
|
|
|
for ds in self._data:
|
2024-01-17 20:08:01 -08:00
|
|
|
headers += ds.headers
|
|
|
|
self.set_headers(*headers)
|
2022-07-31 15:03:19 -07:00
|
|
|
|
|
|
|
def _column_filter(self, row):
|
2024-02-17 17:05:33 -08:00
|
|
|
cols = [col or "" for (pos, col) in enumerate(row) if pos not in self._header_excludes]
|
2022-08-13 12:32:52 -07:00
|
|
|
# pad the row with empty columns if there are more headers than columns
|
2024-02-17 17:05:33 -08:00
|
|
|
cols = cols + [""] * (1 + len(self.headers) - len(row))
|
2023-04-12 10:37:31 -07:00
|
|
|
# strip the leading column if we're hiding the dice rolls
|
2024-01-17 21:15:39 -08:00
|
|
|
return cols[1:] if self.hide_rolls else cols
|
2022-08-13 12:32:52 -07:00
|
|
|
|
|
|
|
def _flatten(self, obj: List) -> List:
|
|
|
|
for member in obj:
|
|
|
|
if isinstance(member, Iterable) and not isinstance(member, (str, bytes)):
|
|
|
|
yield from self._flatten(member)
|
|
|
|
else:
|
|
|
|
yield member
|
2022-07-30 20:44:16 -07:00
|
|
|
|
2022-07-31 15:03:19 -07:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
rows = list(self.rows)
|
2024-02-17 17:05:33 -08:00
|
|
|
str_format = "\t".join(["{:10s}"] * len(rows[0]))
|
|
|
|
return "\n".join([str_format.format(*[r or "" for r in row]) for row in rows])
|