README.rst 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. ========
  2. backcall
  3. ========
  4. .. image:: https://travis-ci.org/takluyver/backcall.png?branch=master
  5. :target: https://travis-ci.org/takluyver/backcall
  6. Specifications for callback functions passed in to an API
  7. If your code lets other people supply callback functions, it's important to
  8. specify the function signature you expect, and check that functions support that.
  9. Adding extra parameters later would break other peoples code unless you're careful.
  10. backcall provides a way of specifying the callback signature using a prototype
  11. function::
  12. from backcall import callback_prototype
  13. @callback_prototype
  14. def handle_ping(sender, delay=None):
  15. # Specify positional parameters without a default, and keyword
  16. # parameters with a default.
  17. pass
  18. def register_ping_handler(callback):
  19. # This checks and adapts the function passed in:
  20. callback = handle_ping.adapt(callback)
  21. ping_callbacks.append(callback)
  22. If the callback takes fewer parameters than your prototype, *backcall* will wrap
  23. it in a function that discards the extra arguments. If the callback expects
  24. more arguments, a TypeError is thrown when it is registered.
  25. For more details, see the `docs <http://backcall.readthedocs.org/en/latest/>`_ or
  26. the `Demo notebook <http://nbviewer.ipython.org/github/takluyver/backcall/blob/master/Demo.ipynb>`_.
  27. The tests are run with `pytest <http://pytest.org/latest/>`_. In the root directory,
  28. execute::
  29. py.test