-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathobservableset.py
93 lines (68 loc) · 2.01 KB
/
observableset.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from observable import Observable
class ObservableSet(Observable, set):
def __init__(self, *args, **kwargs):
Observable.__init__(self)
set.__init__(self, kwargs)
def add(self, item):
if item not in self:
set.add(self, item)
self.raiseEvent('itemsAdded', items=[item])
def remove(self, item):
set.remove(self, item)
self.raiseEvent('itemsRemoved', items=[item])
def discard(self, item):
if item not in self:
set.discard(self, item)
self.raiseEvent('itemsRemoved', items=[item])
def pop(self):
item = set.pop(self)
self.raiseEvent('itemsRemoved', items=[item])
def __isub__(self, other):
self.difference_update(self, other)
return self
def __ixor__(self, other):
self.symmetric_difference_update(other)
return self
def __iand__(self, other):
self.intersection_update(other)
return self
def __ior__(self, other):
self.update(other)
return self
def difference_update(self, *args):
for arg in args:
difference = self.difference(arg)
self._update(difference)
def symmetric_difference_update(self, other):
symmetric_difference = self.symmetric_difference(other)
self._update(symmetric_difference)
def intersection_update(self, *args):
for arg in args:
newSet = set.intersection(self, arg)
self._update(newSet)
def update(self, *args):
for arg in args:
newSet = set.union(self, arg)
self._update(newSet)
def clear(self):
if (len(self) > 0):
removedItems = []
for item in self:
removedItems.append(item)
set.clear(self)
self.raiseEvent('itemsRemoved', items=[removedItems])
def _update(self, set2):
elemsAdded = []
elemsRemoved = []
added = set2 - self
for elem in added:
elemsAdded.append(elem)
set.add(self, elem)
removed = self - set2
for elem in removed:
elemsRemoved.append(elem)
set.remove(self, elem)
if(len(elemsAdded) > 0):
self.raiseEvent('itemsAdded', items=elemsAdded)
if (len(elemsRemoved) > 0):
self.raiseEvent('itemsRemoved', items=elemsRemoved)