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

Skip to content

Commit 434b0f5

Browse files
committed
Simplify Artist.update.
Splitting out the loop logic in its own function with its own docstring is a bit overkill.
1 parent 7ff965a commit 434b0f5

2 files changed

Lines changed: 20 additions & 34 deletions

File tree

lib/matplotlib/artist.py

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -969,39 +969,27 @@ def set_in_layout(self, in_layout):
969969

970970
def update(self, props):
971971
"""
972-
Update this artist's properties from the dictionary *props*.
973-
"""
974-
def _update_property(self, k, v):
975-
"""Sorting out how to update property (setter or setattr).
976-
977-
Parameters
978-
----------
979-
k : str
980-
The name of property to update
981-
v : obj
982-
The value to assign to the property
983-
984-
Returns
985-
-------
986-
ret : obj or None
987-
If using a `set_*` method return it's return, else None.
988-
"""
989-
k = k.lower()
990-
# white list attributes we want to be able to update through
991-
# art.update, art.set, setp
992-
if k in {'axes'}:
993-
return setattr(self, k, v)
994-
else:
995-
func = getattr(self, 'set_' + k, None)
996-
if not callable(func):
997-
raise AttributeError('{!r} object has no property {!r}'
998-
.format(type(self).__name__, k))
999-
return func(v)
972+
Update this artist's properties from the dict *props*.
1000973
974+
Parameters
975+
----------
976+
props : dict
977+
"""
978+
ret = []
1001979
with cbook._setattr_cm(self, eventson=False):
1002-
ret = [_update_property(self, k, v) for k, v in props.items()]
1003-
1004-
if len(ret):
980+
for k, v in props.items():
981+
k = k.lower()
982+
# White list attributes we want to be able to update through
983+
# art.update, art.set, setp.
984+
if k == "axes":
985+
ret.append(setattr(self, k, v))
986+
else:
987+
func = getattr(self, f"set_{k}", None)
988+
if not callable(func):
989+
raise AttributeError(f"{type(self).__name__!r} object "
990+
f"has no property {k!r}")
991+
ret.append(func(v))
992+
if ret:
1005993
self.pchanged()
1006994
self.stale = True
1007995
return ret

lib/matplotlib/text.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ def __init__(self,
168168
self.update(kwargs)
169169

170170
def update(self, kwargs):
171-
"""
172-
Update properties from a dictionary.
173-
"""
171+
# docstring inherited
174172
# Update bbox last, as it depends on font properties.
175173
sentinel = object() # bbox can be None, so use another sentinel.
176174
bbox = kwargs.pop("bbox", sentinel)

0 commit comments

Comments
 (0)