nodes: 0, module; 1, function_definition; 2, function_name:pop_events; 3, parameters; 4, block; 5, identifier:self; 6, identifier:regex_pattern; 7, identifier:timeout; 8, expression_statement; 9, if_statement; 10, expression_statement; 11, while_statement; 12, if_statement; 13, return_statement; 14, comment:"""Pop events whose names match a regex pattern.
  
          If such event(s) exist, pop one event from each event queue that
          satisfies the condition. Otherwise, wait for an event that satisfies
          the condition to occur, with timeout.
  
          Results are sorted by timestamp in ascending order.
  
          Args:
              regex_pattern: The regular expression pattern that an event name
                  should match in order to be popped.
              timeout: Number of seconds to wait for events in case no event
                  matching the condition exits when the function is called.
  
          Returns:
              Events whose names match a regex pattern.
              Empty if none exist and the wait timed out.
  
          Raises:
              IllegalStateError: Raised if pop is called before the dispatcher
                  starts polling.
              queue.Empty: Raised if no event was found before time out.
          """; 15, not_operator; 16, block; 17, assignment; 18, True; 19, comment:#TODO: fix the sleep loop; 20, block; 21, comparison_operator:len(results) == 0; 22, block; 23, call; 24, attribute; 25, raise_statement; 26, identifier:deadline; 27, binary_operator:time.time() + timeout; 28, expression_statement; 29, if_statement; 30, expression_statement; 31, call; 32, integer:0; 33, raise_statement; 34, identifier:sorted; 35, argument_list; 36, identifier:self; 37, identifier:started; 38, call; 39, call; 40, identifier:timeout; 41, assignment; 42, boolean_operator; 43, block; 44, call; 45, identifier:len; 46, argument_list; 47, call; 48, identifier:results; 49, keyword_argument; 50, identifier:IllegalStateError; 51, argument_list; 52, attribute; 53, argument_list; 54, identifier:results; 55, call; 56, comparison_operator:len(results) != 0; 57, comparison_operator:time.time() > deadline; 58, break_statement; 59, attribute; 60, argument_list; 61, identifier:results; 62, attribute; 63, argument_list; 64, identifier:key; 65, lambda; 66, string:"Dispatcher needs to be started before popping."; 67, identifier:time; 68, identifier:time; 69, attribute; 70, argument_list; 71, call; 72, integer:0; 73, call; 74, identifier:deadline; 75, identifier:time; 76, identifier:sleep; 77, integer:1; 78, identifier:queue; 79, identifier:Empty; 80, call; 81, lambda_parameters; 82, subscript; 83, identifier:self; 84, identifier:_match_and_pop; 85, identifier:regex_pattern; 86, identifier:len; 87, argument_list; 88, attribute; 89, argument_list; 90, attribute; 91, argument_list; 92, identifier:event; 93, identifier:event; 94, string; 95, identifier:results; 96, identifier:time; 97, identifier:time; 98, string; 99, identifier:format; 100, identifier:timeout; 101, identifier:regex_pattern; 102, string_content:time; 103, string_content:Timeout after {}s waiting for event: {}
code: def pop_events(self, regex_pattern, timeout):
          """Pop events whose names match a regex pattern.
  
          If such event(s) exist, pop one event from each event queue that
          satisfies the condition. Otherwise, wait for an event that satisfies
          the condition to occur, with timeout.
  
          Results are sorted by timestamp in ascending order.
  
          Args:
              regex_pattern: The regular expression pattern that an event name
                  should match in order to be popped.
              timeout: Number of seconds to wait for events in case no event
                  matching the condition exits when the function is called.
  
          Returns:
              Events whose names match a regex pattern.
              Empty if none exist and the wait timed out.
  
          Raises:
              IllegalStateError: Raised if pop is called before the dispatcher
                  starts polling.
              queue.Empty: Raised if no event was found before time out.
          """
          if not self.started:
              raise IllegalStateError(
                  "Dispatcher needs to be started before popping.")
          deadline = time.time() + timeout
          while True:
              #TODO: fix the sleep loop
              results = self._match_and_pop(regex_pattern)
              if len(results) != 0 or time.time() > deadline:
                  break
              time.sleep(1)
          if len(results) == 0:
              raise queue.Empty('Timeout after {}s waiting for event: {}'.format(
                  timeout, regex_pattern))
  
          return sorted(results, key=lambda event: event['time'])