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

Source Code for Module pytilities.overloading.decorators

 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  from .overloader import Overloader 
22   
23 -def overloaded(overloads, is_method = True):
24 """ 25 Overloads an operation. 26 27 The decorated operation is replaced entirely, its docstring is copied to 28 the replacement operation. 29 30 Parameters: 31 32 `overloads` :: (Overload...) 33 a collection of rules indicating which operation to call for which 34 combination of arguments. At least one overload must be given. 35 36 `is_method` :: bool 37 If True, the decorated is considered to be a method and a self 38 parameter is added to each overload automatically (don't add self 39 parameters manually). If False, the decorated is treated as a 40 function. 41 42 Usage example:: 43 44 def __init_xy(self, x, y): 45 self.__storage = Storage(x, y) 46 47 def __init_storage(self, storage): 48 self.__storage = storage 49 50 @overloaded(( 51 Overload(__init_xy, 52 Param("x", default=0), 53 Param("y", default=0)), 54 Overload(__init_storage, 55 Param("storage")))) 56 def __init__(self): 57 "docstring" 58 pass 59 """ 60 overloader = Overloader(overloads, is_method) 61 62 # replacement for the decorated 63 def __overloaded(*args, **kwargs): 64 return overloader.process_call(args, kwargs)
65 66 def _overloaded(f): 67 __overloaded.__doc__ = f.__doc__ 68 return __overloaded 69 70 return _overloaded 71