history.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. class History(object):
  2. """docstring for History."""
  3. def __init__(self):
  4. self.dates = []
  5. self.datescount = []
  6. def __str__(self, sep='; '):
  7. return sep.join("{0}={1}".format(self.dates[i], self.datescount[i]) for i in range(len(self.dates)))
  8. # get the index of a specified date
  9. def getdateindex(self, moddate):
  10. for i, dat in enumerate(reversed(self.dates) if self.shouldbereversed(moddate) else self.dates):
  11. if dat == moddate:
  12. return i
  13. return -1
  14. # compares proximity of date to start and end of data structure
  15. def shouldbereversed(self, moddate):
  16. if self.dates == []:
  17. return False
  18. if abs(self.dates[0] - moddate) < abs(self.dates[-1] - moddate):
  19. return True
  20. return False
  21. def add(self, moddate):
  22. self.dates.append(moddate)
  23. self.datescount.append(1)
  24. def record(self, moddate):
  25. i = self.getdateindex(moddate)
  26. if i < 0:
  27. self.add(moddate)
  28. else:
  29. self.datescount[i] += 1
  30. # def qsort(self):
  31. def highestdate(self):
  32. current = 0
  33. for dat in self.dates:
  34. if dat > current:
  35. current = dat
  36. print("highest date: {0}".format(current))
  37. return current
  38. def highestcount(self):
  39. current = 0
  40. for count in self.datescount:
  41. if count > current:
  42. current = count
  43. print("highest count: {0}".format(current))
  44. return current
  45. def lowestdate(self):
  46. if len(self.dates) > 0:
  47. current = self.dates[0]
  48. else:
  49. return -1
  50. for dat in self.dates:
  51. if dat < current:
  52. current = dat
  53. print("lowest date: {0}".format(current))
  54. return current
  55. def lowestcount(self):
  56. if len(self.datescount) > 0:
  57. current = self.datescount[0]
  58. else:
  59. return -1
  60. for count in self.datescount:
  61. if count < current:
  62. current = count
  63. print("lowest count: {0}".format(current))
  64. return current