@@ -90,41 +90,38 @@ Create a file
9090----------------------------------------------------------------------------------------
9191This library is called DictDataBase, but you can actually use any json serializable object.
9292``` python
93- user_data_dict = {
94- " users" : {
95- " Ben" : { " age" : 30 , " job" : " Software Engineer" },
96- " Sue" : { " age" : 21 , " job" : " Architect" },
97- " Joe" : { " age" : 50 , " job" : " Manager" }
98- },
99- " follows" : [[" Ben" , " Sue" ], [" Joe" , " Ben" ]]
93+ users_dict = {
94+ " u1" : { " name" : " " Ben, " age" : 30 , " job" : " Software Engineer" },
95+ " u2" : { " name" : " Sue" , " age" : 21 , " job" : " Architect" },
96+ " u3" : { " name" : " Joe" , " age" : 50 , " job" : " Manager" },
10097}
10198DDB .at(" users" ).create(user_data_dict)
102-
103- # There is now a file called users.json (or user_data.ddb if you use compression)
104- # in your specified storage directory.
10599```
100+ There is now a file called ` users.json ` or ` users.ddb ` in your specified storage
101+ directory depending on if you use compression.
102+
106103
107104Check if file or sub-key exists
108105----------------------------------------------------------------------------------------
109106``` python
110- DDB .at(" users" ).exists() # True
111- DDB .at(" users" , key = " none" ).exists() # False
112- # Also works on nested keys
113- DDB .at(" users" , key = " Ben" ).exists() # True
114- DDB .at(" users" , key = " Sam" ).exists() # False
107+ DDB .at(" users" ).exists()
108+ >> > True # File exists
109+ DDB .at(" users" , key = " u10" ).exists()
110+ >> > False # Key "u10" not in users
111+ DDB .at(" users" , key = " u2" ).exists()
112+ >> > True
115113```
116114
117115Read dicts
118116----------------------------------------------------------------------------------------
119117
120118``` python
121119d = DDB .at(" users" ).read()
122- # You now have a copy of the json file named "users"
123- d == user_data_dict # True
120+ d == users_dict # True
124121
125122# Only partially read Joe
126- joe = DDB .at(" users" , key = " Joe " ).read()
127- joe == user_data_dict [" Joe" ] # True
123+ joe = DDB .at(" users" , key = " u3 " ).read()
124+ joe == users_dict [" Joe" ] # True
128125```
129126
130127> Note: Doing a partial read like with ` DDB.at("users", key="Joe").read() ` will return
@@ -139,24 +136,19 @@ DDB.at("numbers").create({"a", 1, "b", 2, "c": 3})
139136above_1 = DDB .at(" numbers" , where = lambda k , v : v > 1 ).read()
140137>> > above_1 == {" b" , 2 , " c" : 3 }
141138```
139+ > The ` where ` callback is a function that takes two parameters, the key and the value.
140+
142141
143142Write dicts
144143----------------------------------------------------------------------------------------
145144
146145``` python
147146with DDB .at(" users" ).session() as (session, users):
148- # You now have a copy of the json file users as the variable users
149- # Inside the with statement, the file of user_data will be locked, and no other
150- # processes will be able to interfere.
151- users[" follows" ].append([" Sue" , " Ben" ])
152- session.write()
153- # session.write() must be called to save the changes!
154- print (DDB .at(" user_data" ).read()[" follows" ])
155- >> > [[" Ben" , " Sue" ], [" Joe" , " Ben" ], [" Sue" , " Ben" ]]
147+ users[" u3" ][" age" ] = 99
148+ print (DDB .at(" users" , key = " u3" ).read()[" age])
149+ >> > 99
156150```
157-
158- If you do not call session.write(), changes will not be written to disk!
159-
151+ > If you do not call session.write(), changes will not be written to disk!
160152
161153Partial writing
162154----------------------------------------------------------------------------------------
0 commit comments