-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.py
More file actions
64 lines (49 loc) · 2.02 KB
/
factory.py
File metadata and controls
64 lines (49 loc) · 2.02 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
import json
from abc import ABC, abstractmethod
from typing import Union
class Feature(ABC):
""" An ABC and traditional Factory rolled into one """
_registry = {}
def __init_subclass__(cls, /, feature_type: str, **kwargs):
"""
identical to the registry part of traditional factory,
EXCEPT it simplifies the process and auto-registers all subclasses, very much inline with DRY
"""
super().__init_subclass__(**kwargs)
cls._registry[feature_type] = cls
def __new__(cls, config: Union[str, dict] = None, **kwargs):
""" essentially a factory """
# processing config, either path to json file, or dict, or in kwargs
if config and isinstance(config, str):
with open(config, 'r') as f:
config = json.load(f)
config = config or kwargs
# get feature_type and select subclass
feature_type = config.get('feature_type')
subclass = cls._registry.get(feature_type)
if not subclass:
raise NotImplementedError(f'subclass of {feature_type=} is not implemented')
# create subclass obj
obj = object.__new__(subclass)
# modify subclass obj
# the following could be moved to __init__
# however, since config is already processed here, DRY is violated if the processing is repeated in __init__
# note: if __init__ is defined, the same arguments will be passed into __init__ after __new__ is called
obj.config = config
return obj
@abstractmethod
def print(self):
pass
class FeatureA(Feature, feature_type='A'):
def print(self):
print('A')
print(self.config)
class FeatureB(Feature, feature_type='B'):
def print(self):
print('B')
print(self.config)
if __name__ == '__main__':
Feature(feature_type='A', some_other_config='hello world A').print()
Feature(feature_type='B', some_other_config='hello world B').print()
from factory_separate.factory_separate_a import A
A('b').print()