Skip to content

Commit b7f4fb5

Browse files
committed
Update repr, str and add find_all
1 parent 133f2af commit b7f4fb5

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/matplotlib/__init__.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,16 @@ def __len__(self):
835835
return sum(len(mapping) for mapping in self._namespace_maps)
836836

837837
def __repr__(self):
838-
return repr(dict(self.items()))
838+
class_name = self.__class__.__name__
839+
indent = len(class_name) + 1
840+
with _api.suppress_matplotlib_deprecation_warning():
841+
repr_split = pprint.pformat(dict(self.items()), indent=1,
842+
width=80 - indent).split('\n')
843+
repr_indented = ('\n' + ' ' * indent).join(repr_split)
844+
return f'{class_name}({repr_indented})'
845+
846+
def __str__(self):
847+
return '\n'.join(map('{0[0]}: {0[1]}'.format, sorted(self.items())))
839848

840849
def keys(self):
841850
keys = (
@@ -897,7 +906,21 @@ def update(self, other=(), /, **kwds):
897906
def copy(self):
898907
return deepcopy(self)
899908

900-
# MutableMapping.register(RcParams)
909+
def find_all(self, pattern):
910+
"""
911+
Return the subset of this RcParams dictionary whose keys match,
912+
using :func:`re.search`, the given ``pattern``.
913+
914+
.. note::
915+
916+
Changes to the returned dictionary are *not* propagated to
917+
the parent RcParams dictionary.
918+
919+
"""
920+
pattern_re = re.compile(pattern)
921+
return RcParams((key, value)
922+
for key, value in self.items()
923+
if pattern_re.search(key))
901924

902925

903926
def rc_params(fail_on_error=False):

0 commit comments

Comments
 (0)