database.py
Database
¶
Model for database, encapsulates a TinyDB object and the created objects.
Attributes:
| Name | Type | Description |
|---|---|---|
location |
str |
The local path for the TinyDB JSON file. |
players |
dict |
The Player objects created from the database. |
tournaments |
dict |
The tournament objects created from the database. |
db |
tinydb.database.TinyDB |
The TinyDB object created from the JSON file. |
__init__(self, location)
special
¶
Constructor for Database. Initiates TinyDB loading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location |
str |
The local path for the TinyDB JSON file. |
required |
Source code in models/database.py
def __init__(self, location: str):
"""Constructor for Database. Initiates TinyDB loading.
Args:
location (str): The local path for the TinyDB JSON file.
"""
self.location = location
self.players = {}
self.tournaments = {}
self.db = None
self.load_database()
create_empty_database()
¶
Creates an empty database JSON file if needed.
Source code in models/database.py
def create_empty_database():
"""Creates an empty database JSON file if needed."""
with open(self.location, "w+") as f:
f.write("{}")
load_database(self)
¶
Loads a TinyDB object from a JSON file.
Source code in models/database.py
def load_database(self):
"""Loads a TinyDB object from a JSON file."""
try:
self.db = TinyDB(self.location)
except FileNotFoundError:
self.create_empty_database()
self.load_database()