Ravenports generated: 14 Jan 2020 19:33
[ravenports.git] / bucket_73 / python-singledispatch
1 # Buildsheet autogenerated by ravenadm tool -- Do not edit.
2
3 NAMEBASE=               python-singledispatch
4 VERSION=                3.4.0.3
5 KEYWORDS=               python devel
6 VARIANTS=               py38 py37
7 SDESC[py37]=            Backport of single-dispatch functions (PY 37)
8 SDESC[py38]=            Backport of single-dispatch functions (PY 38)
9 HOMEPAGE=               http://docs.python.org/3/library/functools.html#functools.singledispatch
10 CONTACT=                Python_Automaton[python@ironwolf.systems]
11
12 DOWNLOAD_GROUPS=        main
13 SITES[main]=            PYPI/s/singledispatch
14 DISTFILE[1]=            singledispatch-3.4.0.3.tar.gz:main
15 DF_INDEX=               1
16 SPKGS[py37]=            single
17 SPKGS[py38]=            single
18
19 OPTIONS_AVAILABLE=      PY38 PY37
20 OPTIONS_STANDARD=       none
21 VOPTS[py37]=            PY38=OFF PY37=ON
22 VOPTS[py38]=            PY38=ON PY37=OFF
23
24 DISTNAME=               singledispatch-3.4.0.3
25
26 GENERATED=              yes
27
28 [PY37].BUILDRUN_DEPENDS_ON=             python-six:single:py37
29 [PY37].USES_ON=                         python:py37
30
31 [PY38].BUILDRUN_DEPENDS_ON=             python-six:single:py38
32 [PY38].USES_ON=                         python:py38
33
34 [FILE:3046:descriptions/desc.single]
35 ==============
36 singledispatch
37 ==============
38
39 `PEP 443 <http://www.python.org/dev/peps/pep-0443/>`_ proposed to expose
40 a mechanism in the ``functools`` standard library module in Python 3.4
41 that provides a simple form of generic programming known as
42 single-dispatch generic functions.
43
44 This library is a backport of this functionality to Python 2.6 - 3.3.
45
46 To define a generic function, decorate it with the ``@singledispatch``
47 decorator. Note that the dispatch happens on the type of the first
48 argument, create your function accordingly::
49
50   >>> from singledispatch import singledispatch
51   >>> @singledispatch
52   ... def fun(arg, verbose=False):
53   ...     if verbose:
54   ...         print("Let me just say,", end=" ")
55   ...     print(arg)
56
57 To add overloaded implementations to the function, use the
58 ``register()`` attribute of the generic function. It is a decorator,
59 taking a type parameter and decorating a function implementing the
60 operation for that type::
61
62   >>> @fun.register(int)
63   ... def _(arg, verbose=False):
64   ...     if verbose:
65   ...         print("Strength in numbers, eh?", end=" ")
66   ...     print(arg)
67   ...
68   >>> @fun.register(list)
69   ... def _(arg, verbose=False):
70   ...     if verbose:
71   ...         print("Enumerate this:")
72   ...     for i, elem in enumerate(arg):
73   ...         print(i, elem)
74
75 To enable registering lambdas and pre-existing functions, the
76 ``register()`` attribute can be used in a functional form::
77
78   >>> def nothing(arg, verbose=False):
79   ...     print("Nothing.")
80   ...
81   >>> fun.register(type(None), nothing)
82
83 The ``register()`` attribute returns the undecorated function which
84 enables decorator stacking, pickling, as well as creating unit tests for
85 each variant independently::
86
87   >>> @fun.register(float)
88   ... @fun.register(Decimal)
89   ... def fun_num(arg, verbose=False):
90   ...     if verbose:
91   ...         print("Half of your number:", end=" ")
92   ...     print(arg / 2)
93   ...
94   >>> fun_num is fun
95   False
96
97 When called, the generic function dispatches on the type of the first
98 argument::
99
100   >>> fun("Hello, world.")
101   Hello, world.
102   >>> fun("test.", verbose=True)
103   Let me just say, test.
104   >>> fun(42, verbose=True)
105   Strength in numbers, eh? 42
106   >>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
107   Enumerate this:
108   0 spam
109   1 spam
110   2 eggs
111   3 spam
112   >>> fun(None)
113   Nothing.
114   >>> fun(1.23)
115   0.615
116
117 Where there is no registered implementation for a specific type, its
118 method resolution order is used to find a more generic implementation.
119 The original function decorated with ``@singledispatch`` is registered
120 for the base ``object`` type, which means it is used if no better
121 implementation is found.
122
123 To check which implementation will the generic function choose for
124 a given type, use the ``dispatch()`` attribute::
125
126   >>> fun.dispatch(float)
127   <function fun_num at 0x1035a2840>
128   >>> fun.dispatch(dict)    # note: default implementation
129   <function fun at 0x103fe0000>
130
131 To access all registered implementations, use the read-only ``registry``
132 attribute::
133
134   >>> fun.registry.keys()
135
136
137 [FILE:108:distinfo]
138 5b06af87df13818d14f08a028e42f566640aef80805c3b50c5056b086e3c2b9c         9529 singledispatch-3.4.0.3.tar.gz
139