-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_simulation.py
More file actions
79 lines (62 loc) · 1.48 KB
/
Copy pathtable_simulation.py
File metadata and controls
79 lines (62 loc) · 1.48 KB
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
import uuid
import json
import time
import random
import requests
import pandas as pd
from connectors_to_databases import PostgreSQL
pg = PostgreSQL(
host='localhost',
port=5432,
login='postgres',
password='postgres',
)
def insert_user():
r = requests.get(url='https://randomuser.me/api/')
d = json.loads(r.text)
dict_user = d['results'][0]
d_ = {
'first_name': [dict_user['name']['first']],
'last_name': [dict_user['name']['last']],
'email': [dict_user['email']],
}
df = pd.DataFrame(d_)
try:
pg.insert_df(
df=df,
pg_table_schema='inventory',
pg_table_name='customers',
)
except Exception as ex:
pass
time.sleep(2)
def update_user():
df_id = pg.execute_to_df(
'''
SELECT id FROM inventory.customers
'''
)
len_list = len(df_id.id)
random_user = random.randint(0, len_list)
try:
pg.execute_script(
f'''
UPDATE
inventory.customers
SET
first_name = '{str(uuid.uuid4())}',
last_name = '{str(uuid.uuid4())}'
WHERE
id = {df_id.id[random_user]}
'''
)
except Exception as ex:
pass
time.sleep(2)
while True:
if random.randint(1, 1000) % 2 == 0:
print('INSERT new USER')
insert_user()
else:
print('UPDATE current USER')
update_user()