-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrf20008_gen_in_progress.py
More file actions
98 lines (82 loc) · 3.39 KB
/
rf20008_gen_in_progress.py
File metadata and controls
98 lines (82 loc) · 3.39 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#TODO: test
GUI = False
import ast, random, json
#returning - preparing for GUI-ification (although I haven't figured how to make this return anything)
def JSONify(chardatas = [{"exists": "false"}], success="false",
errorCause = "No Error", debugInfo = {"provided": "false"},
extraInfo = {"exists": "false"}):
if not success == "true":
return {"success": success, "characters": chardatas, "cause": errorCause, "debug": debugInfo, "extraInfo":extraInfo}
else:
return {"success": success, "characters": chardatas, "debug": debugInfo, "extraInfo": extraInfo}
#for gui-ification
if GUI:
import requests
#data preparation
with open(input('What file? '), 'r') as f:
data = ast.literal_eval(f.read())
f.close()
#variables
populationsDict = {}
charAttributes = {"exists": "true",
"region": None,
"gender": None,
"name": None,
"religion": None,
"political party": None,
"job": None,
"age": None}
#custom error types
#TODO: eliminate or make more pythonic
class NonExistingAttributeError(AttributeError):
'''An attribute for the character doesn't exist!'''
pass
class InvalidDataError(Exception):
'''The data is invalid.'''
pass
#selects from categorical distribution as represented in dictionary
#code credit to StackOverflow user "Boojum"
#https://stackoverflow.com/questions/3655430/selection-based-on-percentage-weighting
def select(values):
'''Selects randomly from a categorical distribution.
{any : numeric} -> any'''
variate = random.random() * sum( values.values() )
cumulative = 0.0
for item, weight in values.items():
cumulative += weight
if variate < cumulative:
return item
return item # Shouldn't get here, but just in case of rounding...
#TODO: implement random trait selection for all traits
#first for region
region = select({region: data[region]["population"] for region in data.keys()})
print(region) #debug
charAttributes["region"] = region
#gender?
gender = select({gender: data[region]["Genders"][gender]["population"] for gender in data[region]["Genders"].keys()})
print(gender) #debug
charAttrributes["gender"]=gender
##########-------------------------------------------#########
#Old code burial
#########---------------------------------------------########
#burial starts under this line
############-----------------------------------------#########
###next we do Gender
##gendersList = list(data[charAttributes["region"]]["Genders"].keys())
##print(f'gendersList = {gendersList}') #debug
###now, Names
##NamesList = list(data[charAttributes["region"]]["Genders"][charAttributes["gender"]]["Names"].keys)
##print(f'NamesList = {NamesList}') #debug
###fourthly, religion
##ReligionsList = list(data[charAttributes["region"]]["Religions"].keys())
##print(f'ReligionsList = {ReligionsList}') #debug
###fifthly, political party
###assuming political party is independent of religion
##PoliticalPartiesList = list(data[charAttributes["region"]]["Political Parties"].keys())
##print(f'PoliticalPartiesList = {PoliticalPartiesList}') #debug
###sixthly, Jobs
##JobsList = list(data[charAttributes["region"]]["Jobs"].keys())
##print(f'JobsList = {JobsList}') #debug
###finally, ages
##AgesList = list(data[charAttributes["region"]]["Ages"].keys())
##print(f'AgesList = {AgesList}') #debug