from datetime import datetime ''' Note: Hard-Coding "Months" and "Days" Sets ====================================================================== I have hardcoded the "months" and "days" sets because they are fixed values -- unless something monumental happens scientifically or politically. On top of that, this makes the graphs easier to read because they are in chronological order. This is not guaranteed if the "keys" for "months" and "sets" are formed from the data-object this function receives. Unfortunately, I cannot do the same for years. That will continue to grow as the years roll on through here -- unless something monumental happens scientifically or politically. This code is intended to be used in graphs -- in co-data project. ''' def get_creation_date_totals(data): years = {} months = {"1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0,"10": 0, "11": 0, "12": 0} days = {"1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0,"10": 0, "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "20": 0, "21": 0,"22": 0, "23": 0, "24": 0, "25": 0, "26": 0, "27": 0, "28": 0, "29": 0, "30": 0, "31": 0} for item in data: ft = datetime.fromisoformat(item["dateCreated"]) if str(ft.year) in years: years[str(ft.year)] += 1 else: years[str(ft.year)] = 1 if str(ft.month) in months: months[str(ft.month)] += 1 else: months[str(ft.month)] = 1 if str(ft.day) in days: days[str(ft.day)] += 1 else: days[str(ft.day)] = 1 return [years, months, days] def get_category_totals(data): categories = {} for item in data: cat = item["category"] ''' The join and split is because the data returned from the A.P.I. call contains a lot of white spaces. This just cleans it up. The white space was, also, making the chart render incorrectly. ''' cat = ''.join(cat.split()) if cat in categories: total = categories.get(cat) categories[cat] = total + 1 else: categories[cat] = 1 return categories def get_db_column_totals(data, column_name): column_data = {} for item in data: col = item[column_name] col = " ".join(col.split()) # print(col) if col in column_data: total = column_data.get(col) column_data[col] = total + 1 else: column_data[col] = 1 return column_data def get_dimension_totals(data, column_name, dimension): dimensions = {} for item in data: dim = item[column_name] distance = dim[dimension]["value"]["distance"] if distance is not None: w = str(distance) if w not in dimensions: dimensions[str(distance)] = 1 else: total = dimensions.get(str(distance)) dimensions[str(distance)] = total + 1 return dimensions