add comments to post.py

This commit is contained in:
Lynne Megido 2019-07-23 19:12:01 +10:00
parent 11b831b5b2
commit 98a344c878

11
post.py
View File

@ -34,6 +34,8 @@ client = Mastodon(
data = json.load(open('data.json', 'r'))
def get_object(kind):
# fill in the given thought with the necessary objects.
# for example, if the kind is "rides", generate a string like "Ride Name 1".
num = random.choice([1, 1, 1, 1, 1, 2, 2, 3])
if kind == "rides":
return "{} {}".format(random.choice(data['objects']['thrilling-rides'] + data['objects']['gentle-rides']), num)
@ -48,6 +50,7 @@ def get_object(kind):
elif kind == "stalls":
return "{} {}".format(random.choice(list(data['objects']['stalls']['edible'].keys()) + list(data['objects']['stalls']['other'].keys())), num)
elif kind == "item-stall":
# return an item and the stall it comes from.
stalls_weighted = []
for key in data['objects']['stalls']:
for i in range(len(data['objects']['stalls'][key])):
@ -57,6 +60,9 @@ def get_object(kind):
stall = "{} {}".format(choice[1], num)
return (choice[0], stall)
# generate a list of thought types for a weighted random choice.
# for example, if there were two plain thoughts and one rides thought, the list would be:
# ['plain', 'plain', 'rides']
thoughts_weighted = []
for key in data['thoughts']:
for i in range(len(data['thoughts'][key])):
@ -64,14 +70,19 @@ for key in data['thoughts']:
thought_type = random.choice(thoughts_weighted)
# choose a random thought of the type chosen above
thought = random.choice(data['thoughts'][thought_type])
if thought_type == "plain":
# we don't need to fill in anything here.
pass
elif thought_type == "item-stall":
# special case, get_object returns a tuple to fill in two blanks
objects = get_object(thought_type)
thought = thought.format(objects[0], objects[1])
else:
# just fill in the one blank
thought = thought.format(get_object(thought_type))
# post the thought
client.status_post(thought)