-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Allow FuncFormatter to take functions with only one field #17288
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
eed5b94
103314b
14f0106
0ed733c
394d73a
a21c4a8
e2d5181
74df2a9
1232f7a
79e9063
bb7280e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,9 +165,12 @@ | |
""" | ||
|
||
import itertools | ||
import inspect | ||
import logging | ||
import locale | ||
import math | ||
import string | ||
import types | ||
from numbers import Integral | ||
|
||
import numpy as np | ||
|
@@ -377,19 +380,39 @@ class FuncFormatter(Formatter): | |
""" | ||
Use a user-defined function for formatting. | ||
|
||
The function should take in two inputs (a tick value ``x`` and a | ||
position ``pos``), and return a string containing the corresponding | ||
tick label. | ||
The function can take in at most two inputs (a required tick value ``x`` | ||
and an optional position ``pos``), and must return a string containing | ||
the corresponding tick label. | ||
""" | ||
def __init__(self, func): | ||
self.func = func | ||
|
||
if not isinstance(func, types.BuiltinFunctionType): | ||
self.nargs = len(inspect.signature(func).parameters) | ||
elif (isinstance(getattr(func, "__self__"), str) and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed at all? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
(getattr(func, "__name__", "") == "format")): | ||
#check if there's a format spec | ||
self.nargs = len([(_, _, fs, _) for (_, _, fs, _) | ||
in string.Formatter().parse(func.__self__) | ||
if fs is not None]) | ||
else: | ||
#finding argcount for other builtins is a mess | ||
self.nargs = 2 | ||
cbook._warn_external(f"{func.__name__} is not supported " | ||
"and may not work as expected") | ||
if self.nargs not in [1, 2]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think best practices are to always use a set for inclusion testing ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is micro-optimization on the nanosecond level, and for the present case not even a benefit
I think the list is slightly more readable because it's used more often, and because sets and dicts use curly braces so you have to look a bit closer to realize this is a set. |
||
raise TypeError(f"{func.__name__} takes {self.nargs} arguments. " | ||
"FuncFormatter functions take at most 2: " | ||
"x (required), pos (optional).") | ||
|
||
def __call__(self, x, pos=None): | ||
""" | ||
Return the value of the user defined function. | ||
|
||
*x* and *pos* are passed through as-is. | ||
""" | ||
if self.nargs == 1: | ||
return self.func(x) | 48F0||
return self.func(x, pos) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func
is part of the public API, which means it can be changed at runtime. So you can't count on the number of arguments being the same when this is actually called.func
probably needs to be changed into a property which recalculates the number of arguments in the setter.