hutool.core.util.func¶
函数字符串解析工具类。
Module Contents¶
Classes¶
函数调用字符串解析与执行工具类。 |
Data¶
API¶
- hutool.core.util.func.logger = 'getLogger(...)'¶
- class hutool.core.util.func.FuncUtil¶
函数调用字符串解析与执行工具类。
基于 Python AST 模块,将函数调用字符串(如
"math.sqrt(2)")解析为 可调用对象或直接执行。支持的功能:
解析函数字符串为
(模块名, 函数名, 位置参数, 关键字参数)解析嵌套调用(如
"datetime.datetime(2024, 1, 1)")将字符串转为
functools.partial可调用对象直接执行函数字符串并返回结果
通过点分路径解析任意 Python 对象
- static parse_func(func_str: str) Optional[Tuple[Optional[str], str, List[Any], Dict[str, Any]]]¶
解析函数调用字符串。
- 参数:
func_str – 函数调用字符串,如
"math.sqrt(2)"- 返回:
(模块名, 函数名, 位置参数列表, 关键字参数字典),无法解析时返回None
示例:
>>> FuncUtil.parse_func("math.sqrt(2)") ('math', 'sqrt', [2], {}) >>> FuncUtil.parse_func("len([1, 2, 3])") (None, 'len', [[1, 2, 3]], {})
- static parse_arg(arg: Any) Any¶
解析单个参数。
支持嵌套函数调用(递归执行)、Python 字面量(int/str/list/dict 等)、 以及 AST 节点回退为源码字符串。
- 参数:
arg – 参数(AST 节点或原始值)
- 返回:
解析后的参数值
- static parse_call(node: ast.Call) Tuple[Optional[str], str, List[Any], Dict[str, Any]]¶
解析 AST Call 节点。
- 参数:
node – AST 函数调用节点
- 返回:
(模块名, 函数名, 位置参数列表, 关键字参数字典)
示例 AST 结构 (
math.sqrt(2)):Call(func=Attribute(value=Name(id='math'), attr='sqrt'), args=[Constant(value=2)], keywords=[])
- static callable_func(func_str: str, model: Any = None) Optional[Callable[..., Any]]¶
解析函数字符串并返回可调用对象。
返回值为
functools.partial,已绑定预设参数,调用时可追加参数。- 参数:
func_str – 函数调用字符串
model – 指定模块对象;为
None时自动从字符串中导入
- 返回:
可调用对象,解析失败返回
None
示例:
>>> sqrt = FuncUtil.callable_func("math.sqrt(2)") >>> sqrt() 1.4142135623730951
- static call_func(func_str: str, *extra_args: Any, model: Any = None, **extra_kwargs: Any) Any¶
解析并执行函数字符串,返回结果。
- 参数:
func_str – 函数调用字符串
extra_args – 追加的位置参数(在原有参数之后)
model – 指定模块对象
extra_kwargs – 追加的关键字参数
- 返回:
函数执行结果
- 抛出:
ValueError – 无法解析函数字符串
ImportError – 无法导入模块
AttributeError – 无法找到函数
示例:
>>> FuncUtil.call_func("math.sqrt", 4) 2.0 >>> FuncUtil.call_func("len", [1, 2, 3]) 3
- static safe_call(func_str: str, *extra_args: Any, default: Any = None, model: Any = None, **extra_kwargs: Any) Any¶
解析并执行函数字符串,失败时返回默认值(不抛异常)。
- 参数:
func_str – 函数调用字符串
extra_args – 追加的位置参数
default – 出错时返回的默认值
model – 指定模块对象
extra_kwargs – 追加的关键字参数
- 返回:
函数执行结果,失败返回
default
示例:
>>> FuncUtil.safe_call("math.sqrt", 4) 2.0 >>> FuncUtil.safe_call("invalid.func", default=-1) -1
- static is_callable(func_str: str) bool¶
判断字符串是否为合法的函数调用表达式。
仅检查语法层面是否可解析,不验证模块/函数是否存在。
- 参数:
func_str – 待检查的字符串
- 返回:
是否为合法的函数调用
示例:
>>> FuncUtil.is_callable("math.sqrt(2)") True >>> FuncUtil.is_callable("not a function") False
- static get_func_name(func_str: str) Optional[str]¶
提取函数调用字符串中的函数名。
- 参数:
func_str – 函数调用字符串
- 返回:
函数名,无法解析时返回
None
示例:
>>> FuncUtil.get_func_name("math.sqrt(2)") 'sqrt' >>> FuncUtil.get_func_name("datetime.datetime.now()") 'now'
- static resolve_func(dotted_path: str) Any¶
将点分路径解析为 Python 对象。
支持模块属性、类方法、嵌套属性等任意 Python 对象的获取。
- 参数:
dotted_path – 点分路径,如
"math.sqrt"或"datetime.datetime.now"- 返回:
解析到的对象,失败返回
None
示例:
>>> FuncUtil.resolve_func("math.sqrt") <built-in function sqrt> >>> FuncUtil.resolve_func("os.path.join") <function join at 0x...>