uom.cmd_line.cmd_convert()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 61
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 4.0011

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 61
ccs 23
cts 24
cp 0.9583
rs 9.016
c 0
b 0
f 0
cc 4
nop 1
crap 4.0011

How to fix   Long Method   

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:

1
"""Command line option."""
2
3 1
from __future__ import absolute_import
4
5 1
from argparse import ArgumentParser
6
7 1
from colorlog import DEBUG, INFO, ColoredFormatter, StreamHandler, debug, getLogger
8
9 1
from uom import base_unit, convert
10
11
12 1
def cmd_convert(arg=None):
13
    """Convert value."""
14 1
    logger = getLogger()
15
16 1
    handler = StreamHandler()
17 1
    handler.setFormatter(
18
        ColoredFormatter(
19
            "%(log_color)s%(asctime)s [%(levelname)s]: %(message)s",
20
            datefmt=None,
21
            reset=True,
22
            log_colors={
23
                "DEBUG": "green",
24
                "INFO": "cyan",
25
                "WARNING": "yellow",
26
                "ERROR": "red",
27
                "CRITICAL": "red,bg_white",
28
            },
29
            secondary_log_colors={},
30
            style="%",
31
        )
32
    )
33
34 1
    logger.propagate = False
35 1
    logger.handlers.clear()
36 1
    logger.addHandler(handler)
37 1
    logger.setLevel(INFO)
38
39
    # debug("Debug 1")
40
    # info("Info 1")
41
42 1
    parser = ArgumentParser(prog="uom_convert_value")
43
44 1
    parser.add_argument("value", type=float, nargs="+", help="value to be converted")
45
46 1
    parser.add_argument("-s", dest="source", help="unit source")
47
48 1
    parser.add_argument("-t", dest="target", help="unit target")
49
50 1
    parser.add_argument("-v", dest="verbose", action="store_true", help="verbose")
51
52 1
    if arg is not None:
53 1
        args = parser.parse_args(arg.split())
54
    else:
55
        args = parser.parse_args()
56
57 1
    if args.verbose:
58 1
        logger.setLevel(DEBUG)
59
60
        # debug("Debug 2")
61
        # info("Info 2")
62
63 1
        debug(args)
64
65 1
    if len(args.value) == 1:
66 1
        out = convert(args.value[0], args.source, args.target)
67
    else:
68 1
        out = convert(args.value, args.source, args.target)
69
70 1
    debug(f"Output: {out}")
71
72 1
    return out
73
74
75 1
def cmd_base_unit(arg=None):
76
    """Base unit."""
77 1
    parser = ArgumentParser(prog="uom_base_unit")
78
79 1
    parser.add_argument("unit", help="input unit")
80
81 1
    parser.add_argument("-v", dest="verbose", action="store_true", help="verbose")
82
83 1
    if arg is not None:
84 1
        args = parser.parse_args(arg.split())
85
    else:
86
        args = parser.parse_args()
87
88 1
    debug(args)
89
90 1
    out = base_unit(args.unit)
91
92 1
    debug(f"Output: {out}")
93
94
    return out
95