File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ __Behavioral Patterns__:
4646| [ memento] ( patterns/behavioral/memento.py ) | generate an opaque token that can be used to go back to a previous state |
4747| [ observer] ( patterns/behavioral/observer.py ) | provide a callback for notification of events/changes to data |
4848| [ publish_subscribe] ( patterns/behavioral/publish_subscribe.py ) | a source syndicates events/data to 0+ registered listeners |
49- | [ registry] ( patterns/behavioral/registry .py ) | keep track of all subclasses of a given class |
49+ | [ registry] ( patterns/behavioral/registry__py3 .py ) | keep track of all subclasses of a given class |
5050| [ specification] ( patterns/behavioral/specification.py ) | business rules can be recombined by chaining the business rules together using boolean logic |
5151| [ state] ( patterns/behavioral/state.py ) | logic is organized into a discrete number of potential states and the next state that can be transitioned to |
5252| [ strategy] ( patterns/behavioral/strategy.py ) | selectable operations over the same data |
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+
5+ class RegistryHolder (type ):
6+
7+ REGISTRY = {}
8+
9+ def __new__ (cls , name , bases , attrs ):
10+ new_cls = type .__new__ (cls , name , bases , attrs )
11+ """
12+ Here the name of the class is used as key but it could be any class
13+ parameter.
14+ """
15+ cls .REGISTRY [new_cls .__name__ ] = new_cls
16+ return new_cls
17+
18+ @classmethod
19+ def get_registry (cls ):
20+ return dict (cls .REGISTRY )
21+
22+
23+ class BaseRegisteredClass (object ):
24+ """
25+ Any class that will inherits from BaseRegisteredClass will be included
26+ inside the dict RegistryHolder.REGISTRY, the key being the name of the
27+ class and the associated value, the class itself.
28+ """
29+ __metaclass__ = RegistryHolder
30+
31+
32+ def main ():
33+ """
34+ Before subclassing
35+ >>> sorted(RegistryHolder.REGISTRY)
36+ ['BaseRegisteredClass']
37+
38+ >>> class ClassRegistree(BaseRegisteredClass):
39+ ... def __init__(self, *args, **kwargs):
40+ ... pass
41+
42+ After subclassing
43+ >>> sorted(RegistryHolder.REGISTRY)
44+ ['BaseRegisteredClass', 'ClassRegistree']
45+ """
46+
47+
48+ if __name__ == "__main__" :
49+ import doctest
50+ doctest .testmod (optionflags = doctest .ELLIPSIS )
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+
5+ class RegistryHolder (type ):
6+
7+ REGISTRY = {}
8+
9+ def __new__ (cls , name , bases , attrs ):
10+ new_cls = type .__new__ (cls , name , bases , attrs )
11+ """
12+ Here the name of the class is used as key but it could be any class
13+ parameter.
14+ """
15+ cls .REGISTRY [new_cls .__name__ ] = new_cls
16+ return new_cls
17+
18+ @classmethod
19+ def get_registry (cls ):
20+ return dict (cls .REGISTRY )
21+
22+
23+ class BaseRegisteredClass (metaclass = RegistryHolder ):
24+ """
25+ Any class that will inherits from BaseRegisteredClass will be included
26+ inside the dict RegistryHolder.REGISTRY, the key being the name of the
27+ class and the associated value, the class itself.
28+ """
29+
30+
31+ def main ():
32+ """
33+ Before subclassing
34+ >>> sorted(RegistryHolder.REGISTRY)
35+ ['BaseRegisteredClass']
36+
37+ >>> class ClassRegistree(BaseRegisteredClass):
38+ ... def __init__(self, *args, **kwargs):
39+ ... pass
40+
41+ After subclassing
42+ >>> sorted(RegistryHolder.REGISTRY)
43+ ['BaseRegisteredClass', 'ClassRegistree']
44+ """
45+
46+
47+ if __name__ == "__main__" :
48+ import doctest
49+ doctest .testmod (optionflags = doctest .ELLIPSIS )
You can’t perform that action at this time.
0 commit comments