Team named tuple, sheduler and refreshScores implementation with singleton Teams data

This commit is contained in:
Xevion
2020-06-19 17:21:44 -05:00
parent 6dd2aca80c
commit 4bc5d7e72e
2 changed files with 20 additions and 9 deletions

View File

@@ -1,10 +1,21 @@
"""
__init__.py
"""
from collections import namedtuple
from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask
# initialize Flask object
app = Flask(__name__)
from trivia import routes, api
# Simple fake 'class' for passing to jinja templates
Team = namedtuple('Team', ['id', 'name', 'scores'])
from trivia import routes, api, utils
# Setup a scheduler for automatically refreshing data
scheduler = BackgroundScheduler()
scheduler.add_job(func=utils.refreshScores, trigger="interval", seconds=5)
utils.refreshScores()

View File

@@ -3,20 +3,20 @@ utils.py
Stores important backend application functionality.
"""
import os
import json
import os
from typing import List
from trivia import Team
# Generate paths
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(BASE_DIR, 'data')
data: List[Team] = None
# data: List[Team] = []
teams = []
def refresh() -> None:
def refreshScores() -> None:
"""
Refreshes scores data safely.
@@ -31,14 +31,14 @@ def refresh() -> None:
# Place all values into Team object for jinja
temp = [
Team(
rank=-1,
id=team['teamno'],
name=team['teamname'],
scores=team['scores']
) for team in temp
]
print(f'Successfully loaded ({len(temp)} teams).')
data = temp
global teams
teams = temp
# If invalid or inaccessible, simply do nothing.
except json.JSONDecodeError:
print('Scores file could not be opened or parsed.')
print('Scores file could not be opened or parsed.')