add methods to mathDB and change snake_case to camelCase

This commit is contained in:
2023-10-06 11:16:36 +02:00
parent 91364e05fb
commit 31043963a7

View File

@@ -1,4 +1,3 @@
from lxml import html, etree from lxml import html, etree
import requests import requests
import threading import threading
@@ -139,7 +138,7 @@ class mathPage:
class mathDB: class mathDB:
def __init__(self, db_file): def __init__(self, db_file):
self.db_file = db_file self.db_file = db_file
self.init_db() self.initDB()
def createConnection(self): def createConnection(self):
""" create a database connection to a SQLite database """ """ create a database connection to a SQLite database """
@@ -153,7 +152,7 @@ class mathDB:
if conn: if conn:
return(conn) return(conn)
def create_table(self, conn, create_table_sql): def createTable(self, conn, create_table_sql):
""" create a table from the create_table_sql statement """ create a table from the create_table_sql statement
:param conn: Connection object :param conn: Connection object
:param create_table_sql: a CREATE TABLE statement :param create_table_sql: a CREATE TABLE statement
@@ -167,7 +166,7 @@ class mathDB:
def init_db(self): def initDB(self):
conn = self.createConnection() conn = self.createConnection()
sql_create_main_table = """CREATE TABLE IF NOT EXISTS mathematicians ( sql_create_main_table = """CREATE TABLE IF NOT EXISTS mathematicians (
id integer PRIMARY KEY, id integer PRIMARY KEY,
@@ -181,34 +180,96 @@ class mathDB:
third_advisor integer third_advisor integer
);""" );"""
if conn is not None: if conn is not None:
self.create_table(conn, sql_create_main_table) self.createTable(conn, sql_create_main_table)
conn.close() conn.close()
else: else:
print("Cannot create database connection") print("Cannot create database connection")
def insert_person(self, mathPage): def insertPerson(self, mathPage):
conn = self.createConnection() conn = self.createConnection()
cur = conn.cursor() cur = conn.cursor()
row = self.get_person(mathPage.id) row = self.getPerson(mathPage.id)
if len(row) == 0: if len(row) == 0:
cur.execute("INSERT INTO mathematicians VALUES (?,?,?,?,?,?,?,?,?)", mathPage.getEntry()) cur.execute("INSERT INTO mathematicians VALUES (?,?,?,?,?,?,?,?,?)", mathPage.getEntry())
else: else:
cur.execute(f""" cur.execute("""
UPDATE mathematicians UPDATE mathematicians
SET name = '{mathPage.name}', SET name = ?,
title = '{mathPage.title}', title = ?,
institute = '{mathPage.inst}', institute = ?,
year = '{mathPage.year}', year = ?,
thesis = '{mathPage.diss}', thesis = ?,
first_advisor = '{mathPage.advisorID[0]}', first_advisor = ?,
second_advisor = '{mathPage.advisorID[1]}', second_advisor = ?,
third_advisor = '{mathPage.advisorID[2]}' third_advisor = ?
WHERE id = {mathPage.id} WHERE id = ?
""") """, (mathPage.name, mathPage.title, mathPage.inst, mathPage.year, mathPage.diss, mathPage.advisorID[0], mathPage.advisorID[1],mathPage.advisorID[2], mathPage.id))
conn.commit() conn.commit()
conn.close() conn.close()
def get_person(self, id): def findMissing(self, limit):
"""
Find missing entries in database.
"""
conn = sqlite3.connect(self.db_file)
cur = conn.cursor()
missing = []
if type(limit) == int:
limit = range(1, limit+1)
try:
l = len(limit)
except TypeError as e:
print("Wrong parameter. Limit must be an iterable object or an integer")
for i in limit:
if type(i) == int:
cur.execute("SELECT EXISTS(SELECT * FROM mathematicians WHERE id = ?)", (i,))
p = cur.fetchall()[0][0]
#print(p)
else:
print("Error: wrong data Type of limit parameter")
return(missing)
if p == 0:
missing.append(i)
conn.close()
return(missing)
def exists(self, id):
conn = sqlite3.connect(self.db_file)
cur = conn.cursor()
cur.execute("SELECT EXISTS(SELECT * FROM mathematicians WHERE id = ?)", (id,))
p = cur.fetchall()[0][0]
conn.close()
if p == 1:
return(True)
else:
return(False)
def fetchMissing(self, limit):
missing = self.findMissing(limit)
if len(missing) > 0:
self.populateDB(missing)
def checkMissingData(self, id):
conn = sqlite3.connect(self.db_file)
cur = conn.cursor()
cur.execute("SELECT EXISTS(SELECT * FROM mathematicians WHERE id = ?)", (id,))
p = cur.fetchall()[0][0]
if p == 0:
conn.close()
return((1,1,1,1,1,1,1,1))
else:
cur.execute("SELECT * FROM mathematicians WHERE id = ?", (id,))
data = cur.fetchall()[0]
conn.close()
return(data)
# print(data)
def getPerson(self, id):
conn = self.createConnection() conn = self.createConnection()
cur = conn.cursor() cur = conn.cursor()
res = cur.execute(f"SELECT * FROM mathematicians WHERE id = {id}") res = cur.execute(f"SELECT * FROM mathematicians WHERE id = {id}")
@@ -216,7 +277,18 @@ class mathDB:
conn.close() conn.close()
return res return res
def populate_db(self, limit, chunk = 10): # def _makeLimitIterable(self, limit):
# if type(limit) == int:
# ret = range(1, limit+1)
# try:
# l = len(limit)
# ret = limit
# except TypeError as e:
# print("Wrong parameter. Limit must be an iterable object or an integer")
# return(ret)
def populateDB(self, limit, chunk = 10):
if type(limit) == int: if type(limit) == int:
limit = range(1, limit+1) limit = range(1, limit+1)
try: try:
@@ -240,7 +312,7 @@ class mathDB:
for j in range(chunk): for j in range(chunk):
print(f"Adding MathID {limit[i*chunk+j]} to database. Entry {i*chunk+j+1} of {l}.", end= '\r') print(f"Adding MathID {limit[i*chunk+j]} to database. Entry {i*chunk+j+1} of {l}.", end= '\r')
self.insert_person(persons[j]) self.insertPerson(persons[j])
threads = list() threads = list()
persons = list() persons = list()
@@ -255,11 +327,11 @@ class mathDB:
thread.join() thread.join()
for j in range(r): for j in range(r):
print(f"Adding MathID {limit[i*chunk+j]} to database. Entry {i*chunk+j+1} of {l}.", end= '\r') print(f"Adding MathID {limit[n*chunk+j]} to database. Entry {n*chunk+j+1} of {l}.", end= '\r')
self.insert_person(persons[j]) self.insertPerson(persons[j])
# print(f"Downloading entry {i} of {limit}", end= '\r') # print(f"Downloading entry {i} of {limit}", end= '\r')
# page = mathPage(i) # page = mathPage(i)
# self.insert_person(page) # self.insertPerson(page)
class mathGenealogy: class mathGenealogy: