Skip to content

Commit 1e1c92b

Browse files
committed
Updated bdict to allow for True, None and False keys/values
1 parent cc04fb7 commit 1e1c92b

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

domdf_python_tools/utils.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)

tests/test_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,30 @@ def test_bdict_errors():
113113
new_dict["Value1"] = 1234
114114
new_dict["Key1"] = "Value1"
115115
new_dict["Value1"] = "Key1"
116+
117+
118+
def test_bdict_bool():
119+
new_dict = bdict(N=None, T=True, F=False)
120+
121+
print(new_dict)
122+
123+
assert None in new_dict
124+
assert True in new_dict
125+
assert True in new_dict
126+
127+
assert isinstance(new_dict["T"], bool) and new_dict["T"]
128+
assert isinstance(new_dict["F"], bool) and not new_dict["F"]
129+
130+
assert new_dict[True] == "T"
131+
assert new_dict[False] == "F"
132+
assert new_dict[None] == "N"
133+
134+
assert "_None" in new_dict
135+
assert new_dict["_None"] == "N"
136+
137+
# Test for pyMHDAC
138+
new_dict_2 = bdict(Unspecified=0, _None=1, GC=2, LC=3, CE=4)
139+
140+
assert None in new_dict_2
141+
142+
assert new_dict_2[None] == 1

0 commit comments

Comments
 (0)