summaryrefslogtreecommitdiff
path: root/python-methoddispatch.spec
blob: 51b530a50ed8b0cd2d07e834d6c9a05db9d16431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
%global _empty_manifest_terminate_build 0
Name:		python-methoddispatch
Version:	3.0.2
Release:	1
Summary:	singledispatch decorator for class methods.
License:	BSD
URL:		https://github.com/seequent/methoddispatch
Source0:	https://mirrors.nju.edu.cn/pypi/web/packages/6d/df/8cf2717fd7b13a8619e1dd491ed779a70d38b9912a43bfe9db5cffe616cc/methoddispatch-3.0.2.tar.gz
BuildArch:	noarch


%description
|Build Status|
Python 3.4 added the ``singledispatch`` decorator to the ``functools`` standard library module.
This library adds this functionality to instance methods.
**Deprecation Warning**
``methoddispatch`` 2 and earlier worked on standard functions too, and could be used in place of ``functools.singledispatch``.
Version 3 no longer supports this functionality as it breaks the Zen of Python "There should be only one way to do something".
Doing this also paved the way to support a better API and deprecate the ``methoddispatch.register`` function.
To define a generic method , decorate it with the ``@singledispatch`` decorator. Note that the dispatch happens on the type of the first argument, create your function accordingly.
To add overloaded implementations to the function, use the ``register()`` attribute of the generic function.
It is a decorator, taking a type parameter and decorating a function implementing the operation for that type.
The ``register()`` attribute returns the undecorated function which enables decorator stacking, pickling, as well as creating unit tests for each variant independently
>>> from methoddispatch import singledispatch, register, SingleDispatch
>>> from decimal import Decimal
>>> class MyClass(SingleDispatch):
The ``register()`` attribute only works inside a class statement, relying on ``SingleDispatch.__init_subclass__``
to create the actual dispatch table.  This also means that (unlike functools.singledispatch) two methods
with the same name cannot be registered as only the last one will be in the class dictionary.
Functions not defined in the class can be registered using the ``add_overload`` attribute.
>>> def nothing(obj, arg, verbose=False):
>>> MyClass.fun.add_overload(type(None), nothing)
When called, the generic function dispatches on the type of the first argument
>>> a = MyClass()
>>> a.fun("Hello, world.")
Hello, world.
>>> a.fun("test.", verbose=True)
Let me just say, test.
>>> a.fun(42, verbose=True)
Strength in numbers, eh? 42
>>> a.fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
Enumerate this:
0 spam
1 spam
2 eggs
3 spam
>>> a.fun(None)
Nothing.
>>> a.fun(1.23)
0.615
Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. The original function decorated with ``@singledispatch`` is registered for the base ``object`` type, which means it is used if no better implementation is found.
To check which implementation will the generic function choose for a given type, use the ``dispatch()`` attribute
>>> a.fun.dispatch(float)
<function MyClass.fun_num at 0x1035a2840>
>>> a.fun.dispatch(dict)    # note: default implementation
<function MyClass.fun at 0x103fe0000>
To access all registered implementations, use the read-only ``registry`` attribute
>>> a.fun.registry.keys()
dict_keys([<class 'NoneType'>, <class 'int'>, <class 'object'>,
          <class 'decimal.Decimal'>, <class 'list'>,
          <class 'float'>])
>>> a.fun.registry[float]
<function MyClass.fun_num at 0x1035a2840>
>>> a.fun.registry[object]
<function MyClass.fun at 0x103fe0000>
Subclasses can extend the type registry of the function on the base class with their own overrides.
The ``SingleDispatch`` mixin class ensures that each subclass has it's own independant copy of the dispatch registry
>>> class SubClass(MyClass):
>>> s = SubClass()
>>> s.fun('hello')
str
>>> b = MyClass()
>>> b.fun('hello')
hello
Method overrides do not need to provide the ``register`` decorator again to be used in the dispatch of ``fun``
>>> class SubClass2(MyClass):
>>> s = SubClass2()
>>> s.fun(1)
subclass int
However, providing the register decorator with the same type will also work.
Decorating a method override with a different type (not a good idea) will register the different type and leave the base-class handler in place for the orginal type.
Method overrides can be specified on individual instances if necessary
>>> def fun_str(obj, arg, verbose=False):
>>> b = MyClass()
>>> b.fun.register(str, fun_str)
<function fun_str at 0x000002376A3D32F0>
>>> b.fun('hello')
str
>>> b2 = MyClass()
>>> b2.fun('hello')
hello
In Python 3.6 and later, for functions annotated with types, the decorator will infer the type of the first argument automatically as shown below
>>> class MyClassAnno(SingleDispatch):
>>> class SubClassAnno(MyClassAnno):
In Python 3.5 and earlier, the ``SingleDispatch`` class uses a meta-class ``SingleDispatchMeta`` to manage the dispatch registries.  However in Python 3.6 and later the ``__init_subclass__`` method is used instead.
If your class also inherits from an ABC interface you can use the ``SingleDispatchABCMeta`` metaclass in Python 3.5 and earlier.
Finally, accessing the method ``fun`` via a class will use the dispatch registry for that class
>>> SubClass2.fun(s, 1)
subclass int
>>> MyClass.fun(s, 1)
1
"""

%package -n python3-methoddispatch
Summary:	singledispatch decorator for class methods.
Provides:	python-methoddispatch
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-methoddispatch
|Build Status|
Python 3.4 added the ``singledispatch`` decorator to the ``functools`` standard library module.
This library adds this functionality to instance methods.
**Deprecation Warning**
``methoddispatch`` 2 and earlier worked on standard functions too, and could be used in place of ``functools.singledispatch``.
Version 3 no longer supports this functionality as it breaks the Zen of Python "There should be only one way to do something".
Doing this also paved the way to support a better API and deprecate the ``methoddispatch.register`` function.
To define a generic method , decorate it with the ``@singledispatch`` decorator. Note that the dispatch happens on the type of the first argument, create your function accordingly.
To add overloaded implementations to the function, use the ``register()`` attribute of the generic function.
It is a decorator, taking a type parameter and decorating a function implementing the operation for that type.
The ``register()`` attribute returns the undecorated function which enables decorator stacking, pickling, as well as creating unit tests for each variant independently
>>> from methoddispatch import singledispatch, register, SingleDispatch
>>> from decimal import Decimal
>>> class MyClass(SingleDispatch):
The ``register()`` attribute only works inside a class statement, relying on ``SingleDispatch.__init_subclass__``
to create the actual dispatch table.  This also means that (unlike functools.singledispatch) two methods
with the same name cannot be registered as only the last one will be in the class dictionary.
Functions not defined in the class can be registered using the ``add_overload`` attribute.
>>> def nothing(obj, arg, verbose=False):
>>> MyClass.fun.add_overload(type(None), nothing)
When called, the generic function dispatches on the type of the first argument
>>> a = MyClass()
>>> a.fun("Hello, world.")
Hello, world.
>>> a.fun("test.", verbose=True)
Let me just say, test.
>>> a.fun(42, verbose=True)
Strength in numbers, eh? 42
>>> a.fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
Enumerate this:
0 spam
1 spam
2 eggs
3 spam
>>> a.fun(None)
Nothing.
>>> a.fun(1.23)
0.615
Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. The original function decorated with ``@singledispatch`` is registered for the base ``object`` type, which means it is used if no better implementation is found.
To check which implementation will the generic function choose for a given type, use the ``dispatch()`` attribute
>>> a.fun.dispatch(float)
<function MyClass.fun_num at 0x1035a2840>
>>> a.fun.dispatch(dict)    # note: default implementation
<function MyClass.fun at 0x103fe0000>
To access all registered implementations, use the read-only ``registry`` attribute
>>> a.fun.registry.keys()
dict_keys([<class 'NoneType'>, <class 'int'>, <class 'object'>,
          <class 'decimal.Decimal'>, <class 'list'>,
          <class 'float'>])
>>> a.fun.registry[float]
<function MyClass.fun_num at 0x1035a2840>
>>> a.fun.registry[object]
<function MyClass.fun at 0x103fe0000>
Subclasses can extend the type registry of the function on the base class with their own overrides.
The ``SingleDispatch`` mixin class ensures that each subclass has it's own independant copy of the dispatch registry
>>> class SubClass(MyClass):
>>> s = SubClass()
>>> s.fun('hello')
str
>>> b = MyClass()
>>> b.fun('hello')
hello
Method overrides do not need to provide the ``register`` decorator again to be used in the dispatch of ``fun``
>>> class SubClass2(MyClass):
>>> s = SubClass2()
>>> s.fun(1)
subclass int
However, providing the register decorator with the same type will also work.
Decorating a method override with a different type (not a good idea) will register the different type and leave the base-class handler in place for the orginal type.
Method overrides can be specified on individual instances if necessary
>>> def fun_str(obj, arg, verbose=False):
>>> b = MyClass()
>>> b.fun.register(str, fun_str)
<function fun_str at 0x000002376A3D32F0>
>>> b.fun('hello')
str
>>> b2 = MyClass()
>>> b2.fun('hello')
hello
In Python 3.6 and later, for functions annotated with types, the decorator will infer the type of the first argument automatically as shown below
>>> class MyClassAnno(SingleDispatch):
>>> class SubClassAnno(MyClassAnno):
In Python 3.5 and earlier, the ``SingleDispatch`` class uses a meta-class ``SingleDispatchMeta`` to manage the dispatch registries.  However in Python 3.6 and later the ``__init_subclass__`` method is used instead.
If your class also inherits from an ABC interface you can use the ``SingleDispatchABCMeta`` metaclass in Python 3.5 and earlier.
Finally, accessing the method ``fun`` via a class will use the dispatch registry for that class
>>> SubClass2.fun(s, 1)
subclass int
>>> MyClass.fun(s, 1)
1
"""

%package help
Summary:	Development documents and examples for methoddispatch
Provides:	python3-methoddispatch-doc
%description help
|Build Status|
Python 3.4 added the ``singledispatch`` decorator to the ``functools`` standard library module.
This library adds this functionality to instance methods.
**Deprecation Warning**
``methoddispatch`` 2 and earlier worked on standard functions too, and could be used in place of ``functools.singledispatch``.
Version 3 no longer supports this functionality as it breaks the Zen of Python "There should be only one way to do something".
Doing this also paved the way to support a better API and deprecate the ``methoddispatch.register`` function.
To define a generic method , decorate it with the ``@singledispatch`` decorator. Note that the dispatch happens on the type of the first argument, create your function accordingly.
To add overloaded implementations to the function, use the ``register()`` attribute of the generic function.
It is a decorator, taking a type parameter and decorating a function implementing the operation for that type.
The ``register()`` attribute returns the undecorated function which enables decorator stacking, pickling, as well as creating unit tests for each variant independently
>>> from methoddispatch import singledispatch, register, SingleDispatch
>>> from decimal import Decimal
>>> class MyClass(SingleDispatch):
The ``register()`` attribute only works inside a class statement, relying on ``SingleDispatch.__init_subclass__``
to create the actual dispatch table.  This also means that (unlike functools.singledispatch) two methods
with the same name cannot be registered as only the last one will be in the class dictionary.
Functions not defined in the class can be registered using the ``add_overload`` attribute.
>>> def nothing(obj, arg, verbose=False):
>>> MyClass.fun.add_overload(type(None), nothing)
When called, the generic function dispatches on the type of the first argument
>>> a = MyClass()
>>> a.fun("Hello, world.")
Hello, world.
>>> a.fun("test.", verbose=True)
Let me just say, test.
>>> a.fun(42, verbose=True)
Strength in numbers, eh? 42
>>> a.fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
Enumerate this:
0 spam
1 spam
2 eggs
3 spam
>>> a.fun(None)
Nothing.
>>> a.fun(1.23)
0.615
Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. The original function decorated with ``@singledispatch`` is registered for the base ``object`` type, which means it is used if no better implementation is found.
To check which implementation will the generic function choose for a given type, use the ``dispatch()`` attribute
>>> a.fun.dispatch(float)
<function MyClass.fun_num at 0x1035a2840>
>>> a.fun.dispatch(dict)    # note: default implementation
<function MyClass.fun at 0x103fe0000>
To access all registered implementations, use the read-only ``registry`` attribute
>>> a.fun.registry.keys()
dict_keys([<class 'NoneType'>, <class 'int'>, <class 'object'>,
          <class 'decimal.Decimal'>, <class 'list'>,
          <class 'float'>])
>>> a.fun.registry[float]
<function MyClass.fun_num at 0x1035a2840>
>>> a.fun.registry[object]
<function MyClass.fun at 0x103fe0000>
Subclasses can extend the type registry of the function on the base class with their own overrides.
The ``SingleDispatch`` mixin class ensures that each subclass has it's own independant copy of the dispatch registry
>>> class SubClass(MyClass):
>>> s = SubClass()
>>> s.fun('hello')
str
>>> b = MyClass()
>>> b.fun('hello')
hello
Method overrides do not need to provide the ``register`` decorator again to be used in the dispatch of ``fun``
>>> class SubClass2(MyClass):
>>> s = SubClass2()
>>> s.fun(1)
subclass int
However, providing the register decorator with the same type will also work.
Decorating a method override with a different type (not a good idea) will register the different type and leave the base-class handler in place for the orginal type.
Method overrides can be specified on individual instances if necessary
>>> def fun_str(obj, arg, verbose=False):
>>> b = MyClass()
>>> b.fun.register(str, fun_str)
<function fun_str at 0x000002376A3D32F0>
>>> b.fun('hello')
str
>>> b2 = MyClass()
>>> b2.fun('hello')
hello
In Python 3.6 and later, for functions annotated with types, the decorator will infer the type of the first argument automatically as shown below
>>> class MyClassAnno(SingleDispatch):
>>> class SubClassAnno(MyClassAnno):
In Python 3.5 and earlier, the ``SingleDispatch`` class uses a meta-class ``SingleDispatchMeta`` to manage the dispatch registries.  However in Python 3.6 and later the ``__init_subclass__`` method is used instead.
If your class also inherits from an ABC interface you can use the ``SingleDispatchABCMeta`` metaclass in Python 3.5 and earlier.
Finally, accessing the method ``fun`` via a class will use the dispatch registry for that class
>>> SubClass2.fun(s, 1)
subclass int
>>> MyClass.fun(s, 1)
1
"""

%prep
%autosetup -n methoddispatch-3.0.2

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-methoddispatch -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 3.0.2-1
- Package Spec generated