2 Dictionary object attributes.
6 from __future__ import absolute_import
8 from fabmetheus_utilities import euclidean
11 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
12 __credits__ = 'Art of Illusion <http://www.artofillusion.org/>'
13 __date__ = '$Date: 2008/02/05 $'
14 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
17 def _getAccessibleAttribute(attributeName, dictionaryObject):
18 'Get the accessible attribute.'
19 if attributeName in globalNativeFunctionSet:
20 return getattr(dictionaryObject, attributeName, None)
21 if attributeName in globalGetAccessibleAttributeSet:
22 stringAttribute = DictionaryAttribute(dictionaryObject)
23 return getattr(stringAttribute, attributeName, None)
27 class DictionaryAttribute:
28 'Class to handle a dictionary.'
29 def __init__(self, dictionaryObject):
31 self.dictionaryObject = dictionaryObject
34 "Get the dictionary representation of this DictionaryAttribute."
35 return str(self.dictionaryObject)
37 def count(self, value):
40 for key, iteratorValue in self.dictionaryObject.iteritems():
41 if iteratorValue == value:
45 def delete(self, arguments):
46 'Get the delete dictionary.'
47 if arguments.__class__ != list:
48 del self.dictionaryObject[arguments]
49 return self.dictionaryObject
50 if len(arguments) == 0:
51 self.dictionaryObject.clear()
52 return self.dictionaryObject
53 if len(arguments) == 1:
54 del self.dictionaryObject[arguments[0]]
55 return self.dictionaryObject
56 for enumeratorKey in euclidean.getEnumeratorKeysAlwaysList(self.dictionaryObject, arguments):
57 del self.dictionaryObject[enumeratorKey]
58 return self.dictionaryObject
60 def getIsIn(self, value):
61 'Determine if the value is in.'
62 return value in self.dictionaryObject
64 def getIsNotIn(self, value):
65 'Determine if the value is in.'
66 return not(value in self.dictionaryObject)
70 return len(self.dictionaryObject)
74 return max(self.dictionaryObject)
78 return min(self.dictionaryObject)
80 def index(self, value):
81 'Get the index element.'
82 for key, iteratorValue in self.dictionaryObject.iteritems():
83 if iteratorValue == value:
85 raise ValueError('Value (%s) not found in index in DictionaryAttribute for (%s).' % (value, self.dictionaryObject))
89 return len(self.dictionaryObject)
91 def set(self, itemIndex, value):
93 self.dictionaryObject[itemIndex] = value
94 return self.dictionaryObject
97 globalAccessibleAttributeDictionary = 'count delete getIsIn getIsNotIn getLength getMax getMin index length set'.split()
98 globalGetAccessibleAttributeSet = set(globalAccessibleAttributeDictionary)
99 globalNativeFunctions = 'clear copy fromkeys get items keys pop popitem remove setdefault update values'.split()
100 globalNativeFunctionSet = set(globalNativeFunctions)