1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Helper functions for pyfuzzy."""
19 __revision__ = "$Id: utils.py,v 1.8 2010-01-19 21:45:35 rliebscher Exp $"
20
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
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