我使用一个专门的 python 模块,它在运行时修改一些 Django 类方法(又名猴子修补)。如果我需要这些“旧”版本,是否可以“返回”它们并覆盖猴子补丁?
例如,导入这些类的初始版本之类的东西?
以下是如何在包中完成修补的示例:
from django.template.base import FilterExpression
def patch_filter_expression():
original_resolve = FilterExpression.resolve
def resolve(self, context, ignore_failures=False):
return original_resolve(self, context, ignore_failures=False)
FilterExpression.resolve = resolve
Run Code Online (Sandbox Code Playgroud) 我想扩展 npm 包“truffle”,但目前在版本 4(和版本 5)中,“truffle”没有插件扩展机制。
所以最广泛的问题是如何“猴子修补”它。一种可能是将 truffle 包列为依赖项,然后运行一些 shell 命令(例如“patch”)来修改通常安装在node_modules
.
npm 是否有办法在成功安装节点依赖项后运行此类 shell 命令?
还有其他建议吗?(另一种方法可能是在nodejs级别进行猴子修补,但我认为这会更麻烦。)
编辑
中的postinstall 字段可能就是我正在寻找的。scripts
package.json
如何重写 Rails 中的方法?
更具体地说,我在 6.0.0.rc1 上,我想要这个提交,但它仅在 6.1 上可用: https: //github.com/rails/rails/pull/36072/files
我想重写私有def read_image
方法
module ActiveStorage
class Analyzer::ImageAnalyzer < Analyzer
private
def read_image
Run Code Online (Sandbox Code Playgroud)
你会怎么做,在哪个 Rails 文件夹中?
在单元测试中,我使用monkeypatch
来更改dict
.
from hypothesis import given, strategies
test_dict = {"first": "text1", "second": "text2"}
given(val=strategies.text())
def test_monkeypath(monkeypatch, val):
monkeypatch.setitem(test_dict, "second", val)
assert isinstance(test_dict["second"], str)
Run Code Online (Sandbox Code Playgroud)
测试通过了,但在使用 执行以下测试代码时收到警告pytest
。
=================================================================================================================== warnings summary ====================================================================================================================
.PyCharm2019.2/config/scratches/hypothesis_monkeypatch.py::test_monkeypath
c:\users\d292498\appdata\local\conda\conda\envs\pybt\lib\site-packages\hypothesis\extra\pytestplugin.py:172: HypothesisDeprecationWarning: .PyCharm2019.2/config/scratches/hypothesis_monkeypatch.py::test_monkeypath uses the 'monkeypatch' fixture, wh
ich is reset between function calls but not between test cases generated by `@given(...)`. You can change it to a module- or session-scoped fixture if it is safe to reuse; if not we recommend using a context …
Run Code Online (Sandbox Code Playgroud) 这是Ruby 1.8问题:
我们都知道如何使用Array#uniq
:
[1,2,3,1].uniq #=> [1,2,3]
Run Code Online (Sandbox Code Playgroud)
但是我想知道我们是否可以以一种处理复杂对象的方式对其进行修补。当前行为是这样的:
[{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}].uniq
#=> [{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}]
Run Code Online (Sandbox Code Playgroud)
请求的是:
[{"three"=>"3"}, {"three"=>"4"}, {"three"=>"3"}].uniq
#=> [{"three"=>"3"}, {"three"=>"4"}]
Run Code Online (Sandbox Code Playgroud) 我正在寻找为什么在ruby中扩展基类不是一个好主意的例子.我需要向一些人展示为什么它是一种谨慎使用的武器.
你可以分享任何恐怖故事吗?
随着时间的推移,我发现需要stdlib
从Python中覆盖几个方法以克服限制或添加一些缺少的功能.
在所有情况下,我都添加了一个包装函数,并用我的包装器替换了模块中的原始方法(包装器调用了原始方法).
我为什么这样做?只是为了确保对该方法的所有调用都使用我的新版本,即使这些是从其他第三方模块调用的.
我知道monkeypatching可能是一件坏事但我的问题是,如果你小心使用它是否有用?意思是:
例子:
os.system()
或subprocess.Popen()
- 让您输出到控制台或/和重定向到另一个文件.os.chown()
或os.lchown()
Windows上缺少的方法.像我这样的事情在我看来是不错的覆盖,但我想看看其他人是如何看待他们的,特别是应该被视为可接受的monkeypatch,什么不是.
有没有办法将方法添加到标准Java类,例如Object
?
我知道你不能将类String
作为final的子类,但Object
不是.我知道我可以只是继承Object并在子类中定义一个方法,并使我的所有类都成为其子类,但我宁愿不必这样做.
我怀疑这是不可能的,或者我忽略了一些东西.提前感谢您的任何答案.
我有一个任务
def task():
a = worker()
a.do_some_work()
Run Code Online (Sandbox Code Playgroud)
Worker
本身是一个单独的类,在单独的模块中,谁使用这样的Driver
类,
class Worker(object):
def __init__(self):
self.driver = Driver(args)
...
Run Code Online (Sandbox Code Playgroud)
并且再次Driver
是单独模块中的单独类
所以,当我尝试类似的东西
with patch('package.module.Driver', new=Mock(return_value=999)):
task()
Run Code Online (Sandbox Code Playgroud)
在任务中仍然有一个Driver
类实例,但不是模拟.那是错的.如何解决?
UPD1:
Driver
与Worker
住在不同的模块和Worker
进口Driver
我已经添加了两个小帮手与数字打交道,但是我发现,我不得不复制粘贴我的方法,使其与工作都Bignum
和Fixnum
.如何在没有这种复制粘贴的情况下为这两个数字类编写方法?
class Bignum
def digits
self.to_s.split('').map(&:to_i)
end
def palindrome?
self.to_s == self.to_s.reverse
end
end
class Fixnum
def digits
self.to_s.split('').map(&:to_i)
end
def palindrome?
self.to_s == self.to_s.reverse
end
end
Run Code Online (Sandbox Code Playgroud) monkeypatching ×10
ruby ×4
python ×3
class ×1
java ×1
methods ×1
node.js ×1
npm ×1
npm-install ×1
overriding ×1
pytest ×1
python-2.7 ×1
python-mock ×1
spring ×1
unique ×1
unit-testing ×1
word-wrap ×1