Package pytilities :: Package overloading :: Module overload
[hide private]
[frames] | no frames]

Source Code for Module pytilities.overloading.overload

  1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
  2  #  
  3  # This file is part of pytilities. 
  4  #  
  5  # pytilities is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  #  
 10  # pytilities is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  #  
 15  # You should have received a copy of the GNU General Public License 
 16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>. 
 17  # 
 18   
 19  __docformat__ = 'reStructuredText' 
 20   
 21  import logging 
 22   
 23  _logger = logging.getLogger("pytilities.overloading.overload") 
 24   
25 -class Overload(object):
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
35 - def __init__(self, operation, *params):
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
53 - def process_call(self, args, kwargs):
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 # check for the right amount of args (the rest of the algorithm depends 69 # on this). Note: this assumes there are no overlaps, so the debug 70 # message may dictate the wrong reason, but this will do 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 # match args 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 # match kwargs 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 # we have a match 90 # construct a list of arguments for the call 91 call_args = {} 92 for param in self.__params: 93 param.write(call_args) 94 95 # make the call 96 return (True, self.__function(**call_args))
97
98 - def insert_params(self, index, *params):
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