我在本地创建了一个 TinyDB(版本 4.5.2,文档:https : //tinydb.readthedocs.io/en/latest/usage.html)数据库(macbook air,运行 macOS BigSur,v. 11.5.2)db.json
,带有一个名为 的表log
,使用 Python 3.9.6 在一些重量训练日志数据之上构建一个小型应用程序。目前我正在尝试在日志表中查询练习名称为: 的数据squat
。使用该方法时获取所有练习any
,或者使用该方法时获取一个空列表all
。中的数据db.json
具有以下结构(仅显示 2 个练习):
{
"log": {
"1": {
"date": "2021-12-11",
"split": "legday",
"exercises": [
{
"name": "squat",
"sets": [
{ "set no.": 1, "reps": 8, "weight": "70 kg" },
{ "set no.": 2, "reps": 8, "weight": "80 kg" },
{ "set no.": 3, "reps": 5, "weight": "90 kg" },
{ "set no.": 4, "reps": 5, "weight": "90 kg" }
]
},
{
"name": "deadlift",
"sets": [
{ "set no.": 1, "reps": 8, "weight": "70 kg" },
{ "set no.": 2, "reps": 8, "weight": "80 kg" },
{ "set no.": 3, "reps": 5, "weight": "90 kg" },
{ "set no.": 4, "reps": 5, "weight": "90 kg" }
]
}
]
}
}
}
到目前为止我已经尝试过:
"""
Date: 2021-12-11
Author: Gustav Collin Rasmussen
Purpose: Store and analyze weight-training data
"""
import json
from tinydb import Query, TinyDB
def insert_data(db, log):
"""Store training log in database"""
log_path = "training_log.json"
with open(log_path) as rf:
json_content = json.load(rf)
log.insert(json_content)
def describe_workout(db):
"""Simple summary statistics for each exercise"""
for item in db:
for exercise in item["exercises"]:
print(f"exercise: {exercise['name']}")
print(f"number of sets: {len(exercise['sets'])}\n")
def analyze_workout(db, log):
"""Deeper analysis of workout"""
Log = Query()
Exercise = Query()
# print(log.search(Exercise.date == "2021-12-11"))
print(log.search(Log.exercises.any(Exercise.name == "squat"))) # <---------- RETURNS ALL DATA !
# print(db.search(Exercise.exercises.all(Exercise.name == "squat")))
print(log.search(Log.exercises.all(Exercise.name == "squat"))) # <---------- RETURNS EMPTY LIST !
def main():
db = TinyDB("db.json")
log = db.table("log")
# insert_data(db, log)
# describe_workout(db)
analyze_workout(db, log) # <-------- CALLING THE QUERIES FROM HERE !
if __name__ == "__main__":
main()
查询嵌套 JSON 的官方示例非常相似,来自https://tinydb.readthedocs.io/en/latest/usage.html:
>>> Group = Query()
>>> Permission = Query()
>>> groups = db.table('groups')
>>> groups.insert({
'name': 'user',
'permissions': [{'type': 'read'}]})
>>> groups.insert({
'name': 'sudo',
'permissions': [{'type': 'read'}, {'type': 'sudo'}]})
>>> groups.insert({
'name': 'admin',
'permissions': [{'type': 'read'}, {'type': 'write'}, {'type': 'sudo'}]})
Now let’s search this table using nested any/all queries:
>>> # Group has a permission with type 'read'
>>> groups.search(Group.permissions.any(Permission.type == 'read'))
[{'name': 'user', 'permissions': [{'type': 'read'}]},
{'name': 'sudo', 'permissions': [{'type': 'read'}, {'type': 'sudo'}]},
{'name': 'admin', 'permissions':
[{'type': 'read'}, {'type': 'write'}, {'type': 'sudo'}]}]
>>> # Group has ONLY permission 'read'
>>> groups.search(Group.permissions.all(Permission.type == 'read'))
[{'name': 'user', 'permissions': [{'type': 'read'}]}]
所以我对不同的行为感到困惑。你能看到这里缺少的链接吗?
(还查看了关于 SO 的类似问题,Is it possible to query nested json object using TinyDB without lucky)