SHARE
TWEET

Untitled

a guest Jun 25th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from datetime import datetime, timedelta, date, time
  2. from scrape import scrape
  3. import csv
  4.  
  5. def time_intersect(start_A, end_A, start_B, end_B):
  6.     """
  7.     accepts 2 timeframe and return the overlap
  8.     :param start_A: datetime
  9.     :param end_A: datetime
  10.     :param start_B: datetime
  11.     :param end_B: datetime
  12.     :return: timedelta
  13.     """
  14.     latest_start = max(start_A, start_B)
  15.     earliest_end = min(end_A, end_B)
  16.     if latest_start <= earliest_end:
  17.         return (earliest_end - latest_start).total_seconds()/3600
  18.     else:
  19.         return 0
  20.  
  21. def create_datelist(start_A, end_B, time_start, time_end, sat=False, sun=False):
  22.     """
  23.     takes 2 datetime object iterates to all dates in range of the 2 datetime object
  24.     and combine the time_start and time_end to all dates in range. Returns a list of 2 datetime item list
  25.  
  26.     :param start_A: datetime
  27.     :param end_B: datetime
  28.     :param time_start: int 0 to 23
  29.     :param time_end: int 0 to 23
  30.     :return: datetime
  31.     """
  32.     if time_start > time_end:
  33.         day_offset = 1
  34.     else:
  35.         day_offset = 0
  36.     counter = 0
  37.     datelist = []
  38.     while True:
  39.         if sat and start_A.weekday() == 5:
  40.             datelist.append([datetime.combine(start_A, time(0)),
  41.                              datetime.combine(start_A + timedelta(days=1), time(0))])
  42.         elif sun and start_A.weekday() == 6:
  43.             datelist.append([datetime.combine(start_A, time(0)),
  44.                              datetime.combine(start_A + timedelta(days=1), time(0))])
  45.         elif not sat and not sun:
  46.             datelist.append([datetime.combine(start_A, time(time_start)),
  47.                              datetime.combine(start_A + timedelta(days=day_offset), time(time_end))])
  48.  
  49.         counter += 1
  50.         start_A += timedelta(days=counter)
  51.         if start_A.date() > end_B.date():
  52.             break
  53.     return datelist
  54.  
  55. #Sample data from scrape() [[2019-03-19 00:00:00,2019-03-19 06:00:00,Staff name,Client name]] there would 1000+ items
  56. rosters = scrape()
  57. rates = {'am':48.14,
  58.          'pm':52.79,
  59.          'on':34.14,
  60.          'sat':66.77,
  61.          'sun':85.45}
  62. roster_rated = []
  63. for roster in rosters:
  64.     mornings = create_datelist(roster[0],roster[1],6,20)
  65.     evenings = create_datelist(roster[0],roster[1],20,0)
  66.     over_nights = create_datelist(roster[0],roster[1],0,6)
  67.  
  68.     if roster[1].hour == 0:#adjust for midnight
  69.         roster[1] += timedelta(days=1)
  70.     sat = 0
  71.     sun = 0
  72.     am = 0
  73.     pm = 0
  74.     on = 0
  75.     if roster[0].weekday() == 5:
  76.         saturdays = create_datelist(roster[0], roster[1], 0, 6, sat=True)
  77.         for saturday in saturdays:
  78.             sat += time_intersect(roster[0], roster[1], saturday[0], saturday[1])
  79.     elif roster[0].weekday() == 6:
  80.         sundays = create_datelist(roster[0], roster[1], 0, 6, sun=True)
  81.         for sunday in sundays:
  82.             sun += time_intersect(roster[0], roster[1], sunday[0], sunday[1])
  83.     else:
  84.         for morning in mornings:
  85.             am += time_intersect(roster[0], roster[1], morning[0], morning[1])
  86.         for evening in evenings:
  87.             pm += time_intersect(roster[0], roster[1], evening[0], evening[1])
  88.         for over_night in over_nights:
  89.             on += time_intersect(roster[0], roster[1], over_night[0], over_night[1])
  90.  
  91.  
  92.     roster.extend([am,am*rates['am'],pm,pm*rates['pm'],on,on*rates['on'],sat,sat*rates['sat'],sun,sun*rates['sun']])
  93.     roster_rated.append(roster)
  94.  
  95. with open('roster_rated.csv', 'w', newline='') as f:
  96.     writer = csv.writer(f)
  97.     for each in roster_rated:
  98.         writer.writerow(each)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top