|
|
|
@ -180,10 +180,13 @@ Exceptions
|
|
|
|
|
|
|
|
|
|
In the main body of the code, use typed exceptions where possible:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
raise errors.AnsibleError("panic!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
versus:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
raise Exception("panic!")
|
|
|
|
|
|
|
|
|
|
Similarly, exception checking should be fine grained:
|
|
|
|
@ -205,7 +208,41 @@ List Comprehensions
|
|
|
|
|
|
|
|
|
|
In general list comprehensions are always preferred to map() and filter() calls.
|
|
|
|
|
|
|
|
|
|
However, they can be abused. Optimize for readability, and avoid nesting them
|
|
|
|
|
However, they can be abused. Optimize for readability, and avoid nesting them too deeply.
|
|
|
|
|
|
|
|
|
|
Regexes
|
|
|
|
|
=======
|
|
|
|
|
|
|
|
|
|
There is a time and place for them, but here's an illustrative joke.
|
|
|
|
|
|
|
|
|
|
"A developer had a problem, and used a regular expression to solve it. Now the developer had two problems".
|
|
|
|
|
|
|
|
|
|
Often regexes are difficult to maintain, and a trusty call to "find" can be a great solution!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Find
|
|
|
|
|
====
|
|
|
|
|
|
|
|
|
|
This expression:
|
|
|
|
|
|
|
|
|
|
if x.find('foo') != -1:
|
|
|
|
|
# blarg
|
|
|
|
|
|
|
|
|
|
Should be written:
|
|
|
|
|
|
|
|
|
|
if 'foo' in x:
|
|
|
|
|
# blarg
|
|
|
|
|
|
|
|
|
|
String checks
|
|
|
|
|
=============
|
|
|
|
|
|
|
|
|
|
To test if something is a string, consider that it may be unicode.
|
|
|
|
|
|
|
|
|
|
# no
|
|
|
|
|
if type(x) == str:
|
|
|
|
|
|
|
|
|
|
# yes
|
|
|
|
|
if isintance(x, basestr):
|
|
|
|
|
|
|
|
|
|
Cleverness
|
|
|
|
|
==========
|
|
|
|
|