Git Product home page Git Product logo

pydvice's Introduction

Pydvice: An attempt at bringing (defadvice ...) from Elisp to Python
         AKA Don't you hate it when youre not writing lisp?

In Elisp (defadvice ...) allows one add code 'before', 'after' and 'around' a given function
to modify, augment, or merely hook into behavior, modify the function arguments and returns  and more.
Each piece of advice can be independently activated and deactivated.  And the sum contribution
of this functionality leads to a much increased ease in code reuse and massaging to a specific
application of more general functionality.

This is my attempt to bring these features to Python by any means necessary.

I advise (get it?) any intrigued party to glance at the Elisp documentation for the advice system:
Elisp advice: http://www.gnu.org/s/emacs/manual/html_node/elisp/Advising-Functions.html

Project Goals:
- Provide 'before', 'after', and 'around' advice features to any function.
- Allow any given piece of advice to be activated or deactivated
- Allow consumers of advised functions to remain ignorant of the application of advice
- Provide robust argument mangling (get/set/reorder) in before and around advice
- Provide robust return value handling in around and after advice
- Try not to suck to badly at any of the above
- Have a good time

Obligatory copy-pasta proof/tutorial/crashcource
------------------------------------------------
# Get the code!
$ git clone git://github.com/sshirokov/pydvice.git
$ cd pydvice

# Run the tests and feel good (or bad) about the result!
$ python test.py

# Built a virtualenv for it and install the library
$ virtualenv ve
$ . ve/bin/activate
$ python setup.py develop

# Throw caution to the wind and give it a shot
$ python

# Import the library
>>> from pydvice import pydvice

# Write a function
>>> def there():
...   print "there",
...
>>> there()
there

# Write a peice of 'before advice' to run before the function
#   New advice is activated by default
>>> @pydvice.before(there)
... def hello():
...   print "Hello",
...

# Note that active advice runs automatically on any function call
>>> there()
Hello there

# Define a peice of 'after advice' but leave it disabled
>>> @pydvice.after(there, activate=False)
... def world(result, args, kwargs):
...   print "world!",
...

# Note that bound, but deactivated advice does not affect the function
>>> there()
Hello there

# Activate the after advice directly
>>> world.advice.activate()
<pydvice.After object at 0x1005bfa10>

# Note that all active advice runs in the correct order
>>> there()
Hello there world!

# Deactivate all advice to restore original behavior
>>> pydvice.deactivate_all()
>>> there()
there

# Write a peice of after advice that changes (or adds) the return value
>>> @pydvice.after(there)
... def return_something(ret, args, kwargs):
...   return "World greeted"
... 

# Note that our old function now returns a value
>>> there()
Hello there world!
'World greeted'

# Seriously, it does
>>> there() == 'World greeted'
Hello there world!
True

# Leave the interpreter excited to try this out for real
>>> exit()

pydvice's People

Contributors

sshirokov 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.