初始提交,未完全测试
This commit is contained in:
56
template/expr/ast/MethodKit.py
Normal file
56
template/expr/ast/MethodKit.py
Normal file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3.9
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
JFinal MethodKit - Method Access Utilities
|
||||
"""
|
||||
|
||||
class MethodKit:
|
||||
"""Method access utilities"""
|
||||
|
||||
@staticmethod
|
||||
def get_method(obj, method_name: str, *args):
|
||||
"""
|
||||
Get method from object
|
||||
|
||||
Args:
|
||||
obj: Object to get method from
|
||||
method_name: Method name
|
||||
args: Method arguments
|
||||
|
||||
Returns:
|
||||
Method or None
|
||||
"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
# Try attribute access
|
||||
if hasattr(obj, method_name):
|
||||
method = getattr(obj, method_name)
|
||||
if callable(method):
|
||||
return method
|
||||
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def invoke_method(obj, method_name: str, *args):
|
||||
"""
|
||||
Invoke method on object
|
||||
|
||||
Args:
|
||||
obj: Object to invoke method on
|
||||
method_name: Method name
|
||||
args: Method arguments
|
||||
|
||||
Returns:
|
||||
Method result or None
|
||||
"""
|
||||
method = MethodKit.get_method(obj, method_name)
|
||||
if method:
|
||||
try:
|
||||
return method(*args)
|
||||
except:
|
||||
return None
|
||||
return None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "MethodKit()"
|
||||
Reference in New Issue
Block a user