@@ -196,6 +196,9 @@ class bdict(dict):
196196 If an attempt is made to add a key or value that already exists in the
197197 dictionary a ValueError will be raised
198198
199+ Keys or values of "None", "True" and "False" will be stored internally as
200+ "_None" "_True" and "_False" respectively
201+
199202 Based on https://stackoverflow.com/a/1063393 by https://stackoverflow.com/users/9493/brian
200203 """
201204
@@ -214,10 +217,61 @@ def __setitem__(self, key, val):
214217 raise ValueError (f"The key '{ key } ' is already present in the dictionary" )
215218 if val in self and self [val ] != key :
216219 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+
218238 dict .__setitem__ (self , key , val )
219239 dict .__setitem__ (self , val , key )
220240
221241 def __delitem__ (self , key ):
222242 dict .__delitem__ (self , self [key ])
223243 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