Python match nested structure

Viewed 71

Im trying to define some logic, that verifies everything in one nested dictionary belongs to another nested nested dictionary.

Ie:

official_data = {
    'Name': 'John Smith',
    'ID': 123123232,
    'Family': [
        {'Name': 'Sarah Smith','ID': 12312323},
        {'Name': 'Joe Smith','ID': 12312324}
        {'Name': 'Tim Smith','ID': 12312325}
        {'Name': 'Sally Smith','ID': 12312326}
    ],
    'Info': {
        'InfoList': [
            {'text': ['Personal Info Message']},
            {'text': ['Secondary Message']}
        ]
    }
}

sample_data = {
    'Family': [
        {"Name": 'Joe Smith'}
    ],
    'Info': {
        'InfoList': [
            {'text': ['Secondary Message']}
        ]
    }
}

matches(official_data, sample_data) # True, because everything in sample data exists in official_data, despite official_data having MORE values.

different_sample = {
    'Info': {
        'InfoList': [{}]
    }
}
matches(official_data, different_sample) # True, because the structure of Dict -> Dict -> List -> Dict exists

bad_data = {'ID': 54242343}
matches(official_data, bad_data) # False, because the ID of bad_data is not the ID of official_data

other_bad_data = {
    'Info': {
        'InfoList': {}
    }
}
matches(official_data, other_bad_data) # False, because InfoList is a list in official data

I have a feeling such logic SHOULD be easy to implement, or has already been implemented and is in wide use, but I am struggling to find what i want, and implementing it on my own becomes complicated, with recursive solutions and casting lists into sets in order to make sure order is ignored.

Im wondering if im missing something obvious, or if this logic is actually really niche and would have to be designed from scratch.

0 Answers
Related