rewrite for a more object oriented fashion
This commit is contained in:
parent
8341b2973f
commit
6c9e2c2db8
122
generate.py
122
generate.py
|
@ -27,20 +27,116 @@ def matriculation_number():
|
|||
# Uhrzeiten, siehe Infos von Verantwortlichen
|
||||
# Groß-Klein-Schreibung
|
||||
|
||||
def generate_line():
|
||||
line = choice(["Student ", "Studentin ", "", "Studierende:r "])
|
||||
if random() > .2:
|
||||
class Choice:
|
||||
def __init__(self, **data):
|
||||
""" stores a dict.
|
||||
the keys are strings that indicate the next state,
|
||||
the values are lists that store the possible emissions in transition to that new state.
|
||||
"""
|
||||
self.data = data
|
||||
self.new_states = list(self.data.keys())
|
||||
|
||||
def choose(self):
|
||||
new_state = choice(self.new_states)
|
||||
emission = choice(self.data[new_state])
|
||||
return new_state, emission
|
||||
|
||||
class SingleArgChoice:
|
||||
def __init__(self, kwarg):
|
||||
if len(kwarg) != 1:
|
||||
raise ValueError(
|
||||
f"{self.__class__.__name__} takes exactly one keyword argument!"
|
||||
)
|
||||
for self.next_state, self.value in kwarg.items():
|
||||
pass
|
||||
|
||||
class RandInt(SingleArgChoice):
|
||||
def __init__(self, **kwarg):
|
||||
super().__init__(kwarg)
|
||||
assert len(self.value) == 2
|
||||
assert type(self.value[0]) == int
|
||||
assert type(self.value[1]) == int
|
||||
|
||||
self.minimum = self.value[0]
|
||||
self.maximum = self.value[1]
|
||||
|
||||
def choose(self):
|
||||
number = randint(self.minimum, self.maximum)
|
||||
return self.next_state, f"{number} "
|
||||
|
||||
class FixedText(SingleArgChoice):
|
||||
def __init__(self, **kwarg):
|
||||
super().__init__(kwarg)
|
||||
self.text = self.value
|
||||
|
||||
def choose(self):
|
||||
return self.next_state, self.text
|
||||
|
||||
class Network:
|
||||
def __init__(self, **choices):
|
||||
""" takes a dict,
|
||||
the keys are strings that name states,
|
||||
the values are the choices for new states and emissions possible in these states.
|
||||
the 'start' state and the 'stop' state have their respective special roles.
|
||||
"""
|
||||
self.choices = choices
|
||||
self.recursion_warning = 100
|
||||
|
||||
def choose(self):
|
||||
state = 'start'
|
||||
emissions = []
|
||||
states = [state]
|
||||
while state != 'stop':
|
||||
state, emission = self.choices[state].choose()
|
||||
emissions.append(emission)
|
||||
states.append(states)
|
||||
if len(emissions) > self.recursion_warning:
|
||||
raise RecursionError(
|
||||
"there are over a hundred emissions. probably a bug!"
|
||||
)
|
||||
|
||||
return emissions, states
|
||||
|
||||
network = Network(
|
||||
start = Choice(
|
||||
student = ["Student ", "Studentin ", "", "Studierende:r "],
|
||||
matr_nr = [""],
|
||||
),
|
||||
student = Choice(
|
||||
last_name = ["Anna ", "Anton ", "Amal "],
|
||||
),
|
||||
last_name = Choice(
|
||||
student_mit = ["Beier ", "Behar ", "Berger "],
|
||||
),
|
||||
student_mit = Choice(
|
||||
matr_nr = ["mit "],
|
||||
),
|
||||
matr_nr = Choice(
|
||||
matr_number = ["Matrikelnummer "],
|
||||
),
|
||||
matr_number = RandInt(
|
||||
hat_den = [
|
||||
40_000_000,
|
||||
55_000_000,
|
||||
]
|
||||
),
|
||||
hat_den = FixedText(
|
||||
versuch = "hat den "
|
||||
),
|
||||
versuch = Choice(
|
||||
nicht = ["Erstversuch ", "1. Versuch ", "Zweitversuch ", "2. Versuch "],
|
||||
),
|
||||
nicht = Choice(
|
||||
bestanden = ["nicht ", ""],
|
||||
),
|
||||
bestanden = FixedText(
|
||||
stop = "bestanden."
|
||||
),
|
||||
)
|
||||
|
||||
if line and random() > .5:
|
||||
line += " mit "
|
||||
line += "(" if random() > .5 else ""
|
||||
line += "Matr" + ("ikel" if random() > .5 else "") + ("Nr" if random() > .5 else "Nummer")
|
||||
line += " " + str(matriculation_number())
|
||||
line += ")" if random() > .5 else ""
|
||||
line += " hat den " + choice(["Erstversuch", "1. Versuch", "Zweitversuch", "2. Versuch"])
|
||||
line += choice([" nicht", ""]) + " bestanden."
|
||||
return line
|
||||
|
||||
if __name__ == "__main__":
|
||||
for __ in range(200):
|
||||
print(generate_line())
|
||||
emissions, _ = network.choose()
|
||||
line = ''.join(emissions)
|
||||
print(line)
|
||||
|
|
Loading…
Reference in a new issue