コード例 #1
0
    def match_entry(self, data, key="title"):
        if isinstance(data, Media):
            data = data.as_json()
        db = JsonDatabase(self.name, self.db_path)
        # search by key/value pair
        movies = db.search_by_value(key, data[key])

        if len(movies):
            selected = movies[0]
            item_id = db.get_item_id(selected)
            if item_id >= 0:
                return selected, item_id
        return None, -1
コード例 #2
0
# keys do not need to be an exact match
users = db.search_by_key("birth", fuzzy=True)
for user, conf in users:
    print("matched with confidence", conf)
    print(user["name"], user["birthday"])

# search by key/value pair
users_12years_old = db.search_by_value("age", 12)

for user in users_12years_old:
    assert user["age"] == 12

# fuzzy key/value pair search
jon_users = db.search_by_value("name", "jon", fuzzy=True)
for user, conf in jon_users:
    print(user["name"])
    print("matched with confidence", conf)

# get database item
item = {"name": "bobby"}
item_id = db.get_item_id(item)

if item_id >= 0:
    db.update_item(item_id, {"name": "don't call me bobby"})
else:
    print("item not found in database")

# clear changes since last commit
db.reset()