Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from datetime import datetime
  4. import os, calendar
  5.  
  6. #******************************************************************************
  7. def time_in_hours(time):
  8.   return (time.hour + (time.minute / 60) + (time.second / 3600))
  9. #******************************************************************************
  10.  
  11. themonth = int(input('Enter the month number(1-12): '))
  12. days = calendar.monthrange(2018, themonth)[1]
  13.  
  14. monthlytotal = 0
  15. dailytotal = []
  16. for i in range(days):
  17.   dailytotal.append(0)
  18.  
  19. fig, ax = plt.subplots(figsize=(12, 6))
  20. plt.subplots_adjust(bottom = 0.15)
  21. logfile = open("log.txt","r")
  22.  
  23. for idx, line in enumerate(logfile.readlines(), start=1):
  24.   try:
  25.     times = line.split(',')
  26.     dt1 = datetime.strptime(times[0], '%m/%d/%Y %I:%M:%S %p')
  27.     dt2 = datetime.strptime(times[1], '%m/%d/%Y %I:%M:%S %p')
  28.     t1 = dt1.time()
  29.     t2 = dt2.time()
  30.  
  31.     if dt1.month == themonth and dt2.month == themonth:    # if Startup Time and Shutdown Time are in the same month
  32.       if dt1.day == dt2.day:                               # same day
  33.         starttime = time_in_hours(t1)
  34.         endtime = time_in_hours(t2)
  35.         duration = endtime - starttime
  36.         ax.broken_barh([(starttime, duration)], ( ((dt1.day - 1)) , 1 ) )
  37.         dailytotal[dt1.day - 1 ] += duration
  38.  
  39.  
  40.       else:                                                # same month, not the same day
  41.         starttime = time_in_hours(t1)
  42.         endtime = time_in_hours(t2)
  43.         ax.broken_barh([(starttime, 24 - starttime)], ( ((dt1.day - 1)) , 1 ) )          # draw for day n
  44.         dailytotal[dt1.day - 1 ] += (24 - starttime)
  45.         ax.broken_barh([(0, endtime)], ( ((dt2.day - 1)) , 1 ) )                         # draw for day n+1
  46.         dailytotal[dt2.day - 1 ] += endtime
  47.  
  48.     elif dt1.month != themonth and dt2.month == themonth:  # if Startup Time is from the previous month
  49.       endtime = time_in_hours(t2)
  50.       ax.broken_barh([(0, endtime)], ( ((dt2.day - 1)) , 1 ) )
  51.       dailytotal[dt2.day - 1 ] += endtime
  52.     elif dt1.month == themonth and dt2.month != themonth:  # if Shutdown Time goes to the next month
  53.       starttime = time_in_hours(t1)
  54.       ax.broken_barh([(starttime, 24 - starttime)], ( ((dt1.day - 1)) , 1 ) )
  55.       dailytotal[dt1.day - 1 ] += (24 - starttime)
  56.  
  57.   except Exception as e:
  58.     print('error in line ' + str(idx) + ': ' + str(e))
  59.  
  60. logfile.close()
  61.  
  62. for i in range(1 , days + 1 ):
  63.   ax.text(24.1, i - 0.1 , i)
  64.   h = int(dailytotal[i-1])
  65.   m = int((dailytotal[i-1] - h) * 60 )
  66.   ax.text(24.8, i - 0.1 , format(h, '02d') + ':' + format(m, '02d'))
  67.   monthlytotal += dailytotal[i-1]
  68.  
  69. ax.text(21, days + 3 , 'Monthly total =        ' +  str(round(monthlytotal , 1)) )
  70. ax.text(21, days + 4 , 'Daily average =        ' +  str(round(monthlytotal / days , 1)) )
  71.  
  72. #******************************************************************************
  73. ax.set_title('Times my lapotp was on in ' + calendar.month_name[themonth])
  74. ax.set_xlim(0, 24)
  75. ax.set_ylim(0, days)
  76. ax.set_xticks(np.arange(25))
  77. ax.set_xlabel('Time of day')
  78. ax.set_ylabel('Day of month')
  79. ax.set_yticks(np.arange(0 , days))
  80. ax.set_yticklabels('')
  81. #to make day numbers show between the ticks: set minor ticks between major ticks. add labels.
  82. ax.set_yticks(np.arange(0.5 , days ), minor=True)
  83. ax.set_yticklabels(np.arange(1, days+1) , minor=True)
  84. # hide minor ticks but show labels
  85. ax.tick_params(axis=u'y', which=u'minor',length=0)
  86. ax.invert_yaxis()
  87. ax.grid(True)
  88.  
  89. plt.show()
  90. fig.savefig(os.path.basename(__file__) + '.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement