GithubHelp home page GithubHelp logo

monkey's Introduction

Monkey

This module is way out of date and has been superseded by testdata.patch, I'd use that instead if you are looking for the functionality of this module, heck, I do.

Easy Python monkey patching

Monkey aims to make it easy to import patched modules for testing or whatnot, it's designed to be easy to use and to fix all the subtle problems monkey patched modules can cause by making the patching explicit and keeping the patched modules out of the global namespace

Monkey Patch a module

Suppose you want to import the wackyzoom module, but you want the foo method to always return bar

import monkey

def foo_patched(*args, **kwargs): return "bar"
wackyzoom = monkey.patch('wackyzoom', foo=foo_patched)

print wackyzoom.foo # prints "bar"

# you can even import the original
import wackyzoom as wz

print wz.foo # will not be monkey patched, so will print whatever this made up func printed originally

That's all there is to it, at this moment, you can even go one level deep (I haven't tested deeper)

wackyzoom = monkey.patch('wackyzoom', {"module.func": foo_patched})

print wackyzoom.modul.func # prints "bar"

And you can pass in multiple things to patch:

wackyzoom = monkey.patch('wackyzoom', {"module.func": foo_patched, "foo": foo_patched})

print wackyzoom.foo # prints "bar"
print wackyzoom.modul.func # prints "bar"

Todo

Currently, Monkey doesn't handle imports that are done in a function, I've got some early code on how to handle that did work, but it's currently disabled pending further testing and cleanup

I don't think it will work n levels deep, ie, I don't think I can patch bar.baz.che in foo yet

Install

Use PIP

pip install git+https://github.com/Jaymon/monkey#egg=monkey

License

MIT I guess, use this code any way you want, if you extend it I'd love a pull request so I can merge any new stuff in.

monkey's People

Contributors

jaymon avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.