Example #1
0
File: cli.py Project: b4dtR1p/radon
def mi(multi=True, exclude=None, ignore=None, show=False, *paths):
    '''Analyze the given Python modules and compute the Maintainability Index.

    The maintainability index (MI) is a compound metric, with the primary aim
    being to determine how easy it will be to maintain a particular body of
    code.

    :param -e, --exclude <str>:  Comma separated list of patterns to exclude.
    :param -i, --ignore <str>: Comma separated list of patterns to ignore.
        Radon won't even descend into those directories.
    :param -m, --multi:  If given, multiline strings are counted as comments.
    :param -s, --show:  If given, the actual MI value is shown in results.
    :param paths: The modules or packages to analyze.
    '''
    for name in iter_filenames(paths, exclude, ignore):
        with open(name) as fobj:
            try:
                result = mi_visit(fobj.read(), multi)
            except Exception as e:
                log(name, indent=1)
                log_error(e, indent=1)
                continue
            except KeyboardInterrupt:
                log(name)
                return
            rank = mi_rank(result)
            color = MI_RANKS[rank]
            to_show = '' if not show else ' ({0:.2f})'.format(result)
            log('{0} - {1}{2}{3}{4}', name, color, rank, to_show, RESET)
Example #2
0
def mi(multi=True, exclude=None, show=False, *paths):
    '''Analyze the given Python modules and compute the Maintainability Index.

    The maintainability index (MI) is a compound metric, with the primary aim
    of to determine how easy it will be to maintain a particular body of code.

    :param multi: Whether or not to count multiline strings as comments. Most
        of the time this is safe since multiline strings are used as functions
        docstrings, but one should be aware that their use is not limited to
        that and sometimes it would be wrong to count them as comment lines.
    :param paths: The modules or packages to analyze.
    '''
    for name in iter_filenames(paths, exclude):
        with open(name) as fobj:
            try:
                result = mi_visit(fobj.read(), multi)
            except Exception as e:
                log('{0}\n{1}ERROR: {2}', name, ' ' * 4, str(e))
                continue
            except KeyboardInterrupt:
                log(name)
                return
            rank = mi_rank(result)
            color = MI_RANKS[rank]
            to_show = ''
            if show:
                to_show = ' ({0:.2f})'.format(result)
            log('{0} - {1}{2}{3}{4}', name, color, rank, to_show, RESET)
Example #3
0
 def to_terminal(self):
     '''Yield lines to be printed to a terminal.'''
     for name, mi in self.results:
         if 'error' in mi:
             yield name, (mi['error'],), {'error': True}
             continue
         rank = mi_rank(mi['mi'])
         if not self.config.min <= rank <= self.config.max:
             continue
         color = MI_RANKS[rank]
         to_show = ''
         if self.config.show:
             to_show = ' ({0:.2f})'.format(mi['mi'])
         yield '{0} - {1}{2}{3}{4}', (name, color, rank, to_show, RESET), {}
Example #4
0
 def gobble(self, fobj):
     '''Analyze the content of the file object.'''
     mi = mi_visit(fobj.read(), self.config.multi)
     rank = mi_rank(mi)
     return {'mi': mi, 'rank': rank}
Example #5
0
def maintainabilityIndex(code):
    miScore = mi_visit(code, True)
    miRank = mi_rank(miScore)
    return [miScore, miRank]
Example #6
0
 def gobble(self, fobj):
     '''Analyze the content of the file object.'''
     mi = mi_visit(fobj.read(), self.config.multi)
     rank = mi_rank(mi)
     return {'mi': mi, 'rank': rank}
Example #7
0
from radon.complexity import cc_rank, cc_visit
from radon.metrics import mi_visit, mi_rank
from radon.raw import analyze

# Metrics that are usable: Maintainability Index

teststring = ('''
  `
''')


def my_function():
    print("Hello from a function")


def factorial(n):
    if n < 2: return 1
    return n * factorial(n - 1)


#print (analyze(teststring))

#print (cc_visit(teststring))

mivalue = (mi_visit(teststring, 0))

print(mivalue)
print(mi_rank(mivalue))