Skip to content

Commit 8bf84d3

Browse files
feat: enable YAML support for TinyDB
1 parent 703e474 commit 8bf84d3

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
google_drive_data_path: "/Users/<USER>/Google Drive/My Drive/DATA/fitness-tracker-data"
44
# google_drive_data_path: "/Users/<USER>/Library/CloudStorage/GoogleDrive-<EMAIL>/My Drive/DATA/fitness-tracker-data"
5-
real_workout_database: "<GOOGLE_DRIVE_DATA_PATH>/<ATHLETE>/db.json"
5+
real_workout_database: "<GOOGLE_DRIVE_DATA_PATH>/<ATHLETE>/db.yml" # "<GOOGLE_DRIVE_DATA_PATH>/<ATHLETE>/db.json"
66
real_disciplines_database: "<GOOGLE_DRIVE_DATA_PATH>/<ATHLETE>/real_disciplines.json"
7-
simulated_workout_database: "<GOOGLE_DRIVE_DATA_PATH>/sim_db.json"
7+
simulated_workout_database: "<GOOGLE_DRIVE_DATA_PATH>/sim_db.yml" # "<GOOGLE_DRIVE_DATA_PATH>/sim_db.json"
88
real_weight_table: "weight_training_log"
99
real_disciplines_table: "disciplines_log"
1010
simulated_weight_table: "weight_training_log"

src/helpers/custom_storage.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import yaml # type: ignore
2+
3+
from tinydb import TinyDB # type: ignore
4+
5+
6+
class YAMLStorage(Storage):
7+
def __init__(self, filename):
8+
self.filename = filename
9+
10+
def read(self):
11+
with open(self.filename) as handle:
12+
try:
13+
data = yaml.safe_load(handle.read())
14+
return data
15+
except yaml.YAMLError:
16+
return None
17+
18+
def write(self, data):
19+
with open(self.filename, 'w+') as handle:
20+
yaml.dump(data, handle)
21+
22+
def close(self):
23+
pass
24+
25+
26+
if __name__ == "__main__":
27+
db = TinyDB('db.yml', storage=YAMLStorage)

src/helpers/json_to_yaml.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import json
2+
import yaml
3+
4+
5+
def json_to_yaml(in_file: str) -> None:
6+
"""Create YAML file from JSON file, without sorting keys alphabetically.
7+
8+
:param in_file: absolute path to JSON file
9+
:type in_file: str
10+
"""
11+
12+
with open(in_file, 'r') as rf, open(in_file.replace('json', 'yml'), "w") as wf:
13+
yaml.dump(json.load(rf), wf, sort_keys=False)
14+
15+
16+
if __name__ == "__main__":
17+
in_file = "/Users/gustavcollinrasmussen/Library/CloudStorage/[email protected]/My Drive/DATA/fitness-tracker-data/gustav_rasmussen/log_archive/JSON/2023/June/test.json"
18+
json_to_yaml(in_file)

src/helpers/set_db_and_table.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from tinydb import TinyDB # type: ignore
1414

15+
from custom_storage import YAMLStorage
16+
1517

1618
def set_db_and_table(
1719
datatype,
@@ -56,14 +58,16 @@ def set_db_and_table(
5658
DATA["real_workout_database"]
5759
.replace("<ATHLETE>", athlete)
5860
.replace("<USER>", user)
59-
.replace("<EMAIL>", email)
61+
.replace("<EMAIL>", email),
62+
storage=YAMLStorage
6063
)
6164
if datatype == "real"
6265
else TinyDB(
6366
DATA["simulated_workout_database"],
64-
sort_keys=True,
65-
indent=4,
66-
separators=(",", ": "),
67+
storage=YAMLStorage
68+
# sort_keys=True,
69+
# indent=4,
70+
# separators=(",", ": "),
6771
)
6872
)
6973
table = (

src/orchestration/real_flow.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Author: Gustav Collin Rasmussen
66
# Purpose: BASH workflow that inserts data in database.
77

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

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

17-
echo "Data inserted in database. Preparing figures..."
17+
# echo "Data inserted in database. Preparing figures..."
1818

19-
if ! python3 ./src/combined_metrics/combined_metrics.py; then
20-
echo "Error: Failed to prepare figures."
21-
exit 1
22-
fi
19+
# if ! python3 ./src/combined_metrics/combined_metrics.py; then
20+
# echo "Error: Failed to prepare figures."
21+
# exit 1
22+
# fi
2323

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

26-
open_images() {
27-
open ./img/workout_frequency.png
28-
open ./img/workout_duration.png
26+
# open_images() {
27+
# open ./img/workout_frequency.png
28+
# open ./img/workout_duration.png
2929
# open img/real_fitted_data_squat_${TRAINING_PROGRAM}.png
3030
# open img/real_fitted_data_barbell_bench_press_${TRAINING_PROGRAM}.png
3131
# open img/real_fitted_data_squat_splines.png
3232
# open img/real_fitted_data_deadlift_splines.png
3333
# open img/real_fitted_data_seated_row_splines.png
3434
# open img/real_fitted_data_barbell_bench_press_splines.png
35-
}
35+
# }
3636

37-
if ! open_images; then
38-
echo "Error: Failed to open images."
39-
exit 1
40-
fi
37+
# if ! open_images; then
38+
# echo "Error: Failed to open images."
39+
# exit 1
40+
# fi

0 commit comments

Comments
 (0)