jeudi 29 mai 2014

python - traitant des multiples formes de données (dict et dict avec listes) - Stack Overflow


I have a situation where I can have data in the following form:


data = {
'multiple': True,
'data1': 'some_data',
'repeats': [
{'data1':{'section1': 'some_data', 'section2': 'some_more_data'}}
{'data2':{'section1': 'some_data', 'section2': 'some_more_data'}}
]
}

and sometimes I will have this:


data = {
'multiple': False,
'data1': 'some_data',
'section1a': 'some_data',
'section2a': 'some_more_data'}
}

I need to validate the data in sectionxy. Right now, I have a validate method that accepts data. In it, I am looking at 'multiple'. If it is multiple, I am doing something like this:


if multiple:
for info in data['repeats']:
validate_single(info.values()[0])
else:
validate_single(data)

where validate_single looks at the value of section1 and section2


Is there a better approach to this? Is there some sort of way to flatten the first data, or expand the second data that would make more sense than what I am trying?



I have a situation where I can have data in the following form:


data = {
'multiple': True,
'data1': 'some_data',
'repeats': [
{'data1':{'section1': 'some_data', 'section2': 'some_more_data'}}
{'data2':{'section1': 'some_data', 'section2': 'some_more_data'}}
]
}

and sometimes I will have this:


data = {
'multiple': False,
'data1': 'some_data',
'section1a': 'some_data',
'section2a': 'some_more_data'}
}

I need to validate the data in sectionxy. Right now, I have a validate method that accepts data. In it, I am looking at 'multiple'. If it is multiple, I am doing something like this:


if multiple:
for info in data['repeats']:
validate_single(info.values()[0])
else:
validate_single(data)

where validate_single looks at the value of section1 and section2


Is there a better approach to this? Is there some sort of way to flatten the first data, or expand the second data that would make more sense than what I am trying?


0 commentaires:

Enregistrer un commentaire