I need to verify if another dict is a subset of another dict, there is a trick that in these dicts there are array of dict's.
superset:
dct_1 = {
'x': 'x',
'y': [
{
't': '123',
'a': 'a'
}
]
}
subset:
dict_2 = {
'x': 'x',
'y': [
{
't': '123'
}
]
}
from Recursive function to check dictionary is a subset of another dictionary answer I get this error:
TypeError: unhashable type: 'dict'
My code:
def is_subset(superset, subset):
for key, value in subset.items():
if key not in superset:
return False
if isinstance(value, dict):
if not is_subset(superset[key], value):
return False
elif isinstance(value, str):
if value not in superset[key]:
return False
elif isinstance(value, list):
if not set(value) <= set(superset[key]):
return False
elif isinstance(value, set):
if not value <= superset[key]:
return False
else:
if not value == superset[key]:
return False
return True
class Test(unittest.TestCase):
def setUp(self):
self.dct = {
'x': 'x',
'y': [
{
't': '123',
'a': 'a'
}
]
}
def check_is_subset_true(self, superset, subset):
return self.assertEqual(is_subset(superset, subset), True)
def test_is_true(self):
self.check_is_subset_true(self.dct, {
'x': 'x',
'y': [
{
't': '123'
}
]
})