Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def is_dogmatic(a):
if isinstance(a, (DogmaticDict, DogmaticList)):
return True
elif isinstance(a, dict):
return any(is_dogmatic(v) for v in a.values())
elif isinstance(a, (list, tuple)):
return any(is_dogmatic(v) for v in a)
def test_isinstance_of_dict():
assert isinstance(DogmaticDict(), dict)
def test_fixed_value_not_initialized():
d = DogmaticDict({"a": 7})
assert "a" not in d
def test_fallback():
d = DogmaticDict(fallback={"a": 23})
assert "a" in d
assert d["a"] == 23
assert d.get("a") == 23
d = DogmaticDict()
d.fallback = {"a": 23}
assert "a" in d
assert d["a"] == 23
assert d.get("a") == 23
def test_fixed_value_fixed():
d = DogmaticDict({"a": 7})
d["a"] = 8
assert d["a"] == 7
del d["a"]
assert "a" in d
assert d["a"] == 7
d.update([("a", 9), ("b", 12)])
assert d["a"] == 7
d.update({"a": 9, "b": 12})
assert d["a"] == 7
d.update(a=10, b=13)
assert d["a"] == 7
def test_dict_interface_update_with_list_of_items():
d = DogmaticDict()
d["a"] = 12
d["b"] = "foo"
d.update([("b", 9), ("c", 7)])
assert d["a"] == 12
assert d["b"] == 9
assert d["c"] == 7
def test_dict_interface_set_item():
d = DogmaticDict()
d["a"] = 12
d["b"] = "foo"
assert "a" in d
assert "b" in d
assert d["a"] == 12
assert d["b"] == "foo"
assert set(d.keys()) == {"a", "b"}
assert set(d.values()) == {12, "foo"}
assert set(d.items()) == {("a", 12), ("b", "foo")}
def undogmatize(obj):
if isinstance(obj, DogmaticDict):
return dict({key: undogmatize(value) for key, value in obj.items()})
elif isinstance(obj, DogmaticList):
return list([undogmatize(value) for value in obj])
elif isinstance(obj, tuple):
return tuple(undogmatize(value) for value in obj)
else:
return obj
def dogmatize(obj):
if isinstance(obj, dict):
return DogmaticDict({key: dogmatize(val) for key, val in obj.items()})
elif isinstance(obj, list):
return DogmaticList([dogmatize(value) for value in obj])
elif isinstance(obj, tuple):
return tuple(dogmatize(value) for value in obj)
else:
return obj