Assign dict values to list if they exist, otherwise none
Assign dict values to list if they exist, otherwise none
Right now, i have something like that:
media_info = MediaInfo.parse(file_path).to_data()
local_file_info = [ media_info['tracks'][1]['frame_rate'],
media_info['tracks'][1]['other_display_aspect_ratio'],
media_info['tracks'][2]['other_sampling_rate'],
media_info['tracks'][2]['other_channel_s'],
media_info['tracks'][3]['Format']]
How can i dumbproof this to assign None instead of throwing IndexError when element doesn't exist? (Example: file parsed with MediaInfo doesn't have subtitle track in video file, so there is no 3rd track)
I know i could do it the dirty way:
try:
local_file_info.append(media_info['tracks'][1]['frame_rate'])
except IndexError
local_file_info.append(None)
try:
local_file_info.append(media_info['tracks'][1]['other_display_aspect_ratio'])
except IndexError
local_file_info.append(None)
try:
local_file_info.append(media_info['tracks'][2]['other_sampling_rate'])
except IndexError
local_file_info.append(None)
try:
local_file_info.append(media_info['tracks'][2]['other_channel_s'])
except IndexError
local_file_info.append(None)
try:
local_file_info.append(media_info['tracks'][3]['Format'])
except IndexError
local_file_info.append(None)
But that just looks horrible and i'm sure there is better way to do that.
2 Answers
2
The obvious problem with your code is the massive code duplication. Two useful tools for reducing code duplication are
media_info
try...except
Ideally, you should do both:
media_keys = [
(1, 'frame_rate'),
(1, 'other_display_aspect_ratio'),
(2, 'other_samping_rate'),
(2, 'other_channel_s'),
(3, 'Format')
]
def get_media_info(index, key):
try:
return media_info['tracks'][index][key]
except IndexError:
return None
local_file_info = [get_media_info(*keys) for keys in media_keys]
I like your implementation better :). List comprehensions are the way to go.
– jpp
Sep 3 at 11:14
a quick fix would be using if conditions
if 3 in media_info['tracks'].keys():
local_file_info.append(media_info['tracks'][3]['Format'])
else:
local_file_info.append(None)
but it still doesn't fix your problem, since it still looks horrible but atleast its not the dirty way
"Not the dirty way"? This is far dirtier than the OP's solution. You can't make code better by making it less DRY.
– Aran-Fey
Sep 3 at 10:51
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why not just do it with for-loop?
– Lev Zakharov
Sep 3 at 10:50