Get the name of a function.
(func)
| 1016 | |
| 1017 | |
| 1018 | def funcname(func) -> str: |
| 1019 | """Get the name of a function.""" |
| 1020 | # functools.partial |
| 1021 | if isinstance(func, functools.partial): |
| 1022 | return funcname(func.func) |
| 1023 | # methodcaller |
| 1024 | if isinstance(func, methodcaller): |
| 1025 | return func.method[:50] |
| 1026 | |
| 1027 | module_name = getattr(func, "__module__", None) or "" |
| 1028 | type_name = getattr(type(func), "__name__", None) or "" |
| 1029 | |
| 1030 | # toolz.curry |
| 1031 | if "toolz" in module_name and "curry" == type_name: |
| 1032 | return func.func_name[:50] |
| 1033 | # multipledispatch objects |
| 1034 | if "multipledispatch" in module_name and "Dispatcher" == type_name: |
| 1035 | return func.name[:50] |
| 1036 | # numpy.vectorize objects |
| 1037 | if "numpy" in module_name and "vectorize" == type_name: |
| 1038 | return ("vectorize_" + funcname(func.pyfunc))[:50] |
| 1039 | |
| 1040 | # All other callables |
| 1041 | try: |
| 1042 | name = func.__name__ |
| 1043 | if name == "<lambda>": |
| 1044 | return "lambda" |
| 1045 | return name[:50] |
| 1046 | except AttributeError: |
| 1047 | return str(func)[:50] |
| 1048 | |
| 1049 | |
| 1050 | def typename(typ: Any, short: bool = False) -> str: |
no outgoing calls
searching dependent graphs…