one-file-projects/pivottable.py

39 lines
1.2 KiB
Python
Raw Normal View History

2014-11-04 13:02:25 +01:00
import urllib.request
dataurl = "https://gist.githubusercontent.com/coderd00d/ca718df8e633285885fa/raw/eb4d0bb084e71c78c68c66e37e07b7f028a41bb6/windfarm.dat"
class Data(object):
days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
def __init__(self):
self.data = {}
def addData(self, tower, day, energy):
if tower not in self.data:
self.data[tower] = { day : 0 for day in self.days }
self.data[tower][day] += energy
def __str__(self):
s = "| Tower | " + " | ".join(self.days) + " |\n"
for tower in sorted(self.data.keys()):
s += ("| %-5d | " % tower) + " | ".join(["%5d" % self.data[tower][day] for day in self.days]) + " |\n"
return s
@staticmethod
def get_from_url(url):
data = Data()
req = urllib.request.urlopen(url)
rawdata = req.read().decode("utf-8")
req.close()
for line in rawdata.split("\n"):
try:
(tower, day, energy) = line.split(" ")
data.addData(int(tower),day,int(energy))
except Exception:
pass
return data
if __name__ == "__main__":
print(str(Data.get_from_url(dataurl)))