Package fuzzy :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module fuzzy.utils

 1  # -*- coding: utf-8 -*- 
 2  # 
 3  # Copyright (C) 2009  Rene Liebscher 
 4  # 
 5  # This program is free software; you can redistribute it and/or modify it under 
 6  # the terms of the GNU Lesser General Public License as published by the Free  
 7  # Software Foundation; either version 3 of the License, or (at your option) any 
 8  # later version. 
 9  # 
10  # This program is distributed in the hope that it will be useful, but WITHOUT  
11  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
12  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
13  # details. 
14  #  
15  # You should have received a copy of the GNU Lesser General Public License 
16  # along with this program; if not, see <http://www.gnu.org/licenses/>.  
17  # 
18  """Helper functions for  pyfuzzy.""" 
19  __revision__ = "$Id: utils.py,v 1.8 2010-01-19 21:45:35 rliebscher Exp $" 
20   
21 -def prop(func):
22 """Function decorator for defining property attributes 23 24 The decorated function is expected to return a dictionary 25 containing one or more of the following pairs: 26 - fget - function for getting attribute value 27 - fset - function for setting attribute value 28 - fdel - function for deleting attribute 29 This can be conveniently constructed by the locals() builtin 30 function; see: 31 U{http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183} 32 """ 33 return property(doc=func.__doc__, **func())
34
35 -def checkRange(value, ranges):
36 """Checks if the value is in the defined range. 37 38 The range definition is a list/iterator from: 39 - float values belonging to the defined range M{x \in {a}} 40 - 2-tuples of two floats which define a range not including the tuple values itself M{x \in ]a,b[} 41 - 2-list of two floats which define a range including the list values M{x \in [a,b]} 42 The order of elements is not important. So could define the set of integer numbers by a 43 generator returning the following sequence: M{0,1,-1,2,-2,3-,3,...} . 44 45 It returns True if the value is in one of the defined ranges. 46 Otherwise it returns false. 47 """ 48 for part in ranges: 49 if isinstance(part, float): 50 if part == value: 51 return True 52 elif isinstance(part, list) and len(part) == 2: 53 if part[0] <= value and value <= part[1]: 54 return True 55 elif isinstance(part, tuple) and len(part) == 2: 56 if part[0] < value and value < part[1]: 57 return True 58 else: 59 from fuzzy.Exception import FuzzyException 60 raise FuzzyException("Range definition is wrong") 61 return False
62 63 64 inf = float("inf") 65 inf_p = float("+inf") 66 inf_n = float("-inf") 67