-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable YAML support for TinyDB
- Loading branch information
1 parent
703e474
commit 8bf84d3
Showing
5 changed files
with
69 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/[email protected]/My Drive/DATA/fitness-tracker-data/gustav_rasmussen/log_archive/JSON/2023/June/test.json" | ||
json_to_yaml(in_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters