1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 __docformat__ = 'reStructuredText'
20
21 import logging
22
23 from .parameter import Param
24
25 _logger = logging.getLogger("pytilities.overloading.overloader")
26
28 """
29 A collection of `Overload`s that can process calls.
30
31 Tool for overloading operations.
32
33 Has an ordered collection of overloads which serve as rules, the operation
34 of the matching overload is called. If none match, an assertion error is
35 raised.
36
37 Methods:
38
39 - `process_call`: Process a call with given args and keyword args
40 """
41
42 - def __init__(self, overloads, add_self = True):
43 """
44 Construct an `Overloader`.
45
46 Parameters:
47
48 `overloads` :: (Overload...)
49 a collection of rules indicating which operation to call for
50 which combination of arguments/parameters. Note: the order can
51 effect which overload is matched as the first matching overload
52 is used. Must specify at least one overload.
53
54 `add_self` :: bool = True
55 if True, a "self" parameter is inserted at the beginning of the
56 parameter list of each overload
57 """
58
59 assert overloads, "Must specify at least one overload"
60
61 self.__overloads = overloads
62
63 if add_self:
64 self_param = Param("self")
65 for overload in overloads:
66 overload.insert_params(0, self_param)
67
69 """
70 Process a call with given args and keyword args.
71
72 The matching overload is called.
73
74 Parameters:
75
76 `args`, `kwargs'
77 arguments of the call
78
79 Returns the return value of the called operation
80 """
81
82 _logger.debug("Processing call")
83
84 for overload in self.__overloads:
85 (matched, return_value) = overload.process_call(args, kwargs)
86 if matched:
87 return return_value
88
89 assert False, "Failed to find matching overload for given"
90