-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-registry-pattern-plugins.py
More file actions
431 lines (309 loc) · 11.7 KB
/
02-registry-pattern-plugins.py
File metadata and controls
431 lines (309 loc) · 11.7 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""Question: Create a class Plugin with a class attribute registry to keep
track of all plugin instances.
Use a class method to register a new plugin and
a static method to list all registered plugins.
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what classes and methods you need
# - Start with a simple implementation
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - What is a plugin registry? (central place to track all plugins)
# - How do you automatically register plugins? (in __init__)
# - What's the difference between class method and static method here?
# - How do you prevent duplicate registrations?
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Define the Plugin class with registry
# ===============================================================================
# Explanation:
# Let's start by creating our Plugin class with a class attribute to serve as a registry.
# This registry will keep track of all plugin instances.
class Plugin:
registry = []
# What we accomplished in this step:
# - Created Plugin class with class attribute registry
# Step 2: Add constructor with automatic registration
# ===============================================================================
# Explanation:
# The constructor should automatically register each new plugin instance.
# This ensures that all plugins are tracked without manual registration.
class Plugin:
registry = []
def __init__(self, name):
self.name = name
Plugin.registry.append(self)
# What we accomplished in this step:
# - Added constructor that automatically registers plugins
# - Each plugin has a name for identification
# Step 3: Add class method for manual registration
# ===============================================================================
# Explanation:
# Sometimes we might want to register plugins manually or register plugins
# created elsewhere. A class method provides this functionality.
class Plugin:
registry = []
def __init__(self, name):
self.name = name
Plugin.registry.append(self)
@classmethod
def register_plugin(cls, plugin):
if plugin not in cls.registry:
cls.registry.append(plugin)
# What we accomplished in this step:
# - Added class method for manual plugin registration
# - Added duplicate prevention
# Step 4: Add static method to list plugins
# ===============================================================================
# Explanation:
# A static method to list all registered plugins provides a clean interface
# for accessing plugin information without needing an instance.
class Plugin:
registry = []
def __init__(self, name):
self.name = name
Plugin.registry.append(self)
@classmethod
def register_plugin(cls, plugin):
if plugin not in cls.registry:
cls.registry.append(plugin)
@staticmethod
def list_plugins():
return [plugin.name for plugin in Plugin.registry]
# What we accomplished in this step:
# - Added static method to list all plugin names
# Step 5: Add additional registry management methods
# ===============================================================================
# Explanation:
# Let's add more functionality to make our plugin registry more complete,
# including methods to find, remove, and get detailed information about plugins.
class Plugin:
registry = []
def __init__(self, name, version="1.0", description=""):
self.name = name
self.version = version
self.description = description
Plugin.registry.append(self)
@classmethod
def register_plugin(cls, plugin):
if plugin not in cls.registry:
cls.registry.append(plugin)
@classmethod
def unregister_plugin(cls, plugin):
if plugin in cls.registry:
cls.registry.remove(plugin)
@classmethod
def find_plugin(cls, name):
for plugin in cls.registry:
if plugin.name == name:
return plugin
return None
@staticmethod
def list_plugins():
return [plugin.name for plugin in Plugin.registry]
@staticmethod
def get_plugin_count():
return len(Plugin.registry)
@classmethod
def clear_registry(cls):
cls.registry.clear()
# What we accomplished in this step:
# - Enhanced constructor with version and description
# - Added unregister_plugin method
# - Added find_plugin method
# - Added get_plugin_count static method
# - Added clear_registry for testing
# Step 6: Add string representation and plugin information
# ===============================================================================
# Explanation:
# Let's add methods to make our plugins more informative and easier to work with.
class Plugin:
registry = []
def __init__(self, name, version="1.0", description=""):
self.name = name
self.version = version
self.description = description
self.enabled = True
Plugin.registry.append(self)
@classmethod
def register_plugin(cls, plugin):
if plugin not in cls.registry:
cls.registry.append(plugin)
@classmethod
def unregister_plugin(cls, plugin):
if plugin in cls.registry:
cls.registry.remove(plugin)
@classmethod
def find_plugin(cls, name):
for plugin in cls.registry:
if plugin.name == name:
return plugin
return None
@staticmethod
def list_plugins():
return [plugin.name for plugin in Plugin.registry]
@staticmethod
def get_plugin_count():
return len(Plugin.registry)
@staticmethod
def list_enabled_plugins():
return [plugin.name for plugin in Plugin.registry if plugin.enabled]
@classmethod
def clear_registry(cls):
cls.registry.clear()
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def get_info(self):
return {
'name': self.name,
'version': self.version,
'description': self.description,
'enabled': self.enabled
}
def __str__(self):
status = "Enabled" if self.enabled else "Disabled"
return f"Plugin(name='{self.name}', version='{self.version}', {status})"
def __repr__(self):
return f"Plugin('{self.name}', '{self.version}', '{self.description}')"
# What we accomplished in this step:
# - Added enabled/disabled functionality
# - Added get_info method for detailed information
# - Added string representations
# - Added list_enabled_plugins static method
# Step 7: Test the plugin registry system
# ===============================================================================
# Explanation:
# Finally, let's test our complete plugin registry system to make sure
# all functionality works correctly.
class Plugin:
registry = []
def __init__(self, name, version="1.0", description=""):
self.name = name
self.version = version
self.description = description
self.enabled = True
Plugin.registry.append(self)
@classmethod
def register_plugin(cls, plugin):
if plugin not in cls.registry:
cls.registry.append(plugin)
@classmethod
def unregister_plugin(cls, plugin):
if plugin in cls.registry:
cls.registry.remove(plugin)
@classmethod
def find_plugin(cls, name):
for plugin in cls.registry:
if plugin.name == name:
return plugin
return None
@staticmethod
def list_plugins():
return [plugin.name for plugin in Plugin.registry]
@staticmethod
def get_plugin_count():
return len(Plugin.registry)
@staticmethod
def list_enabled_plugins():
return [plugin.name for plugin in Plugin.registry if plugin.enabled]
@classmethod
def clear_registry(cls):
cls.registry.clear()
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def get_info(self):
return {
'name': self.name,
'version': self.version,
'description': self.description,
'enabled': self.enabled
}
def __str__(self):
status = "Enabled" if self.enabled else "Disabled"
return f"Plugin(name='{self.name}', version='{self.version}', {status})"
def __repr__(self):
return f"Plugin('{self.name}', '{self.version}', '{self.description}')"
# Test our plugin registry system:
print("Testing Plugin Registry System:")
print(f"Initial plugin count: {Plugin.get_plugin_count()}")
# Create plugins automatically registered
plugin1 = Plugin("Database Connector", "2.1", "Connects to various databases")
plugin2 = Plugin("Authentication", "1.5", "Handles user authentication")
plugin3 = Plugin("Logging", "3.0", "Advanced logging capabilities")
print(f"After creating 3 plugins: {Plugin.get_plugin_count()}")
print(f"All plugins: {Plugin.list_plugins()}")
# Test manual registration
plugin4 = Plugin("Cache Manager", "1.2", "Manages application cache")
Plugin.register_plugin(plugin4) # Already registered, should not duplicate
print(f"After manual registration: {Plugin.get_plugin_count()}")
# Test plugin information
print(f"\nPlugin details:")
for name in Plugin.list_plugins():
plugin = Plugin.find_plugin(name)
print(f" {plugin}")
# Test enable/disable functionality
plugin2.disable()
print(f"\nEnabled plugins: {Plugin.list_enabled_plugins()}")
print(f"All plugins: {Plugin.list_plugins()}")
# Test finding plugins
found_plugin = Plugin.find_plugin("Database Connector")
if found_plugin:
print(f"\nFound plugin: {found_plugin.get_info()}")
# Test unregistration
Plugin.unregister_plugin(plugin3)
print(f"\nAfter unregistering Logging plugin: {Plugin.list_plugins()}")
print(f"Final plugin count: {Plugin.get_plugin_count()}")
# What we accomplished in this step:
# - Created and tested our complete plugin registry system
# - Demonstrated automatic and manual registration
# - Showed plugin management functionality
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Plugin architecture and registry patterns
# - Class attributes for shared state management
# - Automatic registration in constructors
# - Class methods vs static methods usage
# - Plugin lifecycle management (enable/disable)
# - Information retrieval and plugin discovery
#
# Try it yourself:
# 1. Start with Step 1 and code along
# 2. Test each step before moving to the next
# 3. Understand WHY each step is necessary
# 4. Experiment with modifications (try adding plugin dependencies or loading order!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================