Skip to content

Commit

Permalink
feat: enable YAML support for TinyDB
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNewThinkTank committed Jun 24, 2023
1 parent 703e474 commit 8bf84d3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

google_drive_data_path: "/Users/<USER>/Google Drive/My Drive/DATA/fitness-tracker-data"
# google_drive_data_path: "/Users/<USER>/Library/CloudStorage/GoogleDrive-<EMAIL>/My Drive/DATA/fitness-tracker-data"
real_workout_database: "<GOOGLE_DRIVE_DATA_PATH>/<ATHLETE>/db.json"
real_workout_database: "<GOOGLE_DRIVE_DATA_PATH>/<ATHLETE>/db.yml" # "<GOOGLE_DRIVE_DATA_PATH>/<ATHLETE>/db.json"
real_disciplines_database: "<GOOGLE_DRIVE_DATA_PATH>/<ATHLETE>/real_disciplines.json"
simulated_workout_database: "<GOOGLE_DRIVE_DATA_PATH>/sim_db.json"
simulated_workout_database: "<GOOGLE_DRIVE_DATA_PATH>/sim_db.yml" # "<GOOGLE_DRIVE_DATA_PATH>/sim_db.json"
real_weight_table: "weight_training_log"
real_disciplines_table: "disciplines_log"
simulated_weight_table: "weight_training_log"
Expand Down
27 changes: 27 additions & 0 deletions src/helpers/custom_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import yaml # type: ignore

from tinydb import TinyDB # type: ignore


class YAMLStorage(Storage):
def __init__(self, filename):
self.filename = filename

def read(self):
with open(self.filename) as handle:
try:
data = yaml.safe_load(handle.read())
return data
except yaml.YAMLError:
return None

def write(self, data):
with open(self.filename, 'w+') as handle:
yaml.dump(data, handle)

def close(self):
pass


if __name__ == "__main__":
db = TinyDB('db.yml', storage=YAMLStorage)
18 changes: 18 additions & 0 deletions src/helpers/json_to_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json
import yaml


def json_to_yaml(in_file: str) -> None:
"""Create YAML file from JSON file, without sorting keys alphabetically.
:param in_file: absolute path to JSON file
:type in_file: str
"""

with open(in_file, 'r') as rf, open(in_file.replace('json', 'yml'), "w") as wf:
yaml.dump(json.load(rf), wf, sort_keys=False)


if __name__ == "__main__":
in_file = "/Users/gustavcollinrasmussen/Library/CloudStorage/GoogleDrive-gcr84@hotmail.com/My Drive/DATA/fitness-tracker-data/gustav_rasmussen/log_archive/JSON/2023/June/test.json"
json_to_yaml(in_file)
12 changes: 8 additions & 4 deletions src/helpers/set_db_and_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from tinydb import TinyDB # type: ignore

from custom_storage import YAMLStorage


def set_db_and_table(
datatype,
Expand Down Expand Up @@ -56,14 +58,16 @@ def set_db_and_table(
DATA["real_workout_database"]
.replace("<ATHLETE>", athlete)
.replace("<USER>", user)
.replace("<EMAIL>", email)
.replace("<EMAIL>", email),
storage=YAMLStorage
)
if datatype == "real"
else TinyDB(
DATA["simulated_workout_database"],
sort_keys=True,
indent=4,
separators=(",", ": "),
storage=YAMLStorage
# sort_keys=True,
# indent=4,
# separators=(",", ": "),
)
)
table = (
Expand Down
28 changes: 14 additions & 14 deletions src/orchestration/real_flow.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Author: Gustav Collin Rasmussen
# Purpose: BASH workflow that inserts data in database.

WORKOUT_DATE=$(date +%F) # '2023-01-19' # 2022-03-02,2022-03-03
WORKOUT_DATE='2023-06-23' # $(date +%F) # '2023-01-19' # 2022-03-02,2022-03-03
# TRAINING_PROGRAM='nfp' # 'gvt'

# --workout_number 2
Expand All @@ -14,27 +14,27 @@ if ! python3 ./src/CRUD/insert.py --datatype real --dates "$WORKOUT_DATE"; then
exit 1
fi

echo "Data inserted in database. Preparing figures..."
# echo "Data inserted in database. Preparing figures..."

if ! python3 ./src/combined_metrics/combined_metrics.py; then
echo "Error: Failed to prepare figures."
exit 1
fi
# if ! python3 ./src/combined_metrics/combined_metrics.py; then
# echo "Error: Failed to prepare figures."
# exit 1
# fi

# python3 src/model/plot_model.py --datatype real --pgm $TRAINING_PROGRAM

open_images() {
open ./img/workout_frequency.png
open ./img/workout_duration.png
# open_images() {
# open ./img/workout_frequency.png
# open ./img/workout_duration.png
# open img/real_fitted_data_squat_${TRAINING_PROGRAM}.png
# open img/real_fitted_data_barbell_bench_press_${TRAINING_PROGRAM}.png
# open img/real_fitted_data_squat_splines.png
# open img/real_fitted_data_deadlift_splines.png
# open img/real_fitted_data_seated_row_splines.png
# open img/real_fitted_data_barbell_bench_press_splines.png
}
# }

if ! open_images; then
echo "Error: Failed to open images."
exit 1
fi
# if ! open_images; then
# echo "Error: Failed to open images."
# exit 1
# fi

0 comments on commit 8bf84d3

Please sign in to comment.