chiark / gitweb /
Move SF into its own directory, to seperate SF and Cura. Rename newui to gui.
[cura.git] / Cura / cura_sf / fabmetheus_utilities / geometry / geometry_utilities / evaluate_enumerables / dictionary_attribute.py
1 """
2 Dictionary object attributes.
3
4 """
5
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.
8 import __init__
9
10 from fabmetheus_utilities import euclidean
11
12
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'
17
18
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)
26         return None
27
28
29 class DictionaryAttribute:
30         'Class to handle a dictionary.'
31         def __init__(self, dictionaryObject):
32                 'Initialize.'
33                 self.dictionaryObject = dictionaryObject
34
35         def __repr__(self):
36                 "Get the dictionary representation of this DictionaryAttribute."
37                 return str(self.dictionaryObject)
38
39         def count(self, value):
40                 'Get the count.'
41                 countTotal = 0
42                 for key, iteratorValue in self.dictionaryObject.iteritems():
43                         if iteratorValue == value:
44                                 countTotal += 1
45                 return countTotal
46
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
61
62         def getIsIn(self, value):
63                 'Determine if the value is in.'
64                 return value in self.dictionaryObject
65
66         def getIsNotIn(self, value):
67                 'Determine if the value is in.'
68                 return not(value in self.dictionaryObject)
69
70         def getLength(self):
71                 'Get the length.'
72                 return len(self.dictionaryObject)
73
74         def getMax(self):
75                 'Get the max.'
76                 return max(self.dictionaryObject)
77
78         def getMin(self):
79                 'Get the min.'
80                 return min(self.dictionaryObject)
81
82         def index(self, value):
83                 'Get the index element.'
84                 for key, iteratorValue in self.dictionaryObject.iteritems():
85                         if iteratorValue == value:
86                                 return key
87                 raise ValueError('Value (%s) not found in index in DictionaryAttribute for (%s).' % (value, self.dictionaryObject))
88
89         def length(self):
90                 'Get the length.'
91                 return len(self.dictionaryObject)
92
93         def set(self, itemIndex, value):
94                 'Set value.'
95                 self.dictionaryObject[itemIndex] = value
96                 return self.dictionaryObject
97
98
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)