mirror of
https://github.com/UofCBaja/BajaCloud.git
synced 2025-06-15 13:14:17 -06:00
Merge branch 'ReadDB' into Packagers
This commit is contained in:
commit
838e53905a
85
ReadDB.py
85
ReadDB.py
@ -1,42 +1,75 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import json
|
import json
|
||||||
|
from filelock import FileLock, Timeout
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Define the path to the Excel file and the lock file
|
||||||
|
excel_file_path = "./interview_database.xlsx"
|
||||||
|
lock_file_path = "./interview_database.xlsx.lock"
|
||||||
|
|
||||||
def ReadDatabase():
|
def ReadDatabase():
|
||||||
"""
|
"""
|
||||||
Reads the Database
|
Reads the Database
|
||||||
|
|
||||||
``REQUIRES``: None
|
``REQUIRES``: None
|
||||||
|
|
||||||
``PROMISES``: JSON (Interview Avaiable Slots)
|
``PROMISES``: JSON (Interview Available Slots)
|
||||||
|
|
||||||
``Develop in part by``: Ahmad
|
``Developed in part by``: Ahmad
|
||||||
|
|
||||||
``Contact``: ahmad.ahmad1@ucalgary.ca
|
``Contact``: ahmad.ahmad1@ucalgary.ca
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# Load the updated Excel file into a pandas DataFrame
|
# Retry parameters
|
||||||
excel_file_path = "./interview_database.xlsx"
|
max_retries = 60 # Maximum number of retries if the file is locked
|
||||||
df = pd.read_excel(excel_file_path)
|
retry_interval = 0.5 # Wait time (in seconds) between retries
|
||||||
|
|
||||||
# Initialize the dictionary to store the structured data
|
retries = 0
|
||||||
interview_data = {}
|
while retries < max_retries:
|
||||||
|
try:
|
||||||
|
# Attempt to acquire a shared read (non-blocking) access
|
||||||
|
with FileLock(lock_file_path, timeout=0): # Non-blocking, checks if the lock exists
|
||||||
|
# Load the Excel file into a pandas DataFrame
|
||||||
|
df = pd.read_excel(excel_file_path)
|
||||||
|
|
||||||
# Group the DataFrame by Date, Start Time, and Slot for organization
|
# Initialize the dictionary to store the structured data
|
||||||
for _, row in df.iterrows():
|
interview_data = {}
|
||||||
date = str(row['Date'])
|
|
||||||
start_time = str(row['Start Time'])
|
|
||||||
slot = int(row['Slot']) if not pd.isna(row['Slot']) else 0
|
|
||||||
#Returns amount of interviewees in the slot and if it's empty it will return 0
|
|
||||||
interviewee_amount = len(str(row['Interviewee Name']).split()) if str(row['Interviewee Name']) != "nan" else 0
|
|
||||||
|
|
||||||
# Check if the slot is available for an interviewee to attend
|
# Group the DataFrame by Date, Start Time, and Slot for organization
|
||||||
avaliable_slots = interviewee_amount != slot
|
for _, row in df.iterrows():
|
||||||
if avaliable_slots:
|
date = str(row['Date'])
|
||||||
# Initialize nested structure if not present
|
start_time = str(row['Start Time'])
|
||||||
if date not in interview_data:
|
slot = int(row['Slot']) if not pd.isna(row['Slot']) else 0
|
||||||
interview_data[date] = {}
|
|
||||||
#Adds the start time and duration if not present
|
# Returns the number of interviewees in the slot; returns 0 if empty
|
||||||
if start_time not in interview_data[date]:
|
interviewee_amount = len(str(row['Interviewee Name']).split()) if str(row['Interviewee Name']) != "nan" else 0
|
||||||
interview_data[date][start_time] = {
|
|
||||||
'Meeting Duration': row['Meeting Duration'],
|
# Check if the slot is available for an interviewee to attend
|
||||||
}
|
available_slots = interviewee_amount != slot
|
||||||
return interview_data
|
if available_slots:
|
||||||
|
# Initialize nested structure if not present
|
||||||
|
if date not in interview_data:
|
||||||
|
interview_data[date] = {}
|
||||||
|
# Add the start time and duration if not present
|
||||||
|
if start_time not in interview_data[date]:
|
||||||
|
interview_data[date][start_time] = {
|
||||||
|
'Meeting Duration': row['Meeting Duration'],
|
||||||
|
}
|
||||||
|
return interview_data # Successfully read the database
|
||||||
|
|
||||||
|
except Timeout:
|
||||||
|
# File is locked; wait and retry
|
||||||
|
retries += 1
|
||||||
|
print(f"File is locked, retrying ({retries}/{max_retries})...")
|
||||||
|
time.sleep(retry_interval)
|
||||||
|
|
||||||
|
# If max retries are exceeded, raise an error
|
||||||
|
raise RuntimeError("Unable to access the database after multiple attempts due to a file lock.")
|
||||||
|
|
||||||
|
# Example usage of the ReadDatabase function
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
data = ReadDatabase()
|
||||||
|
print(json.dumps(data, indent=4))
|
||||||
|
except RuntimeError as e:
|
||||||
|
print(e)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user