Passed
Push — mpeta ( 92227f...db1c73 )
by Konstantinos
01:53
created

so_magic.data.command_factories.GenericCommandFactory.construct()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 3
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
from abc import ABC
2
import attr
3
from so_magic.utils import Command, Subject
4
from .command_factory_interface import CommandFactoryInterface
5
6
7
class DataManagerCommandFactory(CommandFactoryInterface, ABC):
8
9
    subclasses = {}
10
11
    @classmethod
12
    def register_as_subclass(cls, factory_type):
13
        def wrapper(subclass):
14
            cls.subclasses[factory_type] = subclass
15
            return subclass
16
        return wrapper
17
18
    @classmethod
19
    def create(cls, factory_type, *args, **kwargs):
20
        if factory_type not in cls.subclasses:
21
            raise ValueError('Bad "Factory type" \'{}\''.format(factory_type))
22
        return cls.subclasses[factory_type](*args, **kwargs)
23
24
    @classmethod
25
    def create_factory(cls, name, callback):
26
        @DataManagerCommandFactory.register_as_subclass(name)
27
        class DataManagerRuntimeCommandFactory(DataManagerCommandFactory):
28
            def construct(self, *args, **kwargs) -> Command:
29
                receiver = args[0]
30
                def command(*runtime_args):
31
                    callback(receiver, *runtime_args)
32
                return Command(command, '__call__', *args[1:])
33
34
35
#### Register command factories
36
@DataManagerCommandFactory.register_as_subclass('select_variables')
37
class SelectVariablesCommandFactory(DataManagerCommandFactory):
38
39
    def construct(self, *args, **kwargs) -> Command:
40
        def command(variables):
41
            args[0].feature_manager.feature_configuration = variables
42
        return Command(command, '__call__', *args[1:])
43
44
45
#################
46
47
@attr.s
48
class MegaCommandFactory(Subject):
49
    _data_manager = attr.ib(init=True)
50
    command_factory = attr.ib(init=True, default=DataManagerCommandFactory)
51
52
    def __call__(self, command_type, *args, **kwargs):
53
        self._state, self.name = self.command_factory.create(command_type).construct(self._data_manager, *args, **kwargs), command_type
54
        self.notify()
55
        return self._state
56
57
58
59
# Legacy Code
60
61
# @BaseCommandFactory.register_as_subclass('encode_nominal_subsets')
62
# class NominalAttributeListEncodeCommandFactory(AbstractCommandFactory):
63
64
#     def construct(self, *args, **kwargs) -> Command:
65
#         from so_magic.data.features.phis import ListOfCategoricalPhi, DatapointsAttributePhi
66
#         assert len(args) > 0
67
#         datapoints = args[0]
68
#         attribute = args[1]
69
#         new_attribute = args[2]
70
#         def _command(_datapoints, _attribute, _new_attribute):
71
#             phi = ListOfCategoricalPhi(DatapointsAttributePhi(_datapoints))
72
#             new_values = phi(_attribute)
73
#             _datapoints.mutator.add_column(_datapoints, new_values, _new_attribute)
74
#         return Command(_command, '__call__', datapoints, attribute, new_attribute)
75