assert_is_valid_key()   F
last analyzed

Complexity

Conditions 17

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
c 0
b 0
f 0
dl 0
loc 53
rs 1.8

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like assert_is_valid_key() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python
2
# coding=utf-8
3
from __future__ import division, print_function, unicode_literals
4
5
import jsonpickle.tags
6
7
from sacred import SETTINGS
8
import sacred.optional as opt
9
from sacred.config.custom_containers import DogmaticDict, DogmaticList
10
from sacred.utils import PYTHON_IDENTIFIER, basestring
11
12
13
def assert_is_valid_key(key):
14
    """
15
    Raise KeyError if a given config key violates any requirements.
16
17
    The requirements are the following and can be individually deactivated
18
    in ``sacred.SETTINGS.CONFIG_KEYS``:
19
      * ENFORCE_MONGO_COMPATIBLE (default: True):
20
        make sure the keys don't contain a '.' or start with a '$'
21
      * ENFORCE_JSONPICKLE_COMPATIBLE (default: True):
22
        make sure the keys do not contain any reserved jsonpickle tags
23
        This is very important. Only deactivate if you know what you are doing.
24
      * ENFORCE_STRING (default: False):
25
        make sure all keys are string.
26
      * ENFORCE_VALID_PYTHON_IDENTIFIER (default: False):
27
        make sure all keys are valid python identifiers.
28
29
    Parameters
30
    ----------
31
    key:
32
      The key that should be checked
33
34
    Raises
35
    ------
36
    KeyError:
37
      if the key violates any requirements
38
39
    """
40
    if SETTINGS.CONFIG.ENFORCE_KEYS_MONGO_COMPATIBLE and (
41
            isinstance(key, basestring) and ('.' in key or key[0] == '$')):
42
        raise KeyError('Invalid key "{}". Config-keys cannot '
43
                       'contain "." or start with "$"'.format(key))
44
45
    if SETTINGS.CONFIG.ENFORCE_KEYS_JSONPICKLE_COMPATIBLE and \
46
            isinstance(key, basestring) and (
47
            key in jsonpickle.tags.RESERVED or key.startswith('json://')):
48
        raise KeyError('Invalid key "{}". Config-keys cannot be one of the'
49
                       'reserved jsonpickle tags: {}'
50
                       .format(key, jsonpickle.tags.RESERVED))
51
52
    if SETTINGS.CONFIG.ENFORCE_STRING_KEYS and (
53
            not isinstance(key, basestring)):
54
        raise KeyError('Invalid key "{}". Config-keys have to be strings, '
55
                       'but was {}'.format(key, type(key)))
56
57
    if SETTINGS.CONFIG.ENFORCE_VALID_PYTHON_IDENTIFIER_KEYS and (
58
            isinstance(key, basestring) and not PYTHON_IDENTIFIER.match(key)):
59
        raise KeyError('Key "{}" is not a valid python identifier'
60
                       .format(key))
61
62
    if SETTINGS.CONFIG.ENFORCE_KEYS_NO_EQUALS and (
63
            isinstance(key, basestring) and '=' in key):
64
        raise KeyError('Invalid key "{}". Config keys may not contain an'
65
                       'equals sign ("=").'.format('='))
66
67
68
def normalize_numpy(obj):
69
    if opt.has_numpy and isinstance(obj, opt.np.generic):
70
        try:
71
            return opt.np.asscalar(obj)
72
        except ValueError:
73
            pass
74
    return obj
75
76
77
def normalize_or_die(obj):
78
    if isinstance(obj, dict):
79
        res = dict()
80
        for key, value in obj.items():
81
            assert_is_valid_key(key)
82
            res[key] = normalize_or_die(value)
83
        return res
84
    elif isinstance(obj, (list, tuple)):
85
        return list([normalize_or_die(value) for value in obj])
86
    return normalize_numpy(obj)
87
88
89
def recursive_fill_in(config, preset):
90
    for key in preset:
91
        if key not in config:
92
            config[key] = preset[key]
93
        elif isinstance(config[key], dict):
94
            recursive_fill_in(config[key], preset[key])
95
96
97
def chain_evaluate_config_scopes(config_scopes, fixed=None, preset=None,
98
                                 fallback=None):
99
    fixed = fixed or {}
100
    fallback = fallback or {}
101
    final_config = dict(preset or {})
102
    config_summaries = []
103
    for config in config_scopes:
104
        cfg = config(fixed=fixed,
105
                     preset=final_config,
106
                     fallback=fallback)
107
        config_summaries.append(cfg)
108
        final_config.update(cfg)
109
110
    if not config_scopes:
111
        final_config.update(fixed)
112
113
    return undogmatize(final_config), config_summaries
114
115
116
def dogmatize(obj):
117
    if isinstance(obj, dict):
118
        return DogmaticDict({key: dogmatize(val) for key, val in obj.items()})
119
    elif isinstance(obj, list):
120
        return DogmaticList([dogmatize(value) for value in obj])
121
    elif isinstance(obj, tuple):
122
        return tuple(dogmatize(value) for value in obj)
123
    else:
124
        return obj
125
126
127
def undogmatize(obj):
128
    if isinstance(obj, DogmaticDict):
129
        return dict({key: undogmatize(value) for key, value in obj.items()})
130
    elif isinstance(obj, DogmaticList):
131
        return list([undogmatize(value) for value in obj])
132
    elif isinstance(obj, tuple):
133
        return tuple(undogmatize(value) for value in obj)
134
    else:
135
        return obj
136