71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
|
import datetime
|
||
|
from sqlite3 import *
|
||
|
import sqlite3
|
||
|
import locale
|
||
|
import random
|
||
|
locale.setlocale(locale.LC_TIME, "nl_NL.utf-8") # swedish
|
||
|
|
||
|
class Services:
|
||
|
def __init__(self, sqlpath="sqlite.db") -> None:
|
||
|
self.sqlpath = sqlpath
|
||
|
self.db = sqlite3.connect(self.sqlpath, check_same_thread=False)
|
||
|
self.cursor = self.db.cursor()
|
||
|
self.PopulateDB()
|
||
|
|
||
|
def GetAllServices(self):
|
||
|
'''
|
||
|
Get all services. Returns a dictionary with, for each key a tuple with (date, groups)
|
||
|
'''
|
||
|
serv = self.cursor.execute("select * from services").fetchall()
|
||
|
res = {}
|
||
|
for s in serv:
|
||
|
date = datetime.datetime.strptime(s[1], "%Y-%m-%d %H:%M:%S")
|
||
|
groups = s[2].split(":")
|
||
|
res[s[0]] = (date, groups)
|
||
|
return res
|
||
|
|
||
|
def GetServices(self, number):
|
||
|
serv = self.GetAllServices()
|
||
|
res = {}
|
||
|
for i in serv:
|
||
|
if(number in serv[i][1]):
|
||
|
res[i] = serv[i]
|
||
|
return res
|
||
|
|
||
|
def PopulateDB(self):
|
||
|
'''
|
||
|
Populate simple db
|
||
|
'''
|
||
|
query = f"""CREATE TABLE IF NOT EXISTS services
|
||
|
(
|
||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
|
date STRING,
|
||
|
groups STRING
|
||
|
);
|
||
|
"""
|
||
|
self.cursor.execute(query)
|
||
|
|
||
|
def AddService(self, date, groups):
|
||
|
query = f"""
|
||
|
INSERT INTO services (date, groups)
|
||
|
VALUES('{date}', '{groups}');
|
||
|
"""
|
||
|
self.cursor.execute(query)
|
||
|
self.db.commit()
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
#fill db
|
||
|
ser = Services()
|
||
|
for i in range(100):
|
||
|
d = datetime.datetime.now()
|
||
|
d = d.replace(hour=random.randint(1, 23), day = random.randint(1, 28), month = random.randint(1, 12))
|
||
|
d = d.strftime("%Y-%m-%d %H:%M:%S")
|
||
|
g = ""
|
||
|
for j in range(10):
|
||
|
g += str(random.randint(1, 30))
|
||
|
g += ":"
|
||
|
ser.AddService(d, g)
|
||
|
r = ser.GetAllServices()
|
||
|
d = ser.GetServices(str(4))
|
||
|
print(d)
|
||
|
|