from json_database import JsonDatabase from os.path import isfile optional_file_path = "users.db" if not isfile(optional_file_path): print("run database_create.py first!") exit() db = JsonDatabase( "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
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))