0

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)
Matt Hammond
  • 765
  • 1
  • 7
  • 25
  • Please include the actual code in the question, possibly reducing it a bit to form a minimal example. – phipsgabler Oct 29 '18 at 11:28
  • Sorry, we require that you post the code **here**, not on an external site. Stack Overflow wants to collect questions for future visitors too, and we can't control the lifetime of external links. – Martijn Pieters Oct 29 '18 at 11:28
  • 2
    Take into account that Python doesn't automatically copy objects when you assign them. You are sharing a single dictionary object, not separate copies. – Martijn Pieters Oct 29 '18 at 11:28
  • 2
    That single dictionary is the `Year.month` dictionary. It is a class attribute, not an instance attribute. Create a new dictionary per instance in the `__init__` method. – Martijn Pieters Oct 29 '18 at 11:30
  • 2
    Also, don't use `dictionary.update({key: value})`. Just assign to the dictionary key; `dictionary[key] = value` is much more efficient, cleaner, and clearer as to what happens. – Martijn Pieters Oct 29 '18 at 11:33
  • @MartijnPieters Sorry new to Python. But that makes complete sense and has now fixed the problem. Thank you! – Matt Hammond Oct 29 '18 at 11:35
  • @MartijnPieters Also thank you for telling me about the key value solution! – Matt Hammond Oct 29 '18 at 11:35

0 Answers0