diff --git a/resources/stat_functions.py b/resources/stat_functions.py index c89828366848248c328c739a487c77cc16cdfbff..30788d6b4fe019633a43342aba618bd4d67fa35c 100644 --- a/resources/stat_functions.py +++ b/resources/stat_functions.py @@ -23,11 +23,12 @@ class Aggregations(): Forms aggregation functions on a single dict attribute for all present records. """ @staticmethod - def all(struct, attribute, display=True): + def all(struct, attribute, null_values=None, display=True): """ Finds all available aggregations of the attribute (key) for the provided dict. :param struct: Data structure to aggregate on. :param attribute: Attribute to aggregate. + :param null_values: Values to count as null/empty for attribute. :param display: Bool indicating if helper text should display. :return: All available aggregations of dict attribute. """ @@ -36,6 +37,13 @@ class Aggregations(): # Run all aggregations and store to dictionary. agg_dict = {} + agg_dict['count'] = Aggregations.count(struct, attribute, display=display) + agg_dict['count_not_null'] = Aggregations.count_not_null( + struct, + attribute, + null_values=null_values, + display=display, + ) agg_dict['min'] = Aggregations.min(struct, attribute, display=display) agg_dict['max'] = Aggregations.max(struct, attribute, display=display) agg_dict['sum'] = Aggregations.sum(struct, attribute, display=display) @@ -54,6 +62,53 @@ class Aggregations(): # Return aggregations. return agg_dict + @staticmethod + def count(struct, attribute, display=True): + """ + Finds the count of the attribute (key) for the provided dict. + :param struct: Data structure to aggregate on. + :param attribute: Attribute to aggregate. + :param display: Bool indicating if helper text should display. + :return: Record count of dict attribute. + """ + if display: + logger.info('Finding record count of "{0}".'.format(attribute)) + + # Return aggregation. + return len(struct) + + @staticmethod + def count_not_null(struct, attribute, null_values=None, display=True): + """ + Finds the count of non-empty items of the attribute (key) for the provided dict. + :param struct: Data structure to aggregate on. + :param attribute: Attribute to aggregate. + :param null_values: Values to count as null/empty for attribute. + :param display: Bool indicating if helper text should display. + :return: Record count of non-empty items of dict attribute. + """ + if display: + logger.info('Finding non-empty record count of "{0}".'.format(attribute)) + + # Get list of only attribute. + attr_list = [x[attribute] for x in struct] + valid_count = 0 + + # Check type of null_values variable. + if isinstance(null_values, list) or isinstance(null_values, tuple): + # Is list or tuple. + for item in attr_list: + if item not in null_values: + valid_count += 1 + else: + # Is single item. + for item in attr_list: + if item != null_values: + valid_count += 1 + + # Return aggregation. + return valid_count + @staticmethod def min(struct, attribute, display=True): """