Passed
Pull Request — master (#496)
by
unknown
03:24
created

annif.transformer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 24.07 %

Importance

Changes 0
Metric Value
eloc 43
dl 13
loc 54
rs 10
c 0
b 0
f 0
wmc 11

3 Functions

Rating   Name   Duplication   Size   Complexity  
A get_transformer() 0 10 3
A _parse_transformer_args() 13 13 5
A parse_specs() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# TODO Add docstring
2
import re
3
from . import transformer
4
from . import inputlimiter
5
from annif.exception import ConfigurationException
6
7
8 View Code Duplication
def _parse_transformer_args(param_string):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
9
    if not param_string:
10
        return [], {}
11
    kwargs = {}
12
    posargs = []
13
    param_strings = param_string.split(',')
14
    for p_string in param_strings:
15
        parts = p_string.split('=')
16
        if len(parts) == 1:
17
            posargs.append(p_string)
18
        elif len(parts) == 2:
19
            kwargs[parts[0]] = parts[1]
20
    return posargs, kwargs
21
22
23
def parse_specs(transformers_spec):
24
    """parse a configuration definition such as 'A(x),B(y=1),C' into a tuples
25
    of ((A, [x], {}), (B, [None], {y: 1}))..."""  # TODO
26
    parsed = []
27
    # Split by commas not inside parentheses
28
    parts = re.split(r',\s*(?![^()]*\))', transformers_spec)
29
    for part in parts:
30
        match = re.match(r'(\w+)(\((.*)\))?', part)
31
        if match is None:
32
            continue
33
        transformer = match.group(1)
34
        posargs, kwargs = _parse_transformer_args(match.group(3))
35
        parsed.append((transformer, posargs, kwargs))
36
    return parsed
37
38
39
def get_transformer(transformer_specs, project):
40
    transformer_defs = parse_specs(transformer_specs)
41
    transformer_classes = []
42
    args = []
43
    for trans, posargs, kwargs in transformer_defs:
44
        if trans not in _transformers:
45
            raise ConfigurationException(f"No such transformer {trans}")
46
        transformer_classes.append(_transformers[trans])
47
        args.append((posargs, kwargs))
48
    return transformer.Transformer(transformer_classes, args, project)
49
50
51
_transformers = {
52
    'pass': transformer.IdentityTransformer,
53
    'limit_input': inputlimiter.InputLimiter}
54