You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
6.1 KiB

4 years ago
  1. Metadata-Version: 2.1
  2. Name: wrapt
  3. Version: 1.10.11
  4. Summary: Module for decorators, wrappers and monkey patching.
  5. Home-page: https://github.com/GrahamDumpleton/wrapt
  6. Author: Graham Dumpleton
  7. Author-email: Graham.Dumpleton@gmail.com
  8. License: BSD
  9. Platform: UNKNOWN
  10. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: License :: OSI Approved :: BSD License
  12. Classifier: Programming Language :: Python :: 2.6
  13. Classifier: Programming Language :: Python :: 2.7
  14. Classifier: Programming Language :: Python :: 3.3
  15. Classifier: Programming Language :: Python :: 3.4
  16. Classifier: Programming Language :: Python :: 3.5
  17. Classifier: Programming Language :: Python :: 3.6
  18. Classifier: Programming Language :: Python :: Implementation :: CPython
  19. Classifier: Programming Language :: Python :: Implementation :: PyPy
  20. wrapt
  21. =====
  22. |Travis| |Coveralls| |PyPI|
  23. The aim of the **wrapt** module is to provide a transparent object proxy
  24. for Python, which can be used as the basis for the construction of function
  25. wrappers and decorator functions.
  26. The **wrapt** module focuses very much on correctness. It therefore goes
  27. way beyond existing mechanisms such as ``functools.wraps()`` to ensure that
  28. decorators preserve introspectability, signatures, type checking abilities
  29. etc. The decorators that can be constructed using this module will work in
  30. far more scenarios than typical decorators and provide more predictable and
  31. consistent behaviour.
  32. To ensure that the overhead is as minimal as possible, a C extension module
  33. is used for performance critical components. An automatic fallback to a
  34. pure Python implementation is also provided where a target system does not
  35. have a compiler to allow the C extension to be compiled.
  36. Documentation
  37. -------------
  38. For further information on the **wrapt** module see:
  39. * http://wrapt.readthedocs.org/
  40. Quick Start
  41. -----------
  42. To implement your decorator you need to first define a wrapper function.
  43. This will be called each time a decorated function is called. The wrapper
  44. function needs to take four positional arguments:
  45. * ``wrapped`` - The wrapped function which in turns needs to be called by your wrapper function.
  46. * ``instance`` - The object to which the wrapped function was bound when it was called.
  47. * ``args`` - The list of positional arguments supplied when the decorated function was called.
  48. * ``kwargs`` - The dictionary of keyword arguments supplied when the decorated function was called.
  49. The wrapper function would do whatever it needs to, but would usually in
  50. turn call the wrapped function that is passed in via the ``wrapped``
  51. argument.
  52. The decorator ``@wrapt.decorator`` then needs to be applied to the wrapper
  53. function to convert it into a decorator which can in turn be applied to
  54. other functions.
  55. ::
  56. import wrapt
  57. @wrapt.decorator
  58. def pass_through(wrapped, instance, args, kwargs):
  59. return wrapped(*args, **kwargs)
  60. @pass_through
  61. def function():
  62. pass
  63. If you wish to implement a decorator which accepts arguments, then wrap the
  64. definition of the decorator in a function closure. Any arguments supplied
  65. to the outer function when the decorator is applied, will be available to
  66. the inner wrapper when the wrapped function is called.
  67. ::
  68. import wrapt
  69. def with_arguments(myarg1, myarg2):
  70. @wrapt.decorator
  71. def wrapper(wrapped, instance, args, kwargs):
  72. return wrapped(*args, **kwargs)
  73. return wrapper
  74. @with_arguments(1, 2)
  75. def function():
  76. pass
  77. When applied to a normal function or static method, the wrapper function
  78. when called will be passed ``None`` as the ``instance`` argument.
  79. When applied to an instance method, the wrapper function when called will
  80. be passed the instance of the class the method is being called on as the
  81. ``instance`` argument. This will be the case even when the instance method
  82. was called explicitly via the class and the instance passed as the first
  83. argument. That is, the instance will never be passed as part of ``args``.
  84. When applied to a class method, the wrapper function when called will be
  85. passed the class type as the ``instance`` argument.
  86. When applied to a class, the wrapper function when called will be passed
  87. ``None`` as the ``instance`` argument. The ``wrapped`` argument in this
  88. case will be the class.
  89. The above rules can be summarised with the following example.
  90. ::
  91. import inspect
  92. @wrapt.decorator
  93. def universal(wrapped, instance, args, kwargs):
  94. if instance is None:
  95. if inspect.isclass(wrapped):
  96. # Decorator was applied to a class.
  97. return wrapped(*args, **kwargs)
  98. else:
  99. # Decorator was applied to a function or staticmethod.
  100. return wrapped(*args, **kwargs)
  101. else:
  102. if inspect.isclass(instance):
  103. # Decorator was applied to a classmethod.
  104. return wrapped(*args, **kwargs)
  105. else:
  106. # Decorator was applied to an instancemethod.
  107. return wrapped(*args, **kwargs)
  108. Using these checks it is therefore possible to create a universal decorator
  109. that can be applied in all situations. It is no longer necessary to create
  110. different variants of decorators for normal functions and instance methods,
  111. or use additional wrappers to convert a function decorator into one that
  112. will work for instance methods.
  113. In all cases, the wrapped function passed to the wrapper function is called
  114. in the same way, with ``args`` and ``kwargs`` being passed. The
  115. ``instance`` argument doesn't need to be used in calling the wrapped
  116. function.
  117. Repository
  118. ----------
  119. Full source code for the **wrapt** module, including documentation files
  120. and unit tests, can be obtained from github.
  121. * https://github.com/GrahamDumpleton/wrapt
  122. .. |Travis| image:: https://img.shields.io/travis/GrahamDumpleton/wrapt/develop.svg?style=plastic
  123. :target: https://travis-ci.org/GrahamDumpleton/wrapt?branch=develop
  124. .. |Coveralls| image:: https://img.shields.io/coveralls/GrahamDumpleton/wrapt/develop.svg?style=plastic
  125. :target: https://coveralls.io/github/GrahamDumpleton/wrapt?branch=develop
  126. .. |PyPI| image:: https://img.shields.io/pypi/v/wrapt.svg?style=plastic
  127. :target: https://pypi.python.org/pypi/wrapt