METADATA 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. Metadata-Version: 2.0
  2. Name: singledispatch
  3. Version: 3.4.0.3
  4. Summary: This library brings functools.singledispatch from Python 3.4 to Python 2.6-3.3.
  5. Home-page: http://docs.python.org/3/library/functools.html#functools.singledispatch
  6. Author: Łukasz Langa
  7. Author-email: lukasz@langa.pl
  8. License: MIT
  9. Keywords: single dispatch generic functions singledispatch genericfunctions decorator backport
  10. Platform: any
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Intended Audience :: Developers
  13. Classifier: License :: OSI Approved :: MIT License
  14. Classifier: Natural Language :: English
  15. Classifier: Operating System :: OS Independent
  16. Classifier: Programming Language :: Python
  17. Classifier: Programming Language :: Python :: 2
  18. Classifier: Programming Language :: Python :: 2.6
  19. Classifier: Programming Language :: Python :: 2.7
  20. Classifier: Programming Language :: Python :: 3
  21. Classifier: Programming Language :: Python :: 3.2
  22. Classifier: Programming Language :: Python :: 3.3
  23. Classifier: Programming Language :: Python :: 3.4
  24. Classifier: Topic :: Software Development :: Libraries
  25. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  26. Requires-Dist: six
  27. ==============
  28. singledispatch
  29. ==============
  30. .. image:: https://secure.travis-ci.org/ambv/singledispatch.png
  31. :target: https://secure.travis-ci.org/ambv/singledispatch
  32. `PEP 443 <http://www.python.org/dev/peps/pep-0443/>`_ proposed to expose
  33. a mechanism in the ``functools`` standard library module in Python 3.4
  34. that provides a simple form of generic programming known as
  35. single-dispatch generic functions.
  36. This library is a backport of this functionality to Python 2.6 - 3.3.
  37. To define a generic function, decorate it with the ``@singledispatch``
  38. decorator. Note that the dispatch happens on the type of the first
  39. argument, create your function accordingly::
  40. >>> from singledispatch import singledispatch
  41. >>> @singledispatch
  42. ... def fun(arg, verbose=False):
  43. ... if verbose:
  44. ... print("Let me just say,", end=" ")
  45. ... print(arg)
  46. To add overloaded implementations to the function, use the
  47. ``register()`` attribute of the generic function. It is a decorator,
  48. taking a type parameter and decorating a function implementing the
  49. operation for that type::
  50. >>> @fun.register(int)
  51. ... def _(arg, verbose=False):
  52. ... if verbose:
  53. ... print("Strength in numbers, eh?", end=" ")
  54. ... print(arg)
  55. ...
  56. >>> @fun.register(list)
  57. ... def _(arg, verbose=False):
  58. ... if verbose:
  59. ... print("Enumerate this:")
  60. ... for i, elem in enumerate(arg):
  61. ... print(i, elem)
  62. To enable registering lambdas and pre-existing functions, the
  63. ``register()`` attribute can be used in a functional form::
  64. >>> def nothing(arg, verbose=False):
  65. ... print("Nothing.")
  66. ...
  67. >>> fun.register(type(None), nothing)
  68. The ``register()`` attribute returns the undecorated function which
  69. enables decorator stacking, pickling, as well as creating unit tests for
  70. each variant independently::
  71. >>> @fun.register(float)
  72. ... @fun.register(Decimal)
  73. ... def fun_num(arg, verbose=False):
  74. ... if verbose:
  75. ... print("Half of your number:", end=" ")
  76. ... print(arg / 2)
  77. ...
  78. >>> fun_num is fun
  79. False
  80. When called, the generic function dispatches on the type of the first
  81. argument::
  82. >>> fun("Hello, world.")
  83. Hello, world.
  84. >>> fun("test.", verbose=True)
  85. Let me just say, test.
  86. >>> fun(42, verbose=True)
  87. Strength in numbers, eh? 42
  88. >>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
  89. Enumerate this:
  90. 0 spam
  91. 1 spam
  92. 2 eggs
  93. 3 spam
  94. >>> fun(None)
  95. Nothing.
  96. >>> fun(1.23)
  97. 0.615
  98. Where there is no registered implementation for a specific type, its
  99. method resolution order is used to find a more generic implementation.
  100. The original function decorated with ``@singledispatch`` is registered
  101. for the base ``object`` type, which means it is used if no better
  102. implementation is found.
  103. To check which implementation will the generic function choose for
  104. a given type, use the ``dispatch()`` attribute::
  105. >>> fun.dispatch(float)
  106. <function fun_num at 0x1035a2840>
  107. >>> fun.dispatch(dict) # note: default implementation
  108. <function fun at 0x103fe0000>
  109. To access all registered implementations, use the read-only ``registry``
  110. attribute::
  111. >>> fun.registry.keys()
  112. dict_keys([<class 'NoneType'>, <class 'int'>, <class 'object'>,
  113. <class 'decimal.Decimal'>, <class 'list'>,
  114. <class 'float'>])
  115. >>> fun.registry[float]
  116. <function fun_num at 0x1035a2840>
  117. >>> fun.registry[object]
  118. <function fun at 0x103fe0000>
  119. The vanilla documentation is available at
  120. http://docs.python.org/3/library/functools.html#functools.singledispatch.
  121. Versioning
  122. ----------
  123. This backport is intended to keep 100% compatibility with the vanilla
  124. release in Python 3.4+. To help maintaining a version you want and
  125. expect, a versioning scheme is used where:
  126. * the first three numbers indicate the version of Python 3.x from which the
  127. backport is done
  128. * a backport release number is provided after the last dot
  129. For example, ``3.4.0.0`` is the **first** release of ``singledispatch``
  130. compatible with the library found in Python **3.4.0**.
  131. A single exception from the 100% compatibility principle is that bugs
  132. fixed before releasing another minor Python 3.x.y version **will be
  133. included** in the backport releases done in the mean time. This rule
  134. applies to bugs only.
  135. Maintenance
  136. -----------
  137. This backport is maintained on BitBucket by Łukasz Langa, one of the
  138. members of the core CPython team:
  139. * `singledispatch Mercurial repository <https://bitbucket.org/ambv/singledispatch>`_
  140. * `singledispatch issue tracker <https://bitbucket.org/ambv/singledispatch/issues>`_
  141. Change Log
  142. ----------
  143. 3.4.0.3
  144. ~~~~~~~
  145. Should now install flawlessly on PyPy as well. Thanks to Ryan Petrello
  146. for finding and fixing the ``setup.py`` issue.
  147. 3.4.0.2
  148. ~~~~~~~
  149. Updated to the reference implementation as of 02-July-2013.
  150. * more predictable dispatch order when abstract base classes are in use:
  151. abstract base classes are now inserted into the MRO of the argument's
  152. class where their functionality is introduced, i.e. issubclass(cls,
  153. abc) returns True for the class itself but returns False for all its
  154. direct base classes. Implicit ABCs for a given class (either
  155. registered or inferred from the presence of a special method like
  156. __len__) are inserted directly after the last ABC explicitly listed in
  157. the MRO of said class. This also means there are less "ambiguous
  158. dispatch" exceptions raised.
  159. * better test coverage and improved docstrings
  160. 3.4.0.1
  161. ~~~~~~~
  162. Updated to the reference implementation as of 31-May-2013.
  163. * better performance
  164. * fixed a corner case with PEP 435 enums
  165. * calls to `dispatch()` also cached
  166. * dispatching algorithm now now a module-level routine called `_find_impl()`
  167. with a simplified implementation and proper documentation
  168. * `dispatch()` now handles all caching-related activities
  169. * terminology more consistent: "overload" -> "implementation"
  170. 3.4.0.0
  171. ~~~~~~~
  172. * the first public release compatible with 3.4.0
  173. Conversion Process
  174. ------------------
  175. This section is technical and should bother you only if you are
  176. wondering how this backport is produced. If the implementation details
  177. of this backport are not important for you, feel free to ignore the
  178. following content.
  179. ``singledispatch`` is converted using `six
  180. <http://pypi.python.org/pypi/six>`_ so that a single codebase can be
  181. used for all compatible Python versions. Because a fully automatic
  182. conversion was not doable, I took the following branching approach:
  183. * the ``upstream`` branch holds unchanged files synchronized from the
  184. upstream CPython repository. The synchronization is currently done by
  185. manually copying the required code parts and stating from which
  186. CPython changeset they come from. The tests should pass on Python 3.4
  187. on this branch.
  188. * the ``default`` branch holds the manually translated version and this
  189. is where all tests are run for all supported Python versions using
  190. Tox.