-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·87 lines (72 loc) · 2.54 KB
/
app.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
from flask import Flask
from flask import send_from_directory
from flask import Response
import random
from flask import render_template
app = Flask(__name__)
def generate_username(full_name):
username = ""
first_name = full_name.split(" ")[0]
last_name = full_name.split(" ")[-1]
r = random.random()
if r < 0.33:
# initial + lastname + number
username = "{}{}{}".format(first_name[0], last_name, int(r*100+18))
elif r <0.66:
# fullname + number
username = "{}{}{}".format(first_name, last_name, int(r*100+18))
else:
# initials + number
username = "{}{}{}".format(first_name[0], last_name[0], int(r*10000+18))
#sanitize username
username = username.lower()
username = username.replace("æ", "ae")
username = username.replace("ø", "oe")
username = username.replace("å", "aa")
username = username.replace("'", "")
return username
def generate_full_name():
first_names = []
last_names = []
middle_names = []
with open("first_names.txt") as f:
for line in f.readlines():
if line.strip():
first_names.append(line.strip())
with open("last_names.txt") as f:
for line in f.readlines():
if line.strip():
last_names.append(line.strip())
with open("middle_names.txt") as f:
for line in f.readlines():
if line.strip():
middle_names.append(line.strip())
first_name = random.choice(first_names)
middle_name = random.choice(first_names)
last_name = random.choice(last_names)
probability_middle_name = 0.5
if random.random() > probability_middle_name:
full_name = "{} {} {}".format(first_name, middle_name, last_name)
else:
full_name = "{} {}".format(first_name, last_name)
return full_name
@app.route('/')
def index():
full_name = generate_full_name()
username = generate_username(full_name)
return render_template("base.html", full_name = full_name, username = username)
@app.route('/many')
def many():
names = []
for n in range(100):
full_name = generate_full_name()
name = {}
name["full_name"] = full_name
name["username"] = generate_username(full_name)
names.append(name)
return render_template("many.html", names = names)
@app.route('/static/<path:path>')
def send_static():
return send_from_directory("static", path)
if __name__ == '__main__':
app.run()