@@ -196,6 +196,9 @@ class bdict(dict):
196
196
If an attempt is made to add a key or value that already exists in the
197
197
dictionary a ValueError will be raised
198
198
199
+ Keys or values of "None", "True" and "False" will be stored internally as
200
+ "_None" "_True" and "_False" respectively
201
+
199
202
Based on https://stackoverflow.com/a/1063393 by https://stackoverflow.com/users/9493/brian
200
203
"""
201
204
@@ -214,10 +217,61 @@ def __setitem__(self, key, val):
214
217
raise ValueError (f"The key '{ key } ' is already present in the dictionary" )
215
218
if val in self and self [val ] != key :
216
219
raise ValueError (f"The key '{ val } ' is already present in the dictionary" )
217
-
220
+
221
+ if key is None :
222
+ key = "_None"
223
+ if val is None :
224
+ val = "_None"
225
+
226
+ if isinstance (key , bool ):
227
+ if key :
228
+ key = "_True"
229
+ else :
230
+ key = "_False"
231
+
232
+ if isinstance (val , bool ):
233
+ if val :
234
+ val = "_True"
235
+ else :
236
+ val = "_False"
237
+
218
238
dict .__setitem__ (self , key , val )
219
239
dict .__setitem__ (self , val , key )
220
240
221
241
def __delitem__ (self , key ):
222
242
dict .__delitem__ (self , self [key ])
223
243
dict .__delitem__ (self , key )
244
+
245
+ def __getitem__ (self , key ):
246
+ if key is None :
247
+ key = "_None"
248
+
249
+ if isinstance (key , bool ):
250
+ if key :
251
+ key = "_True"
252
+ else :
253
+ key = "_False"
254
+
255
+ val = dict .__getitem__ (self , key )
256
+
257
+ if val == "_None" :
258
+ return None
259
+ elif val == "_True" :
260
+ return True
261
+ elif val == "_False" :
262
+ return False
263
+ else :
264
+ return val
265
+
266
+ def __contains__ (self , key ):
267
+ print ("Contains" )
268
+ if key is None :
269
+ key = "_None"
270
+
271
+ if isinstance (key , bool ):
272
+ if key :
273
+ key = "_True"
274
+ else :
275
+ key = "_False"
276
+
277
+ return dict .__contains__ (self , key )
0 commit comments