forked from namebrandon/Sparkov_Data_Generation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_config.py
76 lines (63 loc) · 2.24 KB
/
main_config.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
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
import json
class MainConfig:
def __init__(self, main):
self.config = self.all_profiles_dicts(main)
# convert type to a tuple
def convert_config_type(self, x):
if type(x) is dict:
minval = float(x['min'])
maxval = float(x['max'])
if maxval < 0:
return (minval, float('inf'))
else:
return (minval, maxval)
else:
return x
def all_profiles_dicts(self, main):
main_config = json.loads(json.dumps(main).\
replace('\\n','').\
replace('\\t','').\
replace('\\','').\
replace('"{','{').\
replace('}"','}'))
all_profiles = {}
for pf in main_config:
if pf != 'leftovers.json':
all_profiles[pf] = {}
for qual in main_config[pf]:
all_profiles[pf][qual] = \
self.convert_config_type(main_config[pf][qual])
return all_profiles
def in_profile(self, person_dict, profile_quals):
for pq in profile_quals:
if pq == 'age':
if self.fits_qual(return_age(person_dict),\
profile_quals[pq]) == False:
return False
else:
if self.fits_qual(person_dict[pq],\
profile_quals[pq]) == False:
return False
return True
def fits_qual(self, person_val, range_tuple):
if type(range_tuple) is list:
# matching value in string list
# (e.g. ['M','F'])
if unicode(person_val) in range_tuple or \
str(person_val) in range_tuple:
return True
# doesn't match
else:
return False
elif type(range_tuple) is unicode or type(range_tuple) is str:
if unicode(person_val) == unicode(range_tuple):
return True
# doesn't match
else:
return False
# range
if float(person_val) >= float(range_tuple[0]) and\
float(person_val) <= float(range_tuple[1]):
return True
else:
return False