I'm trying to make the python loop through JSON and break the data down into a Year, Month and day overview count. So I have created a Loop that goes through and creates a Year class for each year that the data comes across and then adds one to the follower count.
The problem arise when creating the months inside the year class. It seems to effect every class and not the year class that I have selected. I know I haven't explained this very well but if you look at the data below and the output you can see that 01 has been set twice even though the data has it set in two different years.
Code
Code with Online IDE : https://repl.it/repls/DefenselessNeatLifecycle
Test Data
{
"followers": {
"user1": "2018-10-25T08:44:32",
"user2": "2017-01-25T08:44:03",
"user3": "2017-03-25T08:43:22",
"user4": "2016-01-25T08:40:24",
"user5": "2015-12-25T08:40:06",
"user6": "2015-12-21T08:40:06"
}
}
Output
2015 2
2015 {u'03': 1, u'12': 2, u'01': 2, u'10': 1}
2016 1
2016 {u'03': 1, u'12': 2, u'01': 2, u'10': 1}
2017 2
2017 {u'03': 1, u'12': 2, u'01': 2, u'10': 1}
2018 1
2018 {u'03': 1, u'12': 2, u'01': 2, u'10': 1}
What the output should show
2015 2
2015 {u'12': 2}
2016 1
2016 {u'01': 1}
2017 2
2017 {u'01': 1, u'03': 1}
2018 1
2018 {u'10': 1}
main.py
import json
import operator
from pprint import pprint
from year import Year
json_reference = "test-data.json"
p1 = {}
with open(json_reference, 'r') as f:
json_data = json.load(f)
def report_complete(a):
print("100%")
for year in sorted(p1):
# Print Top Line Year Totals
print(year, p1[year].follower_count)
# Print Top Line Month Totals
print(year, p1[year].months)
def percent_report(loop):
follower_total = len(json_data["followers"])
percentage_complete = int(loop / float(follower_total) * 100)
# print "{0:.0f}%".format(percentage_complete)
for i, lines in enumerate(json_data["followers"]):
joined_date = json_data["followers"][lines]
year = joined_date[0:4]
month = joined_date[5:7]
if year in p1:
p1[year].add_follower(month)
else:
anew = Year(year)
anew.add_follower(month)
p1.update({year: anew})
if i % 10000 == 0:
percent_report(i)
if i + 1 == len(json_data["followers"]):
report_complete(i)
Year Class
class Year:
follower_count = 0
months = {}
def __init__(self, year):
self.year = year
def create_new_month(self, month):
self.months.update({month: 1})
def add_follower(self, month):
self.follower_count += 1
if month in self.months:
self.months[month] += 1
# print self.months[month]
else:
self.create_new_month(month)
print(month)