| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6404c95 commit 24c5048
15 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -68,12 +68,6 @@ def __call__(self, func): | |||
| 68 | 68 | func.__doc__ = inspect.cleandoc(func.__doc__) % self.params | |
| 69 | 69 | return func | |
| 70 | 70 | ||
| 71 | - def update(self, *args, **kwargs): | ||
| 72 | - """ | ||
| 73 | - Update ``self.params`` (which must be a dict) with the supplied args. | ||
| 74 | - """ | ||
| 75 | - self.params.update(*args, **kwargs) | ||
| 76 | - | ||
| 77 | 71 | ||
| 78 | 72 | class _ArtistKwdocLoader(dict): | |
| 79 | 73 | def __missing__(self, key): | |
@@ -89,23 +83,45 @@ def __missing__(self, key): | |||
| 89 | 83 | return self.setdefault(key, kwdoc(cls)) | |
| 90 | 84 | ||
| 91 | 85 | ||
| 92 | - class _ArtistPropertiesSubstitution(Substitution): | ||
| 86 | + class _ArtistPropertiesSubstitution: | ||
| 93 | 87 | """ | |
| 94 | - A `.Substitution` with two additional features: | ||
| 95 | - | ||
| 96 | - - Substitutions of the form ``%(classname:kwdoc)s`` (ending with the | ||
| 97 | - literal ":kwdoc" suffix) trigger lookup of an Artist subclass with the | ||
| 98 | - given *classname*, and are substituted with the `.kwdoc` of that class. | ||
| 99 | - - Decorating a class triggers substitution both on the class docstring and | ||
| 100 | - on the class' ``__init__`` docstring (which is a commonly required | ||
| 101 | - pattern for Artist subclasses). | ||
| 88 | + A class to substitute formatted placeholders in docstrings. | ||
| 89 | + | ||
| 90 | + This is realized in a single instance ``_docstring.interpd``. | ||
| 91 | + | ||
| 92 | + Use `~._ArtistPropertiesSubstition.register` to define placeholders and | ||
| 93 | + their substitution, e.g. ``_docstring.interpd.register(name="some value")``. | ||
| 94 | + | ||
| 95 | + Use this as a decorator to apply the substitution:: | ||
| 96 | + | ||
| 97 | + @_docstring.interpd | ||
| 98 | + def some_func(): | ||
| 99 | + '''Replace %(name)s.''' | ||
| 100 | + | ||
| 101 | + Decorating a class triggers substitution both on the class docstring and | ||
| 102 | + on the class' ``__init__`` docstring (which is a commonly required | ||
| 103 | + pattern for Artist subclasses). | ||
| 104 | + | ||
| 105 | + Substitutions of the form ``%(classname:kwdoc)s`` (ending with the | ||
| 106 | + literal ":kwdoc" suffix) trigger lookup of an Artist subclass with the | ||
| 107 | + given *classname*, and are substituted with the `.kwdoc` of that class. | ||
| 102 | 108 | """ | |
| 103 | 109 | ||
| 104 | 110 | def __init__(self): | |
| 105 | 111 | self.params = _ArtistKwdocLoader() | |
| 106 | 112 | ||
| 113 | + def register(self, **kwargs): | ||
| 114 | + """ | ||
| 115 | + Register substitutions. | ||
| 116 | + | ||
| 117 | + ``_docstring.interpd.register(name="some value")`` makes "name" available | ||
| 118 | + as a named parameter that will be replaced by "some value". | ||
| 119 | + """ | ||
| 120 | + self.params.update(**kwargs) | ||
| 121 | + | ||
| 107 | 122 | def __call__(self, obj): | |
| 108 | - super().__call__(obj) | ||
| 123 | + if obj.__doc__: | ||
| 124 | + obj.__doc__ = inspect.cleandoc(obj.__doc__) % self.params | ||
| 109 | 125 | if isinstance(obj, type) and obj.__init__ != object.__init__: | |
| 110 | 126 | self(obj.__init__) | |
| 111 | 127 | return obj | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,8 +21,9 @@ class _ArtistKwdocLoader(dict[str, str]): | |||
| 21 | 21 | def __missing__(self, key: str) -> str: ... | |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | - class _ArtistPropertiesSubstitution(Substitution): | ||
| 24 | + class _ArtistPropertiesSubstitution: | ||
| 25 | 25 | def __init__(self) -> None: ... | |
| 26 | + def register(self, **kwargs) -> None: ... | ||
| 26 | 27 | def __call__(self, obj: _T) -> _T: ... | |
| 27 | 28 | ||
| 28 | 29 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -181,5 +181,7 @@ def demo(): | |||
| 181 | 181 | + ", ".join([f"'{cs.name}'" for cs in CapStyle]) \ | |
| 182 | 182 | + "}" | |
| 183 | 183 | ||
| 184 | - _docstring.interpd.update({'JoinStyle': JoinStyle.input_description, | ||
| 185 | - 'CapStyle': CapStyle.input_description}) | ||
| 184 | + _docstring.interpd.register( | ||
| 185 | + JoinStyle=JoinStyle.input_description, | ||
| 186 | + CapStyle=CapStyle.input_description, | ||
| 187 | + ) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -319,4 +319,4 @@ def set_color(self, color): | |||
| 319 | 319 | **kwargs : `~matplotlib.axes.Axes` properties. | |
| 320 | 320 | Other miscellaneous Axes parameters. | |
| 321 | 321 | ''' | |
| 322 | - _docstring.interpd.update(_secax_docstring=_secax_docstring) | ||
| 322 | + _docstring.interpd.register(_secax_docstring=_secax_docstring) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -651,7 +651,7 @@ def _format_cursor_data_override(self, data): | |||
| 651 | 651 | ||
| 652 | 652 | ||
| 653 | 653 | # The docstrings here must be generic enough to apply to all relevant methods. | |
| 654 | - mpl._docstring.interpd.update( | ||
| 654 | + mpl._docstring.interpd.register( | ||
| 655 | 655 | cmap_doc="""\ | |
| 656 | 656 | cmap : str or `~matplotlib.colors.Colormap`, default: :rc:`image.cmap` | |
| 657 | 657 | The Colormap instance or registered colormap name used to map scalar data | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,7 +26,7 @@ | |||
| 26 | 26 | ||
| 27 | 27 | _log = logging.getLogger(__name__) | |
| 28 | 28 | ||
| 29 | - _docstring.interpd.update( | ||
| 29 | + _docstring.interpd.register( | ||
| 30 | 30 | _make_axes_kw_doc=""" | |
| 31 | 31 | location : None or {'left', 'right', 'top', 'bottom'} | |
| 32 | 32 | The location, relative to the parent Axes, where the colorbar Axes | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -536,7 +536,7 @@ def _find_closest_point_on_path(xys, p): | |||
| 536 | 536 | return (d2s[imin], projs[imin], (imin, imin+1)) | |
| 537 | 537 | ||
| 538 | 538 | ||
| 539 | - _docstring.interpd.update(contour_set_attributes=r""" | ||
| 539 | + _docstring.interpd.register(contour_set_attributes=r""" | ||
| 540 | 540 | Attributes | |
| 541 | 541 | ---------- | |
| 542 | 542 | ax : `~matplotlib.axes.Axes` | |
@@ -1450,7 +1450,7 @@ def _initialize_x_y(self, z): | |||
| 1450 | 1450 | return np.meshgrid(x, y) | |
| 1451 | 1451 | ||
| 1452 | 1452 | ||
| 1453 | - _docstring.interpd.update(contour_doc=""" | ||
| 1453 | + _docstring.interpd.register(contour_doc=""" | ||
| 1454 | 1454 | `.contour` and `.contourf` draw contour lines and filled contours, | |
| 1455 | 1455 | respectively. Except as noted, function signatures and return values | |
| 1456 | 1456 | are the same for both versions. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -305,7 +305,7 @@ def _update_bbox_to_anchor(self, loc_in_canvas): | |||
| 305 | 305 | _loc_doc_base.format(parent='axes', default=':rc:`legend.loc`', | |
| 306 | 306 | best=_loc_doc_best, outside='') + | |
| 307 | 307 | _legend_kw_doc_base) | |
| 308 | - _docstring.interpd.update(_legend_kw_axes=_legend_kw_axes_st) | ||
| 308 | + _docstring.interpd.register(_legend_kw_axes=_legend_kw_axes_st) | ||
| 309 | 309 | ||
| 310 | 310 | _outside_doc = """ | |
| 311 | 311 | If a figure is using the constrained layout manager, the string codes | |
@@ -323,20 +323,20 @@ def _update_bbox_to_anchor(self, loc_in_canvas): | |||
| 323 | 323 | _loc_doc_base.format(parent='figure', default="'upper right'", | |
| 324 | 324 | best='', outside=_outside_doc) + | |
| 325 | 325 | _legend_kw_doc_base) | |
| 326 | - _docstring.interpd.update(_legend_kw_figure=_legend_kw_figure_st) | ||
| 326 | + _docstring.interpd.register(_legend_kw_figure=_legend_kw_figure_st) | ||
| 327 | 327 | ||
| 328 | 328 | _legend_kw_both_st = ( | |
| 329 | 329 | _loc_doc_base.format(parent='axes/figure', | |
| 330 | 330 | default=":rc:`legend.loc` for Axes, 'upper right' for Figure", | |
| 331 | 331 | best=_loc_doc_best, outside=_outside_doc) + | |
| 332 | 332 | _legend_kw_doc_base) | |
| 333 | - _docstring.interpd.update(_legend_kw_doc=_legend_kw_both_st) | ||
| 333 | + _docstring.interpd.register(_legend_kw_doc=_legend_kw_both_st) | ||
| 334 | 334 | ||
| 335 | 335 | _legend_kw_set_loc_st = ( | |
| 336 | 336 | _loc_doc_base.format(parent='axes/figure', | |
| 337 | 337 | default=":rc:`legend.loc` for Axes, 'upper right' for Figure", | |
| 338 | 338 | best=_loc_doc_best, outside=_outside_doc)) | |
| 339 | - _docstring.interpd.update(_legend_kw_set_loc_doc=_legend_kw_set_loc_st) | ||
| 339 | + _docstring.interpd.register(_legend_kw_set_loc_doc=_legend_kw_set_loc_st) | ||
| 340 | 340 | ||
| 341 | 341 | ||
| 342 | 342 | class Legend(Artist): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -400,7 +400,7 @@ def _single_spectrum_helper( | |||
| 400 | 400 | ||
| 401 | 401 | ||
| 402 | 402 | # Split out these keyword docs so that they can be used elsewhere | |
| 403 | - _docstring.interpd.update( | ||
| 403 | + _docstring.interpd.register( | ||
| 404 | 404 | Spectral="""\ | |
| 405 | 405 | Fs : float, default: 2 | |
| 406 | 406 | The sampling frequency (samples per time unit). It is used to calculate | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1552,7 +1552,7 @@ def _make_verts(self): | |||
| 1552 | 1552 | ] | |
| 1553 | 1553 | ||
| 1554 | 1554 | ||
| 1555 | - _docstring.interpd.update( | ||
| 1555 | + _docstring.interpd.register( | ||
| 1556 | 1556 | FancyArrow="\n".join( | |
| 1557 | 1557 | (inspect.getdoc(FancyArrow.__init__) or "").splitlines()[2:])) | |
| 1558 | 1558 | ||
@@ -2290,7 +2290,7 @@ def __init_subclass__(cls): | |||
| 2290 | 2290 | # - %(BoxStyle:table_and_accepts)s | |
| 2291 | 2291 | # - %(ConnectionStyle:table_and_accepts)s | |
| 2292 | 2292 | # - %(ArrowStyle:table_and_accepts)s | |
| 2293 | - _docstring.interpd.update({ | ||
| 2293 | + _docstring.interpd.register(**{ | ||
| 2294 | 2294 | f"{cls.__name__}:table": cls.pprint_styles(), | |
| 2295 | 2295 | f"{cls.__name__}:table_and_accepts": ( | |
| 2296 | 2296 | cls.pprint_styles() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments