2 Dictionary object attributes.
6 from __future__ import absolute_import
7 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
10 from fabmetheus_utilities import euclidean
13 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
14 __credits__ = 'Art of Illusion <http://www.artofillusion.org/>'
15 __date__ = '$Date: 2008/02/05 $'
16 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
19 def _getAccessibleAttribute(attributeName, dictionaryObject):
20 'Get the accessible attribute.'
21 if attributeName in globalNativeFunctionSet:
22 return getattr(dictionaryObject, attributeName, None)
23 if attributeName in globalGetAccessibleAttributeSet:
24 stringAttribute = DictionaryAttribute(dictionaryObject)
25 return getattr(stringAttribute, attributeName, None)
29 class DictionaryAttribute:
30 'Class to handle a dictionary.'
31 def __init__(self, dictionaryObject):
33 self.dictionaryObject = dictionaryObject
36 "Get the dictionary representation of this DictionaryAttribute."
37 return str(self.dictionaryObject)
39 def count(self, value):
42 for key, iteratorValue in self.dictionaryObject.iteritems():
43 if iteratorValue == value:
47 def delete(self, arguments):
48 'Get the delete dictionary.'
49 if arguments.__class__ != list:
50 del self.dictionaryObject[arguments]
51 return self.dictionaryObject
52 if len(arguments) == 0:
53 self.dictionaryObject.clear()
54 return self.dictionaryObject
55 if len(arguments) == 1:
56 del self.dictionaryObject[arguments[0]]
57 return self.dictionaryObject
58 for enumeratorKey in euclidean.getEnumeratorKeysAlwaysList(self.dictionaryObject, arguments):
59 del self.dictionaryObject[enumeratorKey]
60 return self.dictionaryObject
62 def getIsIn(self, value):
63 'Determine if the value is in.'
64 return value in self.dictionaryObject
66 def getIsNotIn(self, value):
67 'Determine if the value is in.'
68 return not(value in self.dictionaryObject)
72 return len(self.dictionaryObject)
76 return max(self.dictionaryObject)
80 return min(self.dictionaryObject)
82 def index(self, value):
83 'Get the index element.'
84 for key, iteratorValue in self.dictionaryObject.iteritems():
85 if iteratorValue == value:
87 raise ValueError('Value (%s) not found in index in DictionaryAttribute for (%s).' % (value, self.dictionaryObject))
91 return len(self.dictionaryObject)
93 def set(self, itemIndex, value):
95 self.dictionaryObject[itemIndex] = value
96 return self.dictionaryObject
99 globalAccessibleAttributeDictionary = 'count delete getIsIn getIsNotIn getLength getMax getMin index length set'.split()
100 globalGetAccessibleAttributeSet = set(globalAccessibleAttributeDictionary)
101 globalNativeFunctions = 'clear copy fromkeys get items keys pop popitem remove setdefault update values'.split()
102 globalNativeFunctionSet = set(globalNativeFunctions)