-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/adamamer20/mesa-frames
- Loading branch information
1 parent
f313991
commit 9914834
Showing
10 changed files
with
2,381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from textwrap import dedent | ||
from typing import Callable, Union | ||
|
||
|
||
# doc decorator function ported with modifications from Pandas | ||
# https://github.com/pandas-dev/pandas/blob/master/pandas/util/_decorators.py | ||
|
||
|
||
def doc(*docstrings: Union[str, Callable], **params) -> Callable: | ||
""" | ||
A decorator take docstring templates, concatenate them and perform string | ||
substitution on it. | ||
This decorator will add a variable "_docstring_components" to the wrapped | ||
callable to keep track the original docstring template for potential usage. | ||
If it should be consider as a template, it will be saved as a string. | ||
Otherwise, it will be saved as callable, and later user __doc__ and dedent | ||
to get docstring. | ||
Parameters | ||
---------- | ||
*docstrings : str or callable | ||
The string / docstring / docstring template to be appended in order | ||
after default docstring under callable. | ||
**params | ||
The string which would be used to format docstring template. | ||
""" | ||
|
||
def decorator(decorated: Callable) -> Callable: | ||
# collecting docstring and docstring templates | ||
docstring_components: list[Union[str, Callable]] = [] | ||
if decorated.__doc__: | ||
docstring_components.append(dedent(decorated.__doc__)) | ||
|
||
for docstring in docstrings: | ||
if hasattr(docstring, "_docstring_components"): | ||
docstring_components.extend(docstring._docstring_components) | ||
elif isinstance(docstring, str) or docstring.__doc__: | ||
docstring_components.append(docstring) | ||
|
||
# formatting templates and concatenating docstring | ||
decorated.__doc__ = "".join( | ||
component.format(**params) | ||
if isinstance(component, str) | ||
else dedent(component.__doc__ or "") | ||
for component in docstring_components | ||
) | ||
|
||
decorated._docstring_components = docstring_components | ||
return decorated | ||
|
||
return decorator |
Empty file.
Oops, something went wrong.