-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAttributePool.py
71 lines (52 loc) · 2.19 KB
/
AttributePool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from AttributeType import AttributeType
from AttributeType import AttributeLexical
from Attribute import Attribute
class AttributePool:
def __init__(self):
self._pool : dict = dict() # AttributeType - AttributeList
self._names : dict = dict() # AttributeName - AttributeType
self._order : list = list() # List of AttributeNames
def createType(self, attributeName : str, attributeLexical : AttributeLexical):
if attributeName in self._names:
return False
else:
currentType = AttributeType(attributeName, attributeLexical)
self._pool[currentType] = []
self._names[attributeName] = currentType
self._order.append(attributeName)
return True
def createAttribute(self, attributeName : str = None):
if attributeName not in self._names:
print("No AttributeType named " + attributeName + " exists in the pool")
return None
else:
currentType = self._names[attributeName]
self._pool[currentType].append(Attribute(currentType))
return self._pool[currentType][-1]
def destroyAttribute(self, attribute: Attribute):
try:
if attribute.getType() in self._pool:
self._pool[attribute.getType()].remove(attribute)
return True
else:
print("Trying to remove attribute with an unknown type:")
print(attribute.getType())
except ValueError:
print("Trying to remove attribute that shouldn't exist:")
print(attribute)
return False
def getAttributeOrder(self, attribute : Attribute):
if attribute.getName() in self._order:
return self._order.index(attribute.getName())
else:
return -1
def getAttributeOrderByName(self, attributeName : str):
if attributeName in self._order:
return self._order.index(attributeName)
else:
return -1
def getAttributeNameByOrder(self, index : int):
if index < len(self._order):
return self._order[index]
else:
return ""