Esempio n. 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
    "users", optional_file_path)  # loaded automatically from previous step
users_with_defined_age = db.search_by_key("age")

assert len(users_with_defined_age) == 2

for user in users_with_defined_age:
    print(user["name"], user["age"])

# 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:
        self.email = email
        self.secret_key = key
        self.data = data

    def __repr__(self):
        return "User:"******"*****@*****.**", data={"name": "jonas", "birthday": "12 May"})
user2 = User("*****@*****.**",
             "secret",
             data={
                 "name": ["joe", "jony"],
                 "age": 12
             })

# objects will be jsonified here, they will no longer be User objects
# if you need them to be a specific class use some ORM lib instead (SQLAlchemy is great)
db.add_item(user1)
db.add_item(user2)

# search entries with non empty key
print(db.search_by_key("secret_key"))

# search in user provided data
print(db.search_by_key("birth", fuzzy=True))

# search entries with a certain value
print(db.search_by_value("age", 12))
print(db.search_by_value("name", "jon", fuzzy=True))