adding set_frequency()
This commit is contained in:
parent
710560ea0d
commit
e751702610
|
@ -4,6 +4,12 @@ import yaml
|
||||||
from typing import IO
|
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:
|
class DataSource:
|
||||||
"""
|
"""
|
||||||
Represents a yaml data source used to generate roll tables.
|
Represents a yaml data source used to generate roll tables.
|
||||||
|
@ -66,6 +72,19 @@ class DataSource:
|
||||||
frequencies.update(**self.metadata['frequencies'])
|
frequencies.update(**self.metadata['frequencies'])
|
||||||
self.frequencies = frequencies[self.frequency]
|
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:
|
def random_frequencies(self, count: int = 1) -> list:
|
||||||
"""
|
"""
|
||||||
Choose random option names from the frequency table.
|
Choose random option names from the frequency table.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user