def get_stats()

in aristotle/aristotle.py [0:0]


    def get_stats(self, key, keyonly=False, sids=None, include_empty_substat=False):
        """Returns string of statistics (total, enabled, disabled) for specified key and its values.

        :param key: key to print statistics for
        :type key: string, required
        :param keyonly: only print stats for the key itself and not stats for all possible key-value pairs, defaults to `False`
        :type keyonly: bool, optional
        :param sids: list of SIDs to consider. If not provided, global list is used.
        :type sids: list, optional
        :param include_empty_substat: includes cases where substat (key-value pair) has zero results
        :parap include_empty_substat: bool, optional
        :returns: string contaning stats, suitable for printing to stdout
        :rtype: string
        :raises: `AristotleException`
        """
        retstr = ""
        sids_orig = sids
        if sids is None:
            sids = list(self.metadata_dict.keys())
        if key not in self.keys_dict.keys():
            print_warning("key '{}' not found".format(key))
            return
        total = len([sid for sid in sids
                     if key in self.metadata_dict[sid]['metadata'].keys()])
        enabled = len([sid for sid in sids
                       if key in self.metadata_dict[sid]['metadata'].keys()
                       and not self.metadata_dict[sid]['disabled']])
        disabled = total - enabled
        retstr += "{} (Total: {}; Enabled: {}; Disabled: {})\n".format(REDISH + UNDERLINE + BOLD + key + RESET, total, enabled, disabled)

        if not keyonly:
            for value in self.keys_dict[key].keys():
                # use if/else to speed things up when filter not in use
                if sids_orig is None:
                    total = len(self.keys_dict[key][value])
                    enabled = len([sid for sid in self.keys_dict[key][value] if not self.metadata_dict[sid]['disabled']])
                else:
                    total = len([s for s in sids if s in self.keys_dict[key][value]])
                    enabled = len([sid for sid in self.keys_dict[key][value] if sid in sids and not self.metadata_dict[sid]['disabled']])
                disabled = total - enabled
                if include_empty_substat or total > 0:
                    retstr += "\t{} (Total: {}; Enabled: {}; Disabled: {})\n".format(ORANGE + value + RESET, total, enabled, disabled)
            retstr += "\n"
        return retstr