-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneratesampledata.py
More file actions
executable file
·70 lines (58 loc) · 2.74 KB
/
Copy pathgeneratesampledata.py
File metadata and controls
executable file
·70 lines (58 loc) · 2.74 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
#!/usr/bin/env python3
"""
Spyder Editor
This is a temporary script file.
"""
import pymysql
import random
from datetime import datetime
from datetime import date
from datetime import timedelta
import logging
class candySampleData:
def createSampleData(self, numberOfDays, maxEntriesPerDay, startDateInt):
databaseentries = []
try:
start_datetime = datetime.strptime(str(startDateInt),"%Y%m%d")
except Exception as e:
logging.error("Error when trying to convert start date to date value in Python.." + str(startDateInt))
raise
for x in range (0, numberOfDays):
print("Here we go! " + str(x))
todaysEntries = random.randint(0,maxEntriesPerDay)
currentDate = start_datetime + timedelta(days=x)
for entry in range(0,todaysEntries):
print("Entry for day " + str(x) + ": " + str(entry) + " for " + str(currentDate))
entryHour = random.randint(0,23)
entryMinute = random.randint(0,59)
entrySecond = random.randint(0,59)
currentDate = (currentDate + timedelta(hours=entryHour, minutes=entryMinute, seconds=entrySecond))
currentDateStr = currentDate.strftime("%Y,%m,%d, %H:%M:%S")
currentEntrySQL = "INSERT INTO candydb.candycounts (candyconsumption_date_ik,logged_date) VALUES (" + currentDate.strftime("%Y%m%d") + ",STR_TO_DATE('" + currentDateStr + "', '%Y,%m,%d,%T'));"
databaseentries.append({"SQL Statement":currentEntrySQL,"Entry Date":currentDate})
return databaseentries
def connectToDB(self):
connection = pymysql.connect(
host = 'localhost'
,user = 'cobicandy'
,password = 'cobi'
,charset = 'utf8mb4'
,cursorclass=pymysql.cursors.DictCursor
)
connection.connect()
return connection
#syntax: STR_TO_DATE('2018,01,01, 17:23:12', '%Y,%m,%d,%T')
def createExampleData(self, daysOfSampleData, maxRowsPerDay, startDateInteger, truncateDB = True):
exampleData = self.createSampleData(numberOfDays = daysOfSampleData, maxEntriesPerDay = maxRowsPerDay, startDateInt = startDateInteger)
mysql = self.connectToDB()
with mysql.cursor() as cursor:
if truncateDB == True:
sql = "TRUNCATE TABLE candydb.candycounts"
cursor.execute(sql)
for exampleRow in (exampleData):
sql = exampleRow["SQL Statement"]
# print(sql)
cursor.execute(sql)
mysql.commit()
mysql.close()
print("That's all folks!")