Giter VIP home page Giter VIP logo

simpleinterceptor's Introduction

Simple Interceptor

It is a simple tool that can be used to intercept methods of classes to apply advices on them.

Installation

pip install SimpleInterceptor

Please note, pip search SimpleInterceptor doesn't return anything. Alternatively, the package can be cloned or downloaded, extracted and following can be run

  • Install using python setup.py install
  • Run Tests using python setup.py test

Example

If we were to implement a bank transaction logic, it could be as simple as this.

class BankTransaction(object):
    def transfer(self, amt):
        print "Transferring Rs. %d" % amt

And transaction would be done this simply as well.

obj = BankTransaction()
obj.transfer(1000)

Non-intercept Example

Now, there could be various other concerns related to this requirement - logging, checking if balance is available, notifying user about the transaction status etc. We could had written all of this logic there. But in order to modularise it better or separate the cross-cutting concerns, we follow principles of AOP and apply the other concerns(advices) by intercepting the core method. This simple library is intended to do this.

Say the above concerns are implemented this trivially.

def start_transaction_log(*args, **kwargs):
    print "Starting transaction"

def check_balance_available(*args, **kwargs):
    print "Balance check logic says Transaction allowed"

def send_notification(*args, **kwargs):
    print "Transaction successful"

Now, in order to apply these logics to the transfer method, we would need to create an aspect and decorate the BankTransaction class with the intercept decorator, featured by the tool, as shown under. Instead of using transfer we could had used any regex pattern to match the method name. This allows to intercept multiple methods using the same pattern.

aspects = dict()
aspects[r'transfer'] = dict(
    before=start_transaction_log,
    around_before=check_balance_available,
    after_success=send_notification)

from interceptor import intercept

BankTransaction = intercept(aspects)(BankTransaction)

obj = BankTransaction()
obj.transfer(1000)

Intercept Example

Advices honoured

The tool accepts following self-explanatory advices. before logic is run before around_before and around_after is run before after_success. after_exc can be used to write exception logic. around_after doesn't run in case of exception.

  • before
  • around_before
  • after_exc
  • around_after
  • after_success
  • after_finally

simpleinterceptor's People

Contributors

host-anshu avatar

Watchers

James Cloos avatar  avatar

simpleinterceptor's Issues

Block execution in interceptor handler

Hello @host-anshu , is it possible to stop the execution of the intercepted method ?

For example, i intercept the method a with the before handler before_a i would like to stop the execution of a in before_a under some conditions

Best place to intercept for undefined variable

Using the following two tools:
PyCallGraph
cProfile + gprof2dot + graphViz

we get the attached call graphs (only pycallgraph attached, github doesn't support .dot files here). The .dot file can be graphed online at Webgraphviz to view the less detailed call graph.
pycallgraph

Using this obsecure call graph, we need to find the best place to intercept our linting rule. Not related to the call graph, one right place could be AnsibleUndefinedVariable Error. A miniscule implementation can be seen here. It prints the exception in the workers. But then here I'm stuck in a way that how to share a queue or data-store to catch linter output, as ansible binds queue to its workers this way, which if intercepted will raise error.

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.