-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
30 lines (19 loc) · 926 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import json
import random
from collections import namedtuple
def random_choice_index(chances): # choose one option from list of chances, returning its index
# the dice will land on some number between 1 and the sum of the chances
# dice = libtcod.random_get_int(None, 1, sum(chances))
return chances[random.randint(0, len(chances) -1)]
def random_choice(chances_dict):
# choose one option from dictionary of chances, returning its key
chances = chances_dict.values()
strings = chances_dict.keys()
return strings[random_choice_index(chances)]
# https://stackoverflow.com/questions/6578986/how-to-convert-json-data-into-a-python-object
def _json_object_hook(d): return namedtuple('X', d.keys())(*d.values())
def json2obj(data): return json.loads(data, object_hook=_json_object_hook)
def pad(text, desired_length):
while len(text) < desired_length:
text += " "
return text