wily\operators\maintainability.py
-- -- --- --- --- --- --- --- ------- ------- ------- | """
-- -- --- --- --- --- --- --- ------- ------- ------- | Maintainability operator.
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | Measures the "maintainability" using the Halstead index.
-- -- --- --- --- --- --- --- ------- ------- ------- | """
-- -- --- --- --- --- --- --- ------- ------- ------- | import statistics
-- -- --- --- --- --- --- --- ------- ------- ------- | from collections import Counter
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | import radon.cli.harvest as harvesters
-- -- --- --- --- --- --- --- ------- ------- ------- | from radon.cli import Config
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- | from wily import logger
-- -- --- --- --- --- --- --- ------- ------- ------- | from wily.operators import BaseOperator, MetricType, Metric
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def mode(data):
-- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
-- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Return the modal value of a iterable with discrete values.
-- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
-- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | If there is more than 1 modal value, arbritrarily return the first top n.
-- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
-- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | c = Counter(data)
-- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | mode, freq = c.most_common(1)[0]
-- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return mode
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
03 -- --- --- --- --- --- --- ------- ------- ------- | class MaintainabilityIndexOperator(BaseOperator):
03 -- --- --- --- --- --- --- ------- ------- ------- | """MI Operator."""
03 -- --- --- --- --- --- --- ------- ------- ------- |
03 -- --- --- --- --- --- --- ------- ------- ------- | name = "maintainability"
03 -- --- --- --- --- --- --- ------- ------- ------- | defaults = {
03 -- --- --- --- --- --- --- ------- ------- ------- | "exclude": None,
03 -- --- --- --- --- --- --- ------- ------- ------- | "ignore": None,
03 -- --- --- --- --- --- --- ------- ------- ------- | "min": "A",
03 -- --- --- --- --- --- --- ------- ------- ------- | "max": "C",
03 -- --- --- --- --- --- --- ------- ------- ------- | "multi": True,
03 -- --- --- --- --- --- --- ------- ------- ------- | "show": False,
03 -- --- --- --- --- --- --- ------- ------- ------- | "sort": False,
03 -- --- --- --- --- --- --- ------- ------- ------- | "include_ipynb": True,
03 -- --- --- --- --- --- --- ------- ------- ------- | "ipynb_cells": True,
03 -- --- --- --- --- --- --- ------- ------- ------- | }
03 -- --- --- --- --- --- --- ------- ------- ------- |
03 -- --- --- --- --- --- --- ------- ------- ------- | metrics = (
03 -- --- --- --- --- --- --- ------- ------- ------- | Metric("rank", "Maintainability Ranking", str, MetricType.Informational, mode),
03 -- --- --- --- --- --- --- ------- ------- ------- | Metric(
03 -- --- --- --- --- --- --- ------- ------- ------- | "mi", "Maintainability Index", float, MetricType.AimLow, statistics.mean
03 -- --- --- --- --- --- --- ------- ------- ------- | ),
03 -- --- --- --- --- --- --- ------- ------- ------- | )
03 -- --- --- --- --- --- --- ------- ------- ------- |
03 -- --- --- --- --- --- --- ------- ------- ------- | default_metric_index = 1 # MI
03 -- --- --- --- --- --- --- ------- ------- ------- |
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def __init__(self, config, targets):
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Instantiate a new MI operator.
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | :param config: The wily configuration.
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | :type config: :class:`WilyConfig`
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | # TODO : Import config from wily.cfg
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | logger.debug(f"Using {targets} with {self.defaults} for MI metrics")
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
03 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | self.harvester = harvesters.MIHarvester(targets, config=Config(**self.defaults))
03 -- --- --- --- --- --- --- ------- ------- ------- |
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def run(self, module, options):
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | Run the operator.
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | :param module: The target module path.
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | :type module: ``str``
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | :param options: Any runtime options.
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | :type options: ``dict``
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 |
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | :return: The operator results.
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | :rtype: ``dict``
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | """
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | logger.debug("Running maintainability harvester")
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | results = {}
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | for filename, metrics in dict(self.harvester.results).items():
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | results[filename] = {"total": metrics}
03 02 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return results