Skip to content

Commit 37e48a7

Browse files
committed
Separate registry per python version
1 parent 4f647ad commit 37e48a7

4 files changed

Lines changed: 100 additions & 52 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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 |

patterns/behavioral/registry.py

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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)

0 commit comments

Comments
 (0)