GithubHelp home page GithubHelp logo

jonike / gorilla Goto Github PK

View Code? Open in Web Editor NEW

This project forked from christophercrouzet/gorilla

0.0 2.0 0.0 164 KB

Convenient approach to monkey patching.

License: MIT License

Python 99.34% Makefile 0.66%

gorilla's Introduction

Gorilla

Build status Coverage Status PyPI latest version Documentation status License

Gorilla is a Python library that provides a convenient approach to monkey patching.

Monkey patching is the process of modifying module and class attributes at runtime with the purpose of replacing or extending third-party code.

Although not a recommended practice, it is sometimes useful to fix or modify the behaviour of a piece of code from a third-party library, or to extend its public interface while making the additions feel like they are built-in into the library.

The Python language makes monkey patching extremely easy but the advantages of Gorilla are multiple, not only in assuring a consistent behaviour on both Python 2 and Python 3 versions, but also in preventing common source of errors, and making the process both intuitive and convenient even when faced with large numbers of patches to create.

Features

  • intuitive and convenient decorator approach to create patches.
  • can create patches for all class or module members at once.
  • compatible with both Python 2 and Python 3.
  • customizable behaviour.

Usage

Thanks to the dynamic nature of Python that makes monkey patching possible, the process happens at runtime without ever having to directly modify the source code of the third-party library:

>>> import gorilla
>>> import destination
>>> @gorilla.patches(destination.Class)
... class MyClass(object):
...     def method(self):
...         print("Hello")
...     @classmethod
...     def class_method(cls):
...         print("world!")

The code above creates two patches, one for each member of the class MyClass, but does not apply them yet. In other words, they define the information required to carry on the operation but are not yet inserted into the specified destination class destination.Class.

Such patches created with the decorators can then be automatically retrieved by recursively scanning a package or a module, then applied:

>>> import gorilla
>>> import mypackage
>>> patches = gorilla.find_patches([mypackage])
>>> for patch in patches:
...     gorilla.apply(patch)

See the Tutorial section from the documentation for more detailed examples and explanations on how to use Gorilla.

Documentation

Read the documentation online at gorilla.readthedocs.io or check its source in the doc directory.

Out There

Projects using Gorilla include:

Author

Christopher Crouzet <christophercrouzet.com>

gorilla's People

Contributors

christophercrouzet avatar

Watchers

 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.