Passed
Push — master ( 193da7...4bb259 )
by Simon
01:36 queued 11s
created

hyperactive.checks.check_args()   A

Complexity

Conditions 3

Size

Total Lines 30
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 25
nop 8
dl 0
loc 30
rs 9.28
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
6
import collections
7
import numpy as np
8
9
10
def _check_objective_function(value):
11
    if not isinstance(value, collections.Callable):
12
        raise ValueError(r"objective_function must be callable")
13
14
15
def _check_function_parameter(value):
16
    if not isinstance(value, dict):
17
        raise ValueError(r"function_parameter must be of type dict")
18
19
20
def _check_search_space(value):
21
    if not isinstance(value, dict):
22
        raise ValueError(r"search_space must be of type dict")
23
24
25
def _check_memory(value):
26
    memory_list = ["short", "long", False]
27
    if value not in memory_list:
28
        raise ValueError(r"memory must be 'short', 'long' or False")
29
30
31
def _check_optimizer(value):
32
    if not isinstance(value, (dict, str)):
33
        raise ValueError(r"optimizer must be of type dict or str")
34
35
36
def _check_n_iter(value):
37
    if not isinstance(value, int):
38
        raise ValueError(r"n_iter must be of type int")
39
40
41
def _check_n_jobs(value):
42
    if not isinstance(value, int):
43
        raise ValueError(r"n_jobs must be of type int")
44
45
46
def _check_init_para(value):
47
    if not isinstance(value, list) and value is not None:
48
        raise ValueError(r"init_para must be of type list or None")
49
50
51
def _check_distribution(value):
52
    if not isinstance(value, dict) and value is not None:
53
        raise ValueError(r"distribution must be of type dict or None")
54
55
56
arguments = {
57
    "objective_function": _check_objective_function,
58
    "function_parameter": _check_function_parameter,
59
    "search_space": _check_search_space,
60
    "memory": _check_memory,
61
    "optimizer": _check_optimizer,
62
    "n_iter": _check_n_iter,
63
    "n_jobs": _check_n_jobs,
64
    "init_para": _check_init_para,
65
    # "distribution": _check_distribution,
66
}
67
68
69
def check_args(
70
    objective_function,
71
    search_space,
72
    n_iter,
73
    function_parameter,
74
    optimizer,
75
    n_jobs,
76
    init_para,
77
    memory,
78
):
79
    kwargs = {
80
        "objective_function": objective_function,
81
        "search_space": search_space,
82
        "n_iter": n_iter,
83
        "function_parameter": function_parameter,
84
        "optimizer": optimizer,
85
        "n_jobs": n_jobs,
86
        "init_para": init_para,
87
        "memory": memory,
88
    }
89
90
    for keyword in kwargs:
91
        if keyword not in arguments:
92
            raise TypeError(
93
                "add_search got an unexpected keyword argument " + str(keyword)
94
            )
95
96
        value = kwargs[keyword]
97
        check_function = arguments[keyword]
98
        check_function(value)
99
100