How to use forbiddenfruit - 10 common examples

To help you get started, we’ve selected a few forbiddenfruit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
def test_dunder_str():
    assert str(1) == "1"
    def always_one(self):
        return 'one'
    curse(int, '__str__', always_one)
    assert str(1) == "one"
github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
def test_overriding_non_c_things():
    "The `curse` function should not blow up on non-c python objs"

    # Given that I have an instance of a python class
    class Yo(object):
        pass

    obj = Yo()

    # When I curse an instance method
    curse(Yo, "my_method", lambda *a, **k: "Yo" * 2)

    # Then I see that my object was cursed properly
    assert obj.my_method() == "YoYo"
github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
def test_dunder_func_chaining():
    """Overload * (mul) operator to to chaining between functions"""
    def matmul_chaining(self, other):
        if not isinstance(other, FunctionType):
            raise NotImplementedError()
        def wrapper(*args, **kwargs):
            res = other(*args, **kwargs)
            if hasattr(res, "__iter__"):
                return self(*res)
            return self(res)

        return wrapper

    curse(FunctionType, "__mul__", matmul_chaining)
    f = lambda x, y: x * y
    g = lambda x: (x, x)

    squared = f * g

    for i in range(0, 10, 2):
        assert squared(i) == i ** 2
github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
def test_sequence_dunder():
    def derive_func(func, deriv_grad):
        if deriv_grad == 0:
            return func

        e = 0.0000001
        def wrapper(x):
            return (func(x + e) - func(x - e)) / (2 * e)
        if deriv_grad == 1:
            return wrapper
        return wrapper[deriv_grad - 1]

    curse(FunctionType, "__getitem__", derive_func)

    # a function an its derivations
    f = lambda x: x ** 3 - 2 * x ** 2
    f_1 = lambda x: 3 * x ** 2 - 4 * x
    f_2 = lambda x: 6 * x - 4

    for x in range(0, 10):
        x = float(x) / 10.
        assert almost_equal(f(x), f[0](x))
        assert almost_equal(f_1(x), f[1](x))
        # our hacky derivation becomes numerically unstable here
        assert almost_equal(f_2(x), f[2](x), e=.01)
github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
def test_dunder_reverse():
    def type_error_str(self):
        return 'type error'
    curse(TypeError, '__str__', type_error_str)
    te = TypeError("testing")
    assert str(te) == "type error"

    reverse(TypeError, '__str__')
    assert str(te) == "testing"
github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
def test_cursing_a_reversed_curse():
    curse(str, 'one', 1)
    assert str.one == 1

    reverse(str, 'one')
    curse(str, 'one', 2)
    assert str.one == 2
github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
def test_reversing_a_builtin():
    # Given that I have a cursed object
    curse(str, 'stuff', property(lambda s: s * 2))

    # When I bless it
    reverse(str, 'stuff')

    # Then I see that str won't contain
    assert 'stuff' not in dir(str)
github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
def test_dunder_list_revert():
    """Test reversion of a curse with dunders"""
    def map_list(func, list_):
        if not callable(func):
            raise NotImplementedError()
        return map(func, list_)

    curse(list, "__add__", map_list)

    list_ = list(range(10))
    times_2 = lambda x: x * 2

    assert list(times_2 + list_) == list(range(0, 20, 2))

    reverse(list, "__add__")
    try:
        times_2 + list_
    except TypeError:
        pass
    else:
        # should always raise an exception
        assert False
github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
def test_cursed_context_manager():
    "The `cursed` context manager should curse an existing symbols in a scope"

    # Given that I have an instance of a python class
    obj = {'a': 1, 'b': 2}

    # When I curse an instance method
    with cursed(dict, "open_box", lambda self: 'surprise'):
        # Then I see that my object was cursed properly
        assert obj.open_box() == 'surprise'

    # And it was reversed
    assert "open_box" not in dir(obj)
    assert "open_box" not in dir(dict)
github clarete / forbiddenfruit / tests / unit / test_forbidden_fruit.py View on Github external
    @cursed(dict, "open_box", lambda self: 'surprise')
    def function():
        # Then I see that my object was cursed properly
        assert obj.open_box() == 'surprise'