-
Notifications
You must be signed in to change notification settings - Fork 0
/
users_generator.py
43 lines (34 loc) · 1.49 KB
/
users_generator.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
import psycopg2
from randomuser import RandomUser
def gen_hundred_users():
user_list = RandomUser.generate_users(100, {'gender': 'male'})
formatted_users = []
for user in user_list:
formatted_user = (user.get_first_name(),
user.get_last_name(),
user.get_age(),
user.get_gender(),
user.get_email())
formatted_users.append(formatted_user)
return formatted_users
def insert_users(users):
with psycopg2.connect(dbname='postgres', user='postgres', password='postgres',
host='db', port=5432) as conn:
with conn.cursor() as cursor:
cursor.execute('''CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
first_name varchar(30) NOT NULL,
last_name varchar(30) NOT NULL,
age int NOT NULL,
gender varchar(6) NOT NULL,
email varchar(50) NOT NULL
)''')
records_list_template = ','.join(['%s'] * len(users))
insert_query = 'INSERT INTO users (first_name, last_name, age, gender, email) values {}'.\
format(records_list_template)
cursor.execute(insert_query, users)
def main():
formatted_users = gen_hundred_users()
insert_users(formatted_users)
if __name__ == '__main__':
main()