from form_content import FormContent def get_data_stats(data: dict) -> dict: summed = { key: { 'sum': 0, 'total': 0, 'percent': 0 } for key in FormContent.all_keys } topic_other = set() for entry in data.values(): for key, value in dict.items(entry): if key == 'topic-other' and value is not None: topic_other |= { value } if isinstance(value, bool): _sum = summed[key]['sum'] if key in summed else 0 _sum = _sum + 1 if value is True else _sum total = summed[key]['total'] + 1 if key in summed else 1 summed[key] = { 'sum': _sum, 'total': total, 'percent': f'{((_sum / total) * 100):.1f}' } summed['topic-other'] = list(topic_other) return summed