Completed
Branch master (df357f)
by Chris
12:05
created

abydos.util.Rational.__init__()   D

Complexity

Conditions 13

Size

Total Lines 62
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 31
nop 3
dl 0
loc 62
rs 4.2
c 0
b 0
f 0

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 abydos.util.Rational.__init__() 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
# -*- coding: utf-8 -*-
2
3
# Copyright 2014-2018 by Christopher C. Little.
4
# This file is part of Abydos.
5
#
6
# Abydos is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# Abydos is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with Abydos. If not, see <http://www.gnu.org/licenses/>.
18
19
"""abydos.util.
20
21
The util module defines various utility functions for other modules within
22
Abydos, including:
23
24
    - prod -- computes the product of a collection of numbers (akin to sum)
25
    - jitter -- adds small random noise to each member of a list of numbers
26
        (ported from R's jitter function)
27
"""
28
29
from operator import mul
30
31
from six.moves import reduce
32
33
34
def prod(nums):
35
    """Return the product of nums.
36
37
    The product is Π(nums).
38
39
    Cf. https://en.wikipedia.org/wiki/Product_(mathematics)
40
41
    :param nums: a collection (list, tuple, set, etc.) of numbers
42
    :returns: the product of a nums
43
44
    >>> prod([1,1,1,1])
45
    1
46
    >>> prod((2,4,8))
47
    64
48
    >>> prod({1,2,3,4})
49
    24
50
    >>> prod(2**i for i in range(5))
51
    1024
52
    """
53
    return reduce(mul, nums, 1)
54