import collections
import datetime


class ContinousWeek(collections.UserDict):
    def normalise(self, day, hour):
        while day < 0:
            day += 7
        while day > 6:
            day -= 7
        while hour < 0:
            hour += 24
        while hour > 23:
            hour -= 24
        return day, hour

    def set(self, day, hour, value):
        day, hour = self.normalise(day, hour)
        self.data[(day, hour)] = value

    def get(self, day, hour):
        day, hour = self.normalise(day, hour)
        return self.data.get((day, hour))


class BusynessChart:
    def __init__(self, raw_results):
        if not raw_results:
            raise ValueError('There are no results so a chart cannot be '
                             'formed.')

        self.week = ContinousWeek()

        raw_week = ContinousWeek()
        for row in raw_results:
            raw_week.set(int(row[0]), int(row[1]), row[2])

        average = sum(raw_week.data.values()) / len(raw_week.data)

        for day in range(7):
            for hour in range(24):
                busyness = raw_week.get(day, hour)

                if busyness is None:
                    # try the day before
                    for i in range(7):
                        busyness_before = raw_week.get(day - i, hour)
                        if busyness_before is not None:
                            busyness = busyness_before
                            break

                    if busyness is None:
                        # average
                        busyness = average

                self.week.set(day, hour, busyness)

    def as_list(self):
        results = []
        for day in range(7):
            for hour in range(24):
                results.append((day, hour, self.week.get(day, hour)))
        return results

    @property
    def now(self):
        d = datetime.datetime.now()
        return self.week.get(d.weekday(), d.hour)

@@ -560,16 +560,387 @@
 hour))%0A%0A
+    def next(self, day, hour, direction=None):%0A        if direction == -1:%0A            return self.previous(day, hour)%0A%0A        hour += 1%0A        if hour %3E 23:%0A            hour = 0%0A            day += 1%0A        return day, hour%0A%0A    def previous(self, day, hour):%0A        hour -= 1%0A        if hour %3C 0:%0A            hour = 23%0A            day -= 1%0A        return day, hour%0A%0A
 %0Aclass B
@@ -1170,32 +1170,37 @@
 Week()%0A%0A        
+self.
 raw_week = Conti
@@ -1254,16 +1254,21 @@
         
+self.
 raw_week
@@ -1329,16 +1329,21 @@
 e = sum(
+self.
 raw_week
@@ -1364,16 +1364,21 @@
 ) / len(
+self.
 raw_week
@@ -1385,16 +1385,97 @@
 .data)%0A%0A
+        self.raw_week2 = ContinousWeek()%0A%0A        # first fill from previous day%0A
         
@@ -1557,16 +1557,21 @@
 yness = 
+self.
 raw_week
@@ -1628,49 +1628,8 @@
 ne:%0A
-                    # try the day before%0A
     
@@ -1705,16 +1705,21 @@
 efore = 
+self.
 raw_week
@@ -1904,110 +1904,616 @@
     
-    if busyness is None:%0A                        # average%0A                        busyness = average%0A
+self.raw_week2.set(day, hour, busyness)%0A%0A        self.raw_week = self.raw_week2%0A%0A        # next interpolate%0A        for day in range(7):%0A            for hour in range(24):%0A                busyness = self.raw_week.get(day, hour)%0A                if busyness is None:%0A                    busyness = self.interpolate(day, hour)%0A%0A                self.week.set(day, hour, busyness)%0A%0A    def interpolate(self, day, hour):%0A        def find_value(day, hour, direction):%0A            distance = 0%0A            while True:%0A                distance += 1%0A                day, hour = self.raw_week.next(day, hour, direction)
 %0A   
@@ -2528,43 +2528,380 @@
     
+ value =
  self.
+raw_
 week.
-set(day, hour, busyness)
+get(day, hour)%0A                if value is not None:%0A                    return float(value), distance%0A%0A        left_value, left_distance = find_value(day, hour, -1)%0A        right_value, right_distance = find_value(day, hour, +1)%0A%0A        a = left_distance / (left_distance + right_distance + 1)%0A        return left_value * (1 - a) + right_value * a
 %0A%0A  

Add chart interpolation