-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicc.py
More file actions
27 lines (15 loc) · 747 Bytes
/
icc.py
File metadata and controls
27 lines (15 loc) · 747 Bytes
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
"""super-simple inversion of control container; see pymple for something a bit more powerful."""
import typing as tg
_iccdict = dict() # maps abstract type to implementation type
class IccKeyError(KeyError):
pass
def register_class(abstractclass: type, implemclass: type):
_iccdict[abstractclass] = implemclass
def get(abstractclass: type) -> type:
_assert_exists(abstractclass)
return _iccdict[abstractclass]
def init(abstractclass: type, *initargs, **initkwargs) -> tg.Any:
return get(abstractclass)(*initargs, **initkwargs) # call constructor
def _assert_exists(abstractclass):
if abstractclass not in _iccdict:
raise IccKeyError(f"no implementation is known for abstract class '{abstractclass}'")