Conversation
NSoiffer
left a comment
There was a problem hiding this comment.
It seems clunky to keep resetting valid_test_passed and repeating the same structure over and over. I think a better solution is to have a function
validate(user_prefs, keys, valid_values, default_value)
where keys are a list of indexes and valid_values is a list of valid values. Then you can just have
validate(user_preferences, ["Braille", "BrailleCode"], ["Nemeth", "UEB"], "Nemeth")
and have that repeated over and over (maybe boolean could be with [True] or maybe a separate function for that. That would substantially reduce the amount of code.
The implementation of validate would be similar to one of the chunks you currently have for each condition (although it would involve loops or mapping over the keys.
In some languages, it is possible to pass a pointer to the value you want to set (e.g., &user_preferences["Braille"]["BrailleCode"]) which allows you to both dereference it to get the value and also to set it. I wouldn't be surprised if there were a clever way to do that in python, but I don't know it off the top of my head. That would allow you to combine the first two args and avoid a loop in the implementation to get to the dictionary value)
|
Agreed, this is clunky and repetitive. I'm not sure I can use exactly the approach you suggest since I think it may throw an exception before I am ready to catch it. But I have enough of a pointer to go on (pun intended!). |
|
I think by moving it into a function, you can do the catch there and do the correction there (which is why I suggested added the default value to the call). |
Added function to handle validation more elegantly
Add function to check that preference settings are present and valid, use defaults if not.
Resolves #17