json-database 0.8.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- json_database/__init__.py +387 -0
- json_database/crypto.py +80 -0
- json_database/exceptions.py +22 -0
- json_database/search.py +169 -0
- json_database/utils.py +340 -0
- json_database/version.py +6 -0
- json_database/xdg_utils.py +174 -0
- json_database-0.8.0a1.dist-info/LICENSE +19 -0
- json_database-0.8.0a1.dist-info/METADATA +205 -0
- json_database-0.8.0a1.dist-info/RECORD +12 -0
- json_database-0.8.0a1.dist-info/WHEEL +5 -0
- json_database-0.8.0a1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: json-database
|
|
3
|
+
Version: 0.8.0a1
|
|
4
|
+
Summary: searchable json database with persistence
|
|
5
|
+
Home-page: https://github.com/TigreGotico/json_database
|
|
6
|
+
Author: jarbasAI
|
|
7
|
+
Author-email: jarbasai@mailfence.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
Requires-Dist: combo-lock<1.0.0,>=0.2.1
|
|
12
|
+
|
|
13
|
+
# Json Database
|
|
14
|
+
|
|
15
|
+
Python dict based database with persistence and search capabilities
|
|
16
|
+
|
|
17
|
+
For those times when you need something simple and sql is overkill
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
- pure python
|
|
23
|
+
- save and load from file
|
|
24
|
+
- search recursively by key and key/value pairs
|
|
25
|
+
- fuzzy search
|
|
26
|
+
- supports arbitrary objects
|
|
27
|
+
- supports comments in saved files
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install json_database
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### JsonStorage
|
|
39
|
+
|
|
40
|
+
Sometimes you need persistent dicts that you can save and load from file
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from json_database import JsonStorage
|
|
44
|
+
from os.path import exists
|
|
45
|
+
|
|
46
|
+
save_path = "my_dict.conf"
|
|
47
|
+
|
|
48
|
+
my_config = JsonStorage(save_path)
|
|
49
|
+
|
|
50
|
+
my_config["lang"] = "pt"
|
|
51
|
+
my_config["secondary_lang"] = "en"
|
|
52
|
+
my_config["email"] = "jarbasai@mailfence.com"
|
|
53
|
+
|
|
54
|
+
# my_config is a python dict
|
|
55
|
+
assert isinstance(my_config, dict)
|
|
56
|
+
|
|
57
|
+
# save to file
|
|
58
|
+
my_config.store()
|
|
59
|
+
|
|
60
|
+
my_config["lang"] = "pt-pt"
|
|
61
|
+
|
|
62
|
+
# revert to previous saved file
|
|
63
|
+
my_config.reload()
|
|
64
|
+
assert my_config["lang"] == "pt"
|
|
65
|
+
|
|
66
|
+
# clear all fields
|
|
67
|
+
my_config.clear()
|
|
68
|
+
assert my_config == {}
|
|
69
|
+
|
|
70
|
+
# load from a specific path
|
|
71
|
+
my_config.load_local(save_path)
|
|
72
|
+
assert my_config == JsonStorage(save_path)
|
|
73
|
+
|
|
74
|
+
# delete stored file
|
|
75
|
+
my_config.remove()
|
|
76
|
+
assert not exists(save_path)
|
|
77
|
+
|
|
78
|
+
# keep working with dict in memory
|
|
79
|
+
print(my_config)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### JsonDatabase
|
|
83
|
+
|
|
84
|
+
Ever wanted to search a dict?
|
|
85
|
+
|
|
86
|
+
Let's create a dummy database with users
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from json_database import JsonDatabase
|
|
90
|
+
|
|
91
|
+
db_path = "users.db"
|
|
92
|
+
|
|
93
|
+
with JsonDatabase("users", db_path) as db:
|
|
94
|
+
# add some users to the database
|
|
95
|
+
|
|
96
|
+
for user in [
|
|
97
|
+
{"name": "bob", "age": 12},
|
|
98
|
+
{"name": "bobby"},
|
|
99
|
+
{"name": ["joe", "jony"]},
|
|
100
|
+
{"name": "john"},
|
|
101
|
+
{"name": "jones", "age": 35},
|
|
102
|
+
{"name": "joey", "birthday": "may 12"}]:
|
|
103
|
+
db.add_item(user)
|
|
104
|
+
|
|
105
|
+
# pretty print database contents
|
|
106
|
+
db.print()
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
# auto saved when used with context manager
|
|
110
|
+
# db.commit()
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
search entries by key
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from json_database import JsonDatabase
|
|
119
|
+
|
|
120
|
+
db_path = "users.db"
|
|
121
|
+
|
|
122
|
+
db = JsonDatabase("users", db_path) # load db created in previous example
|
|
123
|
+
|
|
124
|
+
# search by exact key match
|
|
125
|
+
users_with_defined_age = db.search_by_key("age")
|
|
126
|
+
|
|
127
|
+
for user in users_with_defined_age:
|
|
128
|
+
print(user["name"], user["age"])
|
|
129
|
+
|
|
130
|
+
# fuzzy search
|
|
131
|
+
users = db.search_by_key("birth", fuzzy=True)
|
|
132
|
+
for user, conf in users:
|
|
133
|
+
print("matched with confidence", conf)
|
|
134
|
+
print(user["name"], user["birthday"])
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
search by key value pair
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
# search by key/value pair
|
|
141
|
+
users_12years_old = db.search_by_value("age", 12)
|
|
142
|
+
|
|
143
|
+
for user in users_12years_old:
|
|
144
|
+
assert user["age"] == 12
|
|
145
|
+
|
|
146
|
+
# fuzzy search
|
|
147
|
+
jon_users = db.search_by_value("name", "jon", fuzzy=True)
|
|
148
|
+
for user, conf in jon_users:
|
|
149
|
+
print(user["name"])
|
|
150
|
+
print("matched with confidence", conf)
|
|
151
|
+
# NOTE that one of the users has a list instead of a string in the name, it also matches
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
updating an existing entry
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
# get database item
|
|
158
|
+
item = {"name": "bobby"}
|
|
159
|
+
|
|
160
|
+
item_id = db.get_item_id(item)
|
|
161
|
+
|
|
162
|
+
if item_id >= 0:
|
|
163
|
+
new_item = {"name": "don't call me bobby"}
|
|
164
|
+
db.update_item(item_id, new_item)
|
|
165
|
+
else:
|
|
166
|
+
print("item not found in database")
|
|
167
|
+
|
|
168
|
+
# clear changes since last commit
|
|
169
|
+
db.reset()
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
You can save arbitrary objects to the database
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
from json_database import JsonDatabase
|
|
176
|
+
|
|
177
|
+
db = JsonDatabase("users", "~/databases/users.json")
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
class User:
|
|
181
|
+
def __init__(self, email, key=None, data=None):
|
|
182
|
+
self.email = email
|
|
183
|
+
self.secret_key = key
|
|
184
|
+
self.data = data
|
|
185
|
+
|
|
186
|
+
user1 = User("first@mail.net", data={"name": "jonas", "birthday": "12 May"})
|
|
187
|
+
user2 = User("second@mail.net", "secret", data={"name": ["joe", "jony"], "age": 12})
|
|
188
|
+
|
|
189
|
+
# objects will be "jsonified" here, they will no longer be User objects
|
|
190
|
+
# if you need them to be a specific class use some ORM lib instead (SQLAlchemy is great)
|
|
191
|
+
db.add_item(user1)
|
|
192
|
+
db.add_item(user2)
|
|
193
|
+
|
|
194
|
+
# search entries with non empty key
|
|
195
|
+
print(db.search_by_key("secret_key"))
|
|
196
|
+
|
|
197
|
+
# search in user provided data
|
|
198
|
+
print(db.search_by_key("birth", fuzzy=True))
|
|
199
|
+
|
|
200
|
+
# search entries with a certain value
|
|
201
|
+
print(db.search_by_value("age", 12))
|
|
202
|
+
print(db.search_by_value("name", "jon", fuzzy=True))
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
json_database/__init__.py,sha256=P0qG3TU5iX0FRhZibTG6mQn93DtqojwhOYdUCfjGWnM,12395
|
|
2
|
+
json_database/crypto.py,sha256=YAQw0mGJKC5sXX1_8UHZ8Ll0Akr7YA2d4CCMsXjvu1g,2346
|
|
3
|
+
json_database/exceptions.py,sha256=scp9y-ZNAzCLnCwYoioOxy0C-Mk95tOlIa5nfSqQO0A,458
|
|
4
|
+
json_database/search.py,sha256=RM4f4Qj-B8gUXIHtdCyQ33XrK_IHYFtgW5XQZDrpVDU,6577
|
|
5
|
+
json_database/utils.py,sha256=jEQftnXu5ZiC--goaQiwLQ3LdhYTFB89B3v2Pnt4sO8,10656
|
|
6
|
+
json_database/version.py,sha256=LcHxD83rEw6t1fjIIETn2QTvmXEIijkX0zzOQfHIISo,114
|
|
7
|
+
json_database/xdg_utils.py,sha256=UnhHXn4ZFUa7gGvc7IYlqH8XTVCJjs4fsPX9S2XoM40,5585
|
|
8
|
+
json_database-0.8.0a1.dist-info/LICENSE,sha256=548oOCrCNqQty8Fsqqry522lXdxnBTqLyzqr38ct-cw,1040
|
|
9
|
+
json_database-0.8.0a1.dist-info/METADATA,sha256=poxYqQH-D1XLHcvmI1f9iWRKmuGL27PHTfoEiyFOzVs,4396
|
|
10
|
+
json_database-0.8.0a1.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
|
11
|
+
json_database-0.8.0a1.dist-info/top_level.txt,sha256=QfyxfZG3hW-n-2FJHzHchBz5GZ37cB92QDOuF1t51X0,14
|
|
12
|
+
json_database-0.8.0a1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
json_database
|