-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_random_pets.py
148 lines (126 loc) · 3.79 KB
/
insert_random_pets.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os,time
from uuid import uuid4
from random import randint
from sqlalchemy import create_engine
from db.consts import *
def connect():
ip = os.environ["EGOR_PUMPKIN_DB_IP"]
connection_url = f'mysql://root:{os.environ["EGOR_PUMPKIN_DB_PASSWORD"]}@{os.environ["EGOR_PUMPKIN_DB_IP"]}:3306/'
print("Connection url ---> ", connection_url )
# time.sleep(40)
engine = create_engine(connection_url)
connection = engine.connect()
query_create_user = "CREATE USER 'pumpkin' IDENTIFIED BY 'password';"
connection.execute(query_create_user)
query_grant_priveleges = "GRANT ALL PRIVILEGES ON * . * TO 'pumpkin';"
connection.execute(query_grant_priveleges)
query_create_database = "CREATE DATABASE Pumpkin;"
connection.execute(query_create_database)
connection.close()
connect()
connection_url = f'mysql://pumpkin:password@{os.environ["EGOR_PUMPKIN_DB_IP"]}:3306/Pumpkin'
print("Another one -->", connection_url)
engine = create_engine(f'mysql://pumpkin:password@{os.environ["EGOR_PUMPKIN_DB_IP"]}:3306/Pumpkin')
connection = engine.connect()
create_table_query = """
create table pet
(
id varchar(32) not null,
age int null,
breed varchar(35) null,
zipcode int null,
name varchar(50) null,
gender varchar(7) null,
species varchar(30) null,
constraint Pet_id_uindex
unique (id)
);
"""
alter_table_query = """
alter table pet
add primary key (id);
"""
connection.execute(create_table_query)
connection.execute(alter_table_query)
names= ['Cara',
'Berna',
'Adena',
'Tyree',
'Charisse',
'Archie',
'Maia',
'Jenni',
'Patrina',
'Herma',
'Aja',
'Janette',
'Marge',
'Boris',
'Alysha',
'Jamel',
'Floretta',
'Oretha',
'Sherita',
'Rea',
'Ian',
'Van',
'Augustus',
'Myrle',
'Johna',
'Nick',
'Augustine',
'Mohammad',
'Vikki',
'Cecelia',
'Quinn',
'Jerry',
'Astrid',
'Leroy',
'Dorine',
'Damien',
'Corrina',
'Kanesha',
'Mistie',
'Belen',
'Elma',
'Florance',
'Robert',
'Doretha',
'Magan',
'Kathryne',
'Kiara',
'Tai',
'Kim',
'Celina']
age_dict = AGE_FACTORS
cat_dict = CAT_BREED_FACTORS
dog_breed_dict = DOG_BREED_FACTORS
speecies_dict = SPECIES_FACTORS
zip_code_dict = ZIPCODE_FACTORS
for name in names:
name_dict = {}
name_dict['name'] = name
name_dict["id"] = ''.join(str(uuid4()).split("-"))
name_dict["gender"] = "Male" if randint(0,1) == 1 else "Female"
name_dict["species"] = "dog" if randint(0,1) == 1 else "cat"
age_list = list(age_dict.keys())
random_age_position = randint(0, len(age_list)-1)
name_dict["age"] = age_list[random_age_position]
zip_code_list = list(zip_code_dict.keys())
random_age_position = randint(0, len(zip_code_list)-1)
name_dict["zipcode"] = zip_code_list[random_age_position]
if name_dict["species"] == "dog":
dog_breed_list = list(dog_breed_dict.keys())
random_breed_position = randint(0, len(dog_breed_list)-1)
name_dict["breed"] = dog_breed_list[random_breed_position]
else:
cat_breed_list = list(cat_dict.keys())
random_breed_position = randint(0, len(cat_breed_list)-1)
name_dict["breed"] = cat_breed_list[random_breed_position]
if name_dict['age'] == ">8":
name_dict["age"] = 10
if name_dict["age"] == "<1":
name_dict["age"] = 0
insert_query = f"INSERT INTO pet(id,age,breed,zipcode,name,gender,species) VALUES ('{name_dict['id']}',{name_dict['age']},'{name_dict['breed']}', {name_dict['zipcode']}, '{name_dict['name']}', '{name_dict['gender']}', '{name_dict['species']}')"
print("Inserting pet with information --> ", name_dict)
connection.execute(insert_query)