Design Pattern Examples
Overview of object-oriented design patterns
mediator_group_classes.py
Go to the documentation of this file.
8
9
11class Group:
12
13
15
16
17 @property
18 def Name(self) -> str:
19 return self._groupName
20
21
22
23 @property
24 def Users(self) -> list[str]:
25 return self._users.copy()
26
27
28
29
33 def __init__(self, name = "") -> None:
34 self._groupName = name
35 self._users = [] # type list[str]
36
37
41
42
49 def ContainsUser(self, name : str) -> bool:
50 return name in self._users
51
52
53
61 def AddUser(self, name : str) -> None:
62 if not name:
63 raise ValueError("Must specify a user name to add it to the group.")
64
65 if not self.ContainsUser(name):
66 self._users.append(name)
67
68
69
74 def RemoveUser(self, name : str) -> None:
75 if self.ContainsUser(name):
76 self._users.remove(name)
77
78
90 def __eq__(self, o) -> bool:
91 if isinstance(o, Group):
92 return self._groupName == o._groupName
93 elif isinstance(o, str):
94 return self._groupName == o
95 else:
96 raise TypeError("Expecting a Group type or string to compare to.")
97
98
99
101
102
103
107
108
110
111
116 @property
117 def GroupNames(self) -> list[str]:
118 groupNames = []
119
120 for group in self._groups:
121 groupNames.append(group.Name)
122
123 # Case-insensitive sort
124 groupNames.sort(key=str.lower)
125 return groupNames
126
127
128
129
130 def __init__(self) -> None:
131 self._groups = [] # type list[Group]
132
133
137
138
139
147 def _SearchForGroup(self, name : str) -> int:
148 foundIndex = -1
149 try:
150 foundIndex = self._groups.index(Group(name))
151 except ValueError:
152 # name was not found in the list
153 pass
154 return foundIndex
155
156
164 def FindGroup(self, name : str) -> Group:
165 foundIndex = self._SearchForGroup(name)
166 if foundIndex != -1:
167 return self._groups[foundIndex]
168 return None
169
170
171
179 def AddGroup(self, name : str) -> None:
180 if not name:
181 raise ValueError("Must specify a group name to add it to the group list.")
182
183 if not self.FindGroup(name):
184 self._groups.append(Group(name))
185
186
187
192 def RemoveGroup(self, name : str) -> None:
193 foundIndex = self._SearchForGroup(name)
194 if foundIndex != -1:
195 self._groups.remove(Group(name))
None AddUser(self, str name)
Add the specified user to this group.
list[str] Users(self)
Property getter for the names of users in this group: value = o.Users
None RemoveUser(self, str name)
Remove a user from this group.
bool ContainsUser(self, str name)
Determine if the specified user is in this group.
str Name(self)
Property gettef for the name of the group: value = o.Name
bool __eq__(self, o)
Determine if the name or the specified group matches this Group's name.
Group FindGroup(self, str name)
Retrieve the Group instance for the specified group name.
int _SearchForGroup(self, str name)
Get an iterator pointing to the group with the specified name.
None RemoveGroup(self, str name)
Remove the specified group from the list.
_groups
The list of Group instances representing all known groups.
list[str] GroupNames(self)
Property getter for the names of all groups contained in this list: value = o.GroupNames.
None AddGroup(self, str name)
Add a group to the list using the given group name.