Advertisement
Guest User

lesson1.py

a guest
Dec 8th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. # We have a log file with timestamps, named events, and a start/end indicator
  2. # We wrote some code that is intended to calculate the average duration of
  3. # each event, but it is producing some unexpected outputs. What's happening, and
  4. # how can it be fixed?
  5.  
  6. # Test log file
  7. # 22000 get_foo start
  8. # 22050 get_foo end
  9. # 22200 get_bar start
  10. # 22250 get_bar end
  11. # 22400 get_foo end
  12. # 22500 get_bar start
  13. # 22550 get_bar end
  14. # 22580 get_foo start
  15. # 22600 get_bar end
  16. # 22610 get_foo end
  17.  
  18. def main():
  19.     # f = open("log.txt", "r")    # open log file in read mode
  20.  
  21.     with open("log.txt", "r") as f:
  22.         lines = f.readlines()
  23.  
  24.     event_start_times = {}      # define empty dictionaries for event starts and total times
  25.     total_times = {}            # defines total time for each event type
  26.  
  27.     for line in lines:              # Loop over each line in log file
  28.         words = line.rstrip().split(" ")    # Define each line in file as 'word' and strip characters to the right and split by space into a list
  29.         timestamp = int(words[0])       # first element in 'words' and convert to int
  30.         event = words[1]                # second element in 'words' list
  31.         event_type = words[2]                 # third element in 'words' list
  32.  
  33.         if event_type == "start":
  34.             event_start_times[event] = timestamp    # event_start dictionary stores the [event] as key and timestamp as value if 'start'
  35.         elif event not in total_times:              # if the event is not already stored in the total_times dict()
  36.             start_timestamp = event_start_times[event]  # Assign a value of the first timestamp that is encountered
  37.             total_times[event] = ((timestamp - start_timestamp), 1)     # find the total time and count of each independent event (get_foo / get_bar)
  38.         else:                                                       # if event IS in total_times or type == 'end'
  39.             time_count_pair = total_times[event]    # variable that returns the total time difference of each event and number of times it has occured
  40.             try:
  41.                 start_timestamp = event_start_times.pop(event)      # Assign a value of the starting timestamp
  42.                 time = time_count_pair[0] + (timestamp - start_timestamp)  # starting time + total time difference  (THESE VARIABLES NEED TO BE SWAPPED 'star_timestamp' and 'timestamp'_
  43.                 count = time_count_pair[1] + 1
  44.                 total_times[event] = (time, count)
  45.             except KeyError:
  46.                 print(f"Warning! Event {event} was not started. Skipping.")
  47.  
  48.         for event in total_times:
  49.             time, count = total_times[event]
  50.             average = time/count
  51.             print(f"{event} took an average of {average} milliseconds.")
  52.  
  53. main()
  54. # NamedTuple
  55. # One of the reasons for the problems - counting in events that are END but never started (like line 5)
  56. # exception handling
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement