Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Papercuts #16
Conversation
AdamIsrael
added some commits
Jan 25, 2016
chuckbutler
reviewed
Jan 26, 2016
| @@ -35,8 +35,10 @@ def validate_config(): | ||
| except Exception as e: | ||
| remove_state('vpe.configured') | ||
| set_state('blocked', 'validation failed: %s' % e) | ||
| - else: |
marcoceppi
Jan 26, 2016
Owner
Not a nice catch. There should be a finally and else. Else means no exception raised. Finally means everything after including exception. With this change vpe.configured will be set always
johnsca
Jan 26, 2016
Owner
It would probably be less confusing to just move the else: code inside the try block at the bottom.
Edit: The main reason to use the else clause with try is if you expect the code in the else clause to raise an exception that would be caught by the except which you don't want to catch. In this case, your except clause is too broad and you should catch something more specific than Exception (both of those could reasonably be ValueErrors, or, even better, you could create a ValidationError subclass), at which point it would be reasonable to move the code from the else block into the try.
chuckbutler
Jan 26, 2016
Contributor
#TIL - python has except/else - https://docs.python.org/3/tutorial/errors.html
marcoceppi
Jan 26, 2016
Owner
It's a good point, moving the set_state inside of the try block is probably best
marcoceppi
reviewed
Jan 26, 2016
| @@ -35,8 +35,10 @@ def validate_config(): | ||
| except Exception as e: | ||
| remove_state('vpe.configured') | ||
| set_state('blocked', 'validation failed: %s' % e) | ||
| - else: | ||
| + finally: | ||
| + remove_state('blocked') |
AdamIsrael
Jan 26, 2016
Member
Line 37 set the state. I found setting the config values never cleared the state.
|
Fixed the else and state/status switch. I redeployed and confirmed that it's working as expected now. |
|
I'm +1 on this PR As my initial glance was interrupted and i got completely schooled on some python :) I'll reserve the actual merging to @marcoceppi |
marcoceppi
reviewed
Jan 26, 2016
| @@ -25,7 +25,7 @@ | ||
| @hook('config-changed') | ||
| def validate_config(): | ||
| try: | ||
| - if ('pass', 'vpe-router', 'user') not in cfg: | ||
| + if not cfg.keys() & {'pass', 'vpe-router', 'user'}: |
marcoceppi
Jan 26, 2016
Owner
This syntax is odd to me. maybe if ['pass', 'vpe-router', 'user'] not in cfg.keys() instead
marcoceppi
Jan 26, 2016
Owner
yeah, I see that. I'd prefer this instead:
>>> cfg
{'vpe-router': None, 'obama': None, 'user': None, 'pass': None}
>>> all(k in cfg for k in ['pass', 'vpe-router', 'user'])
True
>>> del cfg['pass']
>>> cfg
{'vpe-router': None, 'obama': None, 'user': None}
>>> all(k in cfg for k in ['pass', 'vpe-router', 'user'])
False
so
if all(k in cfg for k in ['pass', 'vpe-router', 'user']):
AdamIsrael
added some commits
Jan 26, 2016
|
|

AdamIsrael commentedJan 26, 2016
Fixes a few papercuts:
config_changed hook
fix status_set after successful config_changed
consistent use of status_set