def cmp_rep(x, y): if x == y: return 0 # 'all' age group is last if x == 'all' or y == 'all': return 1 if x == 'all' else -1 # compare minimum age for group to sort xa_min = REPORTS_AGE_GROUPS.get(x, (100, 0))[0] ya_min = REPORTS_AGE_GROUPS.get(y, (100, 0))[0] return 1 if xa_min > ya_min else -1
def get_age_group(sub): ''' return age group (key str) of a given submission based on age''' for key, age_range in REPORTS_AGE_GROUPS.items(): if (sub.get(u'general_information_age', 0) in range(age_range[0], age_range[1] + 1)): return key return u"all"
def init_reports(): ''' A reports container with initial values ''' reports = {} for age_group in REPORTS_AGE_GROUPS.keys(): reports[age_group] = {} for sex in ('male', 'female', 'total'): reports[age_group][sex] = {} for level in REPORTS_READING_LEVELS.keys() \ + REPORTS_NUMERACY_LEVELS.keys() + ['total']: reports[age_group][sex][level] = {'nb': 0, 'percent': None} return reports