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