I have a function general_statistics
that takes a dict as input and get the wanted information. the function is working without any problem, but I want to know if I can do it with a pythonic way because I'm trying to improve my skills.
def general_statistics(input_data: dict):
result = {}
if 'general' in input_data.keys():
if bool(input_data['general']):
result['name'] = input_data['general']['name']
result['information'] = input_data['general']['information']
result['date'] = input_data['general']['creation_date']
result['adress'] = input_data['general']['adress']
if 'two' in input_data['general']['photo'].keys():
result['picture_person'] = input_data['general']['photo']['two']['ids']
else:
result['picture_person'] = input_data['general']['photo']['one']['ids']
if 'phones' in input_data['general'].keys():
result['phones'] = input_data['general']['phones']
else:
pass
else:
result['name'] = ''
result['information'] = ''
result['date'] = ''
result['adress'] = ''
else:
result['name'] = ''
result['information'] = ''
result['date'] = ''
result['adress'] = ''
if 'intrest' in input_data.keys():
if bool(input_data['intrest']):
intrest_list = []
for gender in input_data['intrest']['gender_type']:
intrest_list.append(gender.split('/')[-1])
result['genders'] = intrest_list
else:
result['genders'] = []
if 'command_details' in input_data.keys():
if bool(input_data['command_details']):
result['shiping'] = input_data['command_details']['shiping']
else:
result['shiping'] = ''
if 'command_statistics' in input_data.keys():
if bool(input_data['command_statistics']):
result['com_statistics'] = input_data['command_statistics']
else:
command_statistics = {}
command_statistics['count'] = ''
command_statistics['items'] = ''
command_statistics['payed'] = ''
command_statistics['unpayed'] = ''
result['com_statistics'] = command_statistics
else:
command_statistics = {}
command_statistics['count'] = ''
command_statistics['items'] = ''
command_statistics['payed'] = ''
command_statistics['unpayed'] = ''
result['com_statistics'] = command_statistics
result['com_statistics'] = input_data['command_statistics']
if 'receiver_info' in input_data.keys():
if bool(input_data['receiver_info']):
if 'general' in input_data['receiver_info'].keys():
result['receiver_date'] = input_data['receiver_info']['general']['creation_date']
result['receiver_info'] = input_data['receiver_info']['general']['information']
if 'adress' in input_data['receiver_info']['general'].keys():
result['receiver_adress'] = input_data['receiver_info']['general']['adress']
else:
result['receiver_adress'] = ''
if 'two' in input_data['receiver_info']['general']['photo'].keys():
result['receiver_pictures'] = input_data['receiver_info']['general']['photo']['two']['ids']
else:
result['receiver_pictures'] = input_data['receiver_info']['general']['photo']['one']['ids']
result['receiver_stats'] = input_data['receiver_info']['command_statistics']
receiver_commands = []
if 'intrest' in input_data['receiver_info'].keys():
if input_data['receiver_info']['intrest']:
for gender in input_data['receiver_info']['intrest']['gender_type']:
receiver_commands.append(
gender.split('/')[-1])
result['receiver_genders'] = receiver_commands
if 'receiver_photo' in input_data['receiver_info']['receiver_brand'].keys():
result['receiver_brand_id_photo'] = input_data[
'receiver_info']['receiver_brand']['receiver_photo']['id']
else:
result['receiver_brand_id_photo'] = ''
else:
result['receiver_date'] = ''
result['receiver_adress'] = ''
result['receiver_stats'] = {}
result['receiver_pictures'] = ''
else:
result['receiver_date'] = ''
result['receiver_adress'] = ''
result['receiver_stats'] = {}
result['receiver_pictures'] = ''
else:
result['receiver_date'] = ''
result['receiver_adress'] = ''
result['receiver_stats'] = {}
result['receiver_pictures'] = ''
return result
A small example of the input_data
input_data={
"general": {
"creation_date": "10/10/2015",
"first_id": "10kahd",
"name": "jacky",
"information": "",
"photo": {
"one": {
"ids": "1598",
},
"two": {
"ids": "1489"
},
"tree": {
"ids": "0298"
}
},
"adress": "10,new york 159",
"phones": [
"number 1",
"number 2"
],
"cat": "01",
},
"command_details": {
"shiping": "allowed",
},
"command_statistics": {
"count": "226558",
"items": "2015",
"payed": "1500",
"unpayed": "0"
},
"intrest": {
"gender_type": [
"chairs"
]
},
"cmd_id": "alsd952d",
"receiver_info": {
"general": {
"name": "10,new york 159",
"information": "",
"creation_date": "01/06/2012",
"photo": {
"one": {
"ids": "159z"
},
"two": {
"ids": "98zzf"
},
"tree": {
"ids": "pokAcs6"
}
},
},
"command_statistics": {
"count": "15987643325",
"all_cmd_count": "16549878466245",
"all_cmd_count_years": 125,
"sub_cmd": "1664987322"
},
"intrest": {
"gender_ids": [
"qsmplos"
],
"gender_type": [
"chairs",
"pens"
]
},
"receiver_brand": {
"brand_abdress": {
"adress": "10,new york 159"
}
},
}
}