-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOC: Replace old string formatting syntax in calling of Appender decorators #30933
Comments
An code example to explain this ideaThe current implementation is @Appender(_shared_docs["align"] % _shared_doc_kwargs) It might be better if we change it to @Appender(_shared_docs["align"].format(**_shared_doc_kwargs)) This change would also need to update _shared_docs[
"align"
] = """
...
broadcast_axis : %(axes_single_arg)s, default None
...
""" to _shared_docs[
"align"
] = """
...
broadcast_axis : {axes_single_arg}, default None
...
""" |
I think this change is quite big, and the proposed solution is an improvement, but still not as good as it could be. Personally, when reusing a docstring, I'd like to write something like: @doc(method='cummin', operation='minimum')
def cummax(whatever):
"""
This is the {method} method.
It computes the cumulative {operation}.
"""
@doc('pandas.Series.cummax', method='cummin', operation='minimum')
def cummin(whatever):
pass I think it's not difficult to implement a @HH-MWB what's your opinion on this syntax? Do you want to work on a proof of concept to see how this would look like in practice? |
@datapythonista Yes, I agree with you. This seems to be simpler and more readable. And of course, I would be happy to work on that. Should I put |
Solutiondef doc(*args, **kwargs):
def function(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
templates = [func.__doc__ if func.__doc__ else ""]
for arg in args:
if isinstance(arg, str):
templates.append(arg)
elif hasattr(arg, "_doc_template"):
templates.append(arg._doc_template)
elif arg.__doc__:
templates.append(arg.__doc__)
wrapper._doc_template = '\n'.join(dedent(t) for t in templates)
wrapper.__doc__ = wrapper._doc_template.format(**kwargs)
return wrapper
return function All non-keyworded arguments will be consider as doc template reference. If it's a string, then it will be use as a template. If it's a function, then consider the original doc string as template. All templates will be concatenate together, and format by keyword arguments. ExampleHere is some code in /pandas/core/series.py @Appender(
"""
Examples
--------
>>> s = pd.Series(["elk", "pig", "dog", "quetzal"], name="animal")
>>> print(s.to_markdown())
| | animal |
|---:|:---------|
| 0 | elk |
| 1 | pig |
| 2 | dog |
| 3 | quetzal |
"""
)
@Substitution(klass="Series")
@Appender(generic._shared_docs["to_markdown"])
def to_markdown(
self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, **kwargs
) -> Optional[str]:
return self.to_frame().to_markdown(buf, mode, **kwargs) We could rewrite it into @doc(
DataFrame.to_markdown,
"""
Examples
--------
>>> s = pd.Series(["elk", "pig", "dog", "quetzal"], name="animal")
>>> print(s.to_markdown())
| | animal |
|---:|:---------|
| 0 | elk |
| 1 | pig |
| 2 | dog |
| 3 | quetzal |
""",
klass="Series"
)
def to_markdown(
self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, **kwargs
) -> Optional[str]:
return self.to_frame().to_markdown(buf, mode, **kwargs) (Assuming the doc string in |
Thanks for working on this @HH-MWB. That looks like a good improvement to me. Do you want to open a PR fixing some random cases, so people can see how it looks like in practice, and discuss whether it's worth all the effort to replace the current syntax. |
Yes, I will submit a PR, after I briefly structure the code. I want it matches the style here. |
One note about using Now, of course that can be solved (double {{), and probably that doesn't occur to often to be annoying? |
I agree with you. This could be a potential issue when switching from #29547 is working on replacing string formatting to f-string. Foreseeable, in my opinion, the majority string formatting in pandas will be using I guess what we want to compare is:
I would prefer to keep consistent, but this is just my personal thought. I am happy to switching to |
I found calling of
@Appender()
are using%
to format string. I think it might be better to replace with.format
based on PEP 3101 and a discussion on Stack Overflow.#29547 is working on replacing
%
with f-strings. This change would also help to keep the code more consistent in string template. Be more specific, the template using%
to formate will be something like%(XXX)s
, but using.format
and f-strings will be the same as{XXX}
.The text was updated successfully, but these errors were encountered: