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 _logger = logging.getLogger("pytilities.overloading.overload")
24
26 """
27 A list of parameters that when matched, call an associated operation.
28
29 You can think of it as part of an operation signature
30
31 Methods:
32
33 - `process_call`: Call overload's operation if args match.
34 - `insert_params`: Insert parameters
35 """
36
38 """
39 Construct an Overload
40
41 Parameters:
42
43 `function`
44 the operation to call when a call's args match the params
45
46 `params` :: (Parameter...)
47 sequence of parameters that args of calls should match. The
48 overload will only match when all params are satisfied and no
49 args are left.
50 """
51 self.__function = operation
52 self.__params = []
53 self.insert_params(0, *params)
54
56 """
57 Call the overload's operation with args if all args match.
58
59 Parameters:
60
61 `args` :: (value...)
62 positional arguments of the call
63
64 `kwargs` :: {name::string : value}
65 keyword arguments of the call
66
67 Returns (True, return_value) if the args matched, (False, None) otherwise
68 """
69
70
71
72
73 if len(self.__params) < len(args) + len(kwargs):
74 _logger.debug("Overload failed to match: unexpected amount of args")
75 return (False, None)
76
77
78 for arg, param in zip(args, self.__params):
79 if not param.read_arg(arg):
80 _logger.debug("Overload failed to match, failed at %s" %
81 param)
82 return (False, None)
83
84
85 for param in self.__params[len(args):]:
86 if not param.read_kwargs(kwargs):
87 _logger.debug("Overload failed to match, failed at %s" %
88 param)
89 return (False, None)
90
91
92
93 call_args = {}
94 for param in self.__params:
95 param.write(call_args)
96
97
98 return (True, self.__function(**call_args))
99
101 """
102 Insert parameters into the current list of parameters at index
103
104 Parameters:
105
106 `index` :: number
107 index of where to insert
108
109 `params` :: (Parameter...)
110 the parameters to insert
111 """
112 self.__params[index:index] = params
113