adding set_frequency()

This commit is contained in:
evilchili 2023-12-24 11:48:39 -08:00
parent 710560ea0d
commit e751702610

View File

@ -4,6 +4,12 @@ import yaml
from typing import IO
class UnknownFrequencyError(Exception):
"""
Thrown when attempting to set a datasource's fequency without a frequency table in the metadat.
"""
class DataSource:
"""
Represents a yaml data source used to generate roll tables.
@ -66,6 +72,19 @@ class DataSource:
frequencies.update(**self.metadata['frequencies'])
self.frequencies = frequencies[self.frequency]
def set_frequency(self, frequency: str) -> None:
"""
Select a new frequency distribution from the data source metadata.
"""
if 'frequencies' not in self.metadata:
raise UnknownFrequencyError(
"Cannnot set a new frequency because there is no frequency table in the metadata."
)
if frequency not in self.metadata['frequencies']:
raise UnknownFrequencyError(f"{frequency} is not present in the frequency table.")
self.frequency = frequency
self.init_frequencies()
def random_frequencies(self, count: int = 1) -> list:
"""
Choose random option names from the frequency table.