README.rst 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. Decorators for Humans
  2. =====================
  3. The goal of the decorator module is to make it easy to define
  4. signature-preserving function decorators and decorator factories.
  5. It also includes an implementation of multiple dispatch and other niceties
  6. (please check the docs). It is released under a two-clauses
  7. BSD license, i.e. basically you can do whatever you want with it but I am not
  8. responsible.
  9. Installation
  10. -------------
  11. If you are lazy, just perform
  12. ``$ pip install decorator``
  13. which will install just the module on your system.
  14. If you prefer to install the full distribution from source, including
  15. the documentation, clone the `GitHub repo`_ or download the tarball_, unpack it and run
  16. ``$ pip install .``
  17. in the main directory, possibly as superuser.
  18. .. _tarball: https://pypi.org/project/decorator/#files
  19. .. _GitHub repo: https://github.com/micheles/decorator
  20. Testing
  21. --------
  22. If you have the source code installation you can run the tests with
  23. `$ python src/tests/test.py -v`
  24. or (if you have setuptools installed)
  25. `$ python setup.py test`
  26. Notice that you may run into trouble if in your system there
  27. is an older version of the decorator module; in such a case remove the
  28. old version. It is safe even to copy the module `decorator.py` over
  29. an existing one, since we kept backward-compatibility for a long time.
  30. Repository
  31. ---------------
  32. The project is hosted on GitHub. You can look at the source here:
  33. https://github.com/micheles/decorator
  34. Documentation
  35. ---------------
  36. The documentation has been moved to https://github.com/micheles/decorator/blob/master/docs/documentation.md
  37. From there you can get a PDF version by simply using the print
  38. functionality of your browser.
  39. Here is the documentation for previous versions of the module:
  40. https://github.com/micheles/decorator/blob/4.3.2/docs/tests.documentation.rst
  41. https://github.com/micheles/decorator/blob/4.2.1/docs/tests.documentation.rst
  42. https://github.com/micheles/decorator/blob/4.1.2/docs/tests.documentation.rst
  43. https://github.com/micheles/decorator/blob/4.0.0/documentation.rst
  44. https://github.com/micheles/decorator/blob/3.4.2/documentation.rst
  45. For the impatient
  46. -----------------
  47. Here is an example of how to define a family of decorators tracing slow
  48. operations:
  49. .. code-block:: python
  50. from decorator import decorator
  51. @decorator
  52. def warn_slow(func, timelimit=60, *args, **kw):
  53. t0 = time.time()
  54. result = func(*args, **kw)
  55. dt = time.time() - t0
  56. if dt > timelimit:
  57. logging.warn('%s took %d seconds', func.__name__, dt)
  58. else:
  59. logging.info('%s took %d seconds', func.__name__, dt)
  60. return result
  61. @warn_slow # warn if it takes more than 1 minute
  62. def preprocess_input_files(inputdir, tempdir):
  63. ...
  64. @warn_slow(timelimit=600) # warn if it takes more than 10 minutes
  65. def run_calculation(tempdir, outdir):
  66. ...
  67. Enjoy!