@wrapt 使用装饰器进行条件猴子修补
Wrapt有两个特点
- 装饰师的最佳实践
- 猴子补丁
我这样做是为了能够有条件地更改调用的方法。我提供了一个基于 pandas 版本的示例。虽然这可行,但我使用了准系统猴子补丁,而不是使用 wrapt 功能。如何使用 wrapt 来修补替代方法?
import pandas as pd
import wrapt, inspect
from distutils.version import StrictVersion
def alt_impl(alt_fn, cond):
@wrapt.decorator
def wrapper(wrapped, instance, args, kwargs):
nonlocal alt_fn
if cond():
# @staticmethod and @classmethod need to navigate to actual function
if not inspect.isfunction(alt_fn):
alt_fn = alt_fn.__func__
# class instance methods need to be bound to class instance
if instance is not None:
alt_fn = alt_fn.__get__(instance, instance.__class__)
return alt_fn(*args, **kwargs)
else:
return wrapped(*args, **kwargs)
return wrapper
示例用法
class alt_impl_example():
def upgraded_pandas(self, args):
print(f"upgraded {pd.__version__}")
@alt_impl(upgraded_pandas, lambda: StrictVersion(pd.__version__) >= StrictVersion("1.0.0"))
def pandas(self, args):
print(pd.__version__)
t = alt_impl_example()
t.pandas({})