1 """\
2 Indexed set module. Contains the indexed set class, a set that is indexed like
3 a dict.
4
5 @author: Aaron Mavrinac
6 @organization: University of Windsor
7 @contact: mavrin1@uwindsor.ca
8 @license: GPL-3
9 """
10
11 import warnings
12 from copy import copy
13
14
17
18
20 """\
21 Indexed set class.
22 """
24 """\
25 Constructor.
26
27 @param index: The attribute by which to index items.
28 @type index: C{str}
29 @param iterable: The iterable to intialize the set with.
30 @type iterable: C{iterable}
31 """
32 self.index = str(index)
33 set.__init__(self)
34 self.update(iterable)
35
37 """\
38 Return a set item indexed by key.
39
40 @param key: The index of the item to get.
41 @type key: C{object}
42 @return: The matching item.
43 @rtype: C{object}
44 """
45 for item in self:
46 if getattr(item, self.index) == key:
47 return item
48 raise KeyError(key)
49
51 """\
52 Assign an item by key. Normally, new items are added via add() and
53 existing items modified via object reference; this is included for
54 completeness.
55
56 @param key: The index of the item to assign.
57 @type key: C{object}
58 @param item: The item to assign.
59 @type item: C{object}
60 """
61 if not getattr(item, self.index) == key:
62 raise ValueError("key does not match item index attribute")
63 if key in self:
64 self.remove(key)
65 set.add(self, item)
66
68 """\
69 Return whether an item is a member of the set, by key or by object.
70
71 @param key: The index or object to test for.
72 @type key: C{object}
73 @return: True if member, false otherwise.
74 @rtype: C{bool}
75 """
76 try:
77 for item in self:
78 if getattr(item, self.index) == key:
79 return True
80 except AttributeError:
81 pass
82 return set.__contains__(self, key)
83
85 """\
86 Update the set by adding all items in an iterable to it.
87
88 @param iterable: The iterable containing the items to add.
89 @type iterable: C{iterable}
90 """
91 keys = self.keys()
92 for item in iterable:
93 if not getattr(item, self.index) in keys:
94 set.add(self, copy(item))
95
96 - def add(self, item):
97 """\
98 Add an item to the set, verifying that it has the required index
99 attribute and that no other item in the set has the same index.
100
101 @param item: The item to add.
102 @type item: C{object}
103 """
104 if not getattr(item, self.index) in self.keys():
105 set.add(self, copy(item))
106 else:
107 warnings.warn("attempted to add item with duplicate index",
108 DuplicateItemWarning)
109
111 """\
112 Remove an item from the set by key or by object.
113
114 @param key: The index or object to remove.
115 @type key: C{object}
116 """
117 try:
118 set.remove(self, self[key])
119 except KeyError:
120 set.remove(self, key)
121
123 """\
124 Return a list of keys in the set.
125
126 @return: List of keys in the set.
127 @rtype: C{list}
128 """
129 return [getattr(item, self.index) for item in self]
130
132 """\
133 Return whether this set contains an item with a given index.
134
135 @param key: The index to test for.
136 @type key: C{object}
137 @return: True if a matching key exists, false otherwise.
138 @rtype: C{bool}
139 """
140 return key in self.keys()
141