This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_data.py
executable file
·99 lines (84 loc) · 2.83 KB
/
generate_data.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/python3
## @package generate_data
# by Alex Timmermann and Frederik Will
# script for generating random passengers
# parameter: int passengers to be generated, default 10
# disclaimer: may not fullfill real world probabilities
import json
import sys
from pprint import pprint
from random import randint
from random import choice
# defining possible passenger attributes
destinations = ["Frankfurt", "London", "Paris", "New York"]
seat_prefs = ["window", "middle", "aisle"]
times = ["morning", "afternoon", "evening"]
seat_classes = ["economy", "business", "first"]
current_ID = 0
# number of passengers to generate
# default 10
if len(sys.argv) > 1:
count = int(sys.argv[1])
else:
count = 10
data = {}
passengers = []
## generates a passenger group of group_size
# if group_size=1 returns a single passenger
# else returns a group as a list
def generate_passenger(group_size=1):
dest = choice(destinations)
class_rand = randint(0, 100)
time = choice(times)
if class_rand < 60:
seat_class = seat_classes[0]
elif class_rand < 90:
seat_class = seat_classes[1]
else:
seat_class = seat_classes[2]
if group_size == 1:
return generate_passenger_group(seat_class, dest, time)
else:
group = []
for i in range(0, group_size):
group.append(generate_passenger_group(seat_class, dest, time))
return group
## basically a helper, returns a passenger for a specified seat_class and destination
def generate_passenger_group(seat_class, dest, time):
global current_ID
seat_pref = choice(seat_prefs)
passenger = {'ID': current_ID, 'destination': dest, 'time': time, 'class': seat_class, 'seat_pref': seat_pref}
current_ID = current_ID + 1
return passenger
# generate groups
# following code could be refactored
current_passenger = 0
while current_passenger < count:
group_rand = randint(0, 100)
if group_rand < 50:
passengers.append(generate_passenger())
current_passenger += 1
else:
group_size_rand = randint(0, 99)
if group_size_rand < 50:
group_size = 2
elif group_size_rand < 70:
group_size = 3
elif group_size_rand < 85:
group_size = 4
elif group_size_rand < 95:
group_size = 5
elif group_size_rand < 100:
group_size = 6
if group_size > (count - current_passenger):
group_size = (count - current_passenger)
passengers.append(generate_passenger(group_size))
current_passenger += group_size
# generate passengers
#for i in range(count):
# passengers.append(generate_passenger())
#seat_class = choice(seat_classes)
data["passengers"] = passengers
pprint(data)
with open('passengers.json', 'w') as outfile:
json.dump(data, outfile, sort_keys=True, indent=4, separators=(',', ': '))