More documentation about classes.

This commit is contained in:
Bram Barbieri
2024-04-02 15:32:45 +02:00
parent cf16a2cdc1
commit b6c2bdb4a4
2 changed files with 21 additions and 11 deletions

View File

@@ -1,21 +1,27 @@
## Questionaire class
This file prmairly is dedicated to the class which gets its data form the questionare and pushes it to the database.
This is done this way to make the code more visable and more clear.
```py
#Importing different librarys.
import mysql.connector
import json
#importing different classes.
from classes_data import Node
from classes_data import dbLogin
```
Here a class is made to again provide more clear viewing and more reusability.
The
```py
#Node is between brackets to show that this class is a child class from the parent class "Node"
class EnqueteNode(Node):
query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
#A private query to use later in a function.
__query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
#use a super to get info from the parent class.
def __init__(self, macAdress, response, questionID):
super().__init__(macAdress)
self.response = response
self.questionID = questionID
#making a database connection to then load in the processed data.
async def processEnqueteNodeData(data, nodeID):
try:
mydb = dbLogin()
@@ -26,12 +32,15 @@ class EnqueteNode(Node):
EnqueteNode.questionID = (processedData['QuestionID'])
EnqueteNode.response = (processedData['Response'])
#an array with the data to push.
pushingDataArray = [(EnqueteNode.questionID, nodeID, EnqueteNode.response)]
#push the data according to the query to the database.
for i in pushingDataArray:
print(EnqueteNode.query, i)
cursor.execute(EnqueteNode.query, i)
print(EnqueteNode.__query, i)
cursor.execute(EnqueteNode.__query, i)
mydb.commit()
#print an error.
except mysql.connector.Error as err:
print("MySQL Error:", err)
finally:

View File

@@ -5,7 +5,8 @@ from classes_data import Node
from classes_data import dbLogin
class EnqueteNode(Node):
query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
__query = "INSERT INTO `Reply` (Result, Node_NodeID, Question_QuestionID) VALUES (%s, %s, %s)"
def __init__(self, macAdress, response, questionID):
super().__init__(macAdress)
@@ -25,8 +26,8 @@ class EnqueteNode(Node):
pushingDataArray = [(EnqueteNode.questionID, nodeID, EnqueteNode.response)]
for i in pushingDataArray:
print(EnqueteNode.query, i)
cursor.execute(EnqueteNode.query, i)
print(EnqueteNode.__query, i)
cursor.execute(EnqueteNode.__query, i)
mydb.commit()
except mysql.connector.Error as err:
print("MySQL Error:", err)