monkeypatching Questions

4

Solved

So I decided to add an url attr_accessor to ActiveStorage::Attachment objects. In development the patch holds for a while until it seems to "have been lost". Meaning it works for few minutes, then...

13

Solved

I have a function with a decorator that I'm trying test with the help of the Python Mock library. I'd like to use mock.patch to replace the real decorator with a mock 'bypass' decorator which just ...
Birkenhead asked 5/10, 2011 at 20:50

4

Solved

With the following code : import types class Foo(): def __getitem__(self, x): return x def new_get(self, x): return x + 1 x = Foo() x.__getitem__ = types.MethodType(new_get, x) x.__getitem...
Shrewsbury asked 8/8, 2019 at 13:10

5

Solved

I am trying to debug the behaviour of a large library I depend on, which uses a scattering (no make that plethora) of debug print statements through its many source files. Trouble is, most if not a...
Phelips asked 3/2, 2011 at 8:22

9

Solved

I am trying to modify the responseText received by a function that I cannot modify. This function creates a XMLHttpRequest that I can attach to, but I have been unable to "wrap" the responseText in...
Legitimate asked 19/10, 2014 at 4:34

26

Solved

The goal is to create a mock class which behaves like a db resultset. So for example, if a database query returns, using a dict expression, {'ab':100, 'cd':200}, then I would like to see: >&g...
More asked 25/8, 2009 at 1:53

9

Solved

For example—say I want to add a helloWorld() method to Python's dict type. Can I do this? JavaScript has a prototype object that behaves this way. Maybe it's bad design and I should subclass...
Atheling asked 15/1, 2011 at 7:27

1

Today I came across a situation where I need to make an extension to monkey-patch some objects in a web page. However I found little to none documentation on how it can actually be achieved, I've t...

4

Solved

I understand that if go code is structured such that it's programmed to interfaces, it's trivial to mock; however, I'm working with a code base that I cannot change (that is not mine) and this is n...
Lick asked 6/5, 2016 at 18:34

1

Solved

I'm trying to understand the value-added of using fastai's fastcore.basics.patch_to decorator. Here's the fastcore way: from fastcore.basics import patch_to class _T3(int): pass @patch_to(_T3)...
Hypogeal asked 11/8, 2022 at 6:20

4

Solved

How can I extend a builtin class in python? I would like to add a method to the str class. I've done some searching but all I'm finding is older posts, I'm hoping someone knows of something newer. ...
Kostman asked 9/12, 2008 at 12:1

3

Solved

I am attempting to unit test some Python 3 code that imports a module. Unfortunately, the way the module is written, simply importing it has unpleasant side effects, which are not important for the...
Bigner asked 17/10, 2019 at 13:3

7

Solved

I am still new to Ruby and basically just writing my first micro-program after finishing Cooper's book. I was pointed to the direction of avoiding monkey patching but the problem is I don't know wh...
Gogol asked 29/3, 2009 at 21:45

19

Solved

How do I add a method to an existing object (i.e., not in the class definition) in Python? I understand that it's not generally considered good practice to do so, except in some cases.
Appalachia asked 4/8, 2008 at 2:17

3

Solved

I came upon this post on monkey patching Django: from django.contrib.auth.models import User User.add_to_class('openid', models.CharField(max_length=250,blank=True)) def get_user_name(self): if...
Nonpartisan asked 16/7, 2011 at 23:8

15

Solved

Ruby can add methods to the Number class and other core types to get effects like this: 1.should_equal(1) But it seems like Python cannot do this. Is this true? And if so, why? Does it have someth...

4

Solved

A 3rd party library we use contains a rather long function that uses a nested function inside it. Our use of that library triggers a bug in that function, and we very much would like to solve that ...
Vania asked 18/12, 2014 at 15:35

4

Solved

Consider my module "mymodule.py" # contents of "mymodule.py" def func1(x): return x * 2 I want to mock this function and alter its return. Per the documentation I can do this: # contents of "...
Nilsson asked 13/6, 2018 at 17:22

3

Solved

I'd like to monkey patch the constructor for this 'Controller' object. But how do I monkey patch the constructor function so I can still call the original? This is what I've tried. // original fun...
Adelric asked 16/12, 2011 at 6:11

5

I have been playing with Gevent, and I like it a lot. However I have run into a problem. Breakpoint are not being hit, and debugging doesn't work (using both Visual Studio Python Tools and Eclipse ...
Dromous asked 15/7, 2012 at 20:27

2

currently I am writing some Webapp, but this time I want to learn how to write proper tests for it (using pytest) :) A very common pattern I often see is to make the default configuration changeab...
Attain asked 18/6, 2015 at 12:36

1

A project I'm involved in uses testbook to test code cells of Jupyter notebooks. Patching works fine — unless the code to be tested asks for user input with input(). I just can't figure out how to ...

3

Solved

I need to make sure that running unit tests won't trigger calling a heavy outer world function, say, this one: # bigbad.py def request(param): return 'I searched the whole Internet for "{}"'.form...
Shondrashone asked 18/5, 2016 at 10:44

2

Solved

The overall goal of what I am trying to achieve is sending a Keras model to each spark worker so that I can use the model within a UDF applied to a column of a DataFrame. To do this, the Keras mode...
Gypsy asked 24/4, 2018 at 16:54

2

Solved

Consider the following: class MockResponse: status_code = 200 @staticmethod def json(): return {'key': 'value'} # where api_session is a fixture def test_api_session_get(monkeypatch, api_sess...
Ladylike asked 15/12, 2020 at 14:55

© 2022 - 2025 — McMap. All rights reserved.