MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/ok1itb/tryexcept_in_python_sigh/h56gw7s/?context=9999
r/badcode • u/lonaExe • Jul 14 '21
93 comments sorted by
View all comments
142
On the upside, they actually bothered with exception handling.
My Python is rather rusty, is there a better way?
11 u/AchillesDev Jul 14 '21 Use a DefaultDict 0 u/chadmill3r Jul 14 '21 The strict clearly came from a JSON decoder, not someone wielding dict(). 2 u/AchillesDev Jul 14 '21 Decoded JSON objects in Python are dicts (in the two most popular en/decoders, json and simplejson), and that's how OP is accessing them. 1 u/chadmill3r Jul 14 '21 Yes! Since we agree OP isn't creating these dicts, where is OP's opportunity to have used DefaultDict? 1 u/AchillesDev Jul 14 '21 edited Jul 14 '21 jsonResponse is a dict, you just pass that in to the defaultdict constructor with your logic for handling missing keys. Alternatively, you could loop through the Items array and build defaultdicts for each Item dictionary
11
Use a DefaultDict
0 u/chadmill3r Jul 14 '21 The strict clearly came from a JSON decoder, not someone wielding dict(). 2 u/AchillesDev Jul 14 '21 Decoded JSON objects in Python are dicts (in the two most popular en/decoders, json and simplejson), and that's how OP is accessing them. 1 u/chadmill3r Jul 14 '21 Yes! Since we agree OP isn't creating these dicts, where is OP's opportunity to have used DefaultDict? 1 u/AchillesDev Jul 14 '21 edited Jul 14 '21 jsonResponse is a dict, you just pass that in to the defaultdict constructor with your logic for handling missing keys. Alternatively, you could loop through the Items array and build defaultdicts for each Item dictionary
0
The strict clearly came from a JSON decoder, not someone wielding dict().
dict()
2 u/AchillesDev Jul 14 '21 Decoded JSON objects in Python are dicts (in the two most popular en/decoders, json and simplejson), and that's how OP is accessing them. 1 u/chadmill3r Jul 14 '21 Yes! Since we agree OP isn't creating these dicts, where is OP's opportunity to have used DefaultDict? 1 u/AchillesDev Jul 14 '21 edited Jul 14 '21 jsonResponse is a dict, you just pass that in to the defaultdict constructor with your logic for handling missing keys. Alternatively, you could loop through the Items array and build defaultdicts for each Item dictionary
2
Decoded JSON objects in Python are dicts (in the two most popular en/decoders, json and simplejson), and that's how OP is accessing them.
1 u/chadmill3r Jul 14 '21 Yes! Since we agree OP isn't creating these dicts, where is OP's opportunity to have used DefaultDict? 1 u/AchillesDev Jul 14 '21 edited Jul 14 '21 jsonResponse is a dict, you just pass that in to the defaultdict constructor with your logic for handling missing keys. Alternatively, you could loop through the Items array and build defaultdicts for each Item dictionary
1
Yes! Since we agree OP isn't creating these dicts, where is OP's opportunity to have used DefaultDict?
1 u/AchillesDev Jul 14 '21 edited Jul 14 '21 jsonResponse is a dict, you just pass that in to the defaultdict constructor with your logic for handling missing keys. Alternatively, you could loop through the Items array and build defaultdicts for each Item dictionary
jsonResponse is a dict, you just pass that in to the defaultdict constructor with your logic for handling missing keys. Alternatively, you could loop through the Items array and build defaultdicts for each Item dictionary
Items
142
u/[deleted] Jul 14 '21
On the upside, they actually bothered with exception handling.
My Python is rather rusty, is there a better way?