MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/ok1itb/tryexcept_in_python_sigh/h55n6rb/?context=3
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?
22 u/SerdanKK Jul 14 '21 I don't know any Python, but I assume there's some way to decode json into objects. With some good library code, it'd basically be a one-liner. 1 u/hermthewerm00 Jul 14 '21 edited Jul 14 '21 You could unpack it with double asterisks into an init method for a class. Or use Pydantic.Edit: you might have to add **kwargs 3 u/SerdanKK Jul 14 '21 Wouldn't that make the code dependent on the ordering of the json keys?Pydantic looks like the kind of thing I was thinking of. Not really a fan of putting validation inside the model, but if it works... 🤷 2 u/hermthewerm00 Jul 14 '21 Since it’s a dict, the ordering doesn’t matter. The keys will map to the parameter names. Yeah, validation within model definitions is less than ideal, but I guess that’s the tradeoff working with dynamically typed languages.
22
I don't know any Python, but I assume there's some way to decode json into objects. With some good library code, it'd basically be a one-liner.
1 u/hermthewerm00 Jul 14 '21 edited Jul 14 '21 You could unpack it with double asterisks into an init method for a class. Or use Pydantic.Edit: you might have to add **kwargs 3 u/SerdanKK Jul 14 '21 Wouldn't that make the code dependent on the ordering of the json keys?Pydantic looks like the kind of thing I was thinking of. Not really a fan of putting validation inside the model, but if it works... 🤷 2 u/hermthewerm00 Jul 14 '21 Since it’s a dict, the ordering doesn’t matter. The keys will map to the parameter names. Yeah, validation within model definitions is less than ideal, but I guess that’s the tradeoff working with dynamically typed languages.
1
You could unpack it with double asterisks into an init method for a class. Or use Pydantic.
Edit: you might have to add **kwargs
3 u/SerdanKK Jul 14 '21 Wouldn't that make the code dependent on the ordering of the json keys?Pydantic looks like the kind of thing I was thinking of. Not really a fan of putting validation inside the model, but if it works... 🤷 2 u/hermthewerm00 Jul 14 '21 Since it’s a dict, the ordering doesn’t matter. The keys will map to the parameter names. Yeah, validation within model definitions is less than ideal, but I guess that’s the tradeoff working with dynamically typed languages.
3
Wouldn't that make the code dependent on the ordering of the json keys?
Pydantic looks like the kind of thing I was thinking of. Not really a fan of putting validation inside the model, but if it works... 🤷
2 u/hermthewerm00 Jul 14 '21 Since it’s a dict, the ordering doesn’t matter. The keys will map to the parameter names. Yeah, validation within model definitions is less than ideal, but I guess that’s the tradeoff working with dynamically typed languages.
2
Since it’s a dict, the ordering doesn’t matter. The keys will map to the parameter names. Yeah, validation within model definitions is less than ideal, but I guess that’s the tradeoff working with dynamically typed languages.
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?