Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@santisoler
Copy link
Member

@santisoler santisoler commented Nov 30, 2022

Fix flake8 B009 warnings. Refactor the way fixed attributes are
obtained: instead of using getattr with fixed strings, use the
standard object.attribute syntax that is more efficient and readable.
Use hasattr for checking if the wanted attribute exists instead of
using getattr and returning None if not: improves readability.

Fix `flake8` `B008` warnings. Refactor the way fixed attributes are
obtained: instead of using `getattr` with fixed strings, use the
standard `object.attribute` syntax that is more efficient and readable.
Use `hasattr` for checking if the wanted attribute exists instead of
using `getattr` and returning None if not: improves readability.
@codecov
Copy link

codecov bot commented Nov 30, 2022

Codecov Report

Merging #1139 (d5b2283) into main (d45c6c9) will increase coverage by 25.08%.
The diff coverage is 83.33%.

@@             Coverage Diff             @@
##             main    #1139       +/-   ##
===========================================
+ Coverage   55.14%   80.23%   +25.08%     
===========================================
  Files         158      158               
  Lines       23010    23012        +2     
===========================================
+ Hits        12690    18464     +5774     
+ Misses      10320     4548     -5772     
Impacted Files Coverage Δ
SimPEG/directives/directives.py 78.74% <75.00%> (+0.63%) ⬆️
SimPEG/objective_function.py 93.36% <100.00%> (ø)
SimPEG/optimization.py 84.73% <0.00%> (+0.40%) ⬆️
SimPEG/regularization/base.py 93.20% <0.00%> (+0.42%) ⬆️
SimPEG/fields.py 96.29% <0.00%> (+0.46%) ⬆️
SimPEG/utils/code_utils.py 80.66% <0.00%> (+0.50%) ⬆️
SimPEG/props.py 90.24% <0.00%> (+0.81%) ⬆️
SimPEG/simulation.py 94.82% <0.00%> (+0.86%) ⬆️
SimPEG/data.py 92.38% <0.00%> (+0.95%) ⬆️
... and 76 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@jcapriot
Copy link
Member

jcapriot commented Dec 1, 2022

looks good, I know the getattr is used fairly often for caching items on class instances.

@santisoler
Copy link
Member Author

Indeed. It's a great tool to catch attributes programatically. But if the attribute's name is already known, having to allocate a string and calling getattr is less efficient. In cases where we don't know if the attribute exists, I find the getattr(obj, attr, None) less readable than:

if hasattr(obj, attr):
    # Do something with obj.attribute
    ...

@santisoler santisoler force-pushed the getattr-and-constants branch from e894467 to 3e83ccc Compare January 10, 2023 21:57
@jcapriot jcapriot merged commit 06c5bee into simpeg:main Jan 11, 2023
@santisoler santisoler deleted the getattr-and-constants branch January 12, 2023 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants