| Conditions | 17 |
| Total Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
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 |
||
| 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 | |||
| 136 |