-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory_utils.py
36 lines (31 loc) · 1.14 KB
/
factory_utils.py
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
from typing import Any, Dict, Optional, Tuple
def get_factory_adder() -> Tuple[Any, Dict[str, Any]]:
"""Get a function that adds a class to a list and the corresponding
list. Useful for creating a factory with a list of classes. The
intended use is as a decorator.
You can also can specify a different name for the class in the list,
to use it at creation time instead of the class name.
Example:
>>> add_class, classes_dict = get_factory_adder()
>>> @add_class
... class A:
... pass
>>> @add_class('Cc')
... class C:
... pass
"""
classes_dict = {}
def _add_class(class_: Any, name: Optional[str]=None) -> Any:
if name is None:
name = class_.__name__
classes_dict[name] = class_
return class_
def add_class(class_: Any, name: Optional[str]=None) -> Any:
if not callable(class_):
name = class_
def wrapper(class_: Any) -> Any:
return _add_class(class_, name)
return wrapper
else:
return _add_class(class_)
return add_class, classes_dict