nodes: 0, module; 1, function_definition; 2, function_name:prev_next_group; 3, parameters; 4, block; 5, identifier:project; 6, identifier:group; 7, expression_statement; 8, comment:# TODO: Profile and optimize this query if necessary; 9, expression_statement; 10, try_statement; 11, expression_statement; 12, expression_statement; 13, return_statement; 14, comment:"""Return adjacent group objects or None for the given project and group.
  
      The previous and next group objects are relative to sort order of the
      project's groups with respect to the passed in group.
  
      """; 15, assignment; 16, block; 17, except_clause; 18, assignment; 19, assignment; 20, expression_list; 21, identifier:groups; 22, call; 23, expression_statement; 24, identifier:ValueError; 25, block; 26, identifier:prev_group; 27, conditional_expression:groups[index - 1] if index > 0 else None; 28, identifier:next_group; 29, conditional_expression:groups[index + 1] if index + 1 < len(groups) else None; 30, identifier:prev_group; 31, identifier:next_group; 32, identifier:sorted; 33, generator_expression; 34, assignment; 35, return_statement; 36, subscript; 37, comparison_operator:index > 0; 38, None; 39, subscript; 40, comparison_operator:index + 1 < len(groups); 41, None; 42, identifier:x; 43, for_in_clause; 44, if_clause; 45, identifier:index; 46, call; 47, expression_list; 48, identifier:groups; 49, binary_operator:index - 1; 50, identifier:index; 51, integer:0; 52, identifier:groups; 53, binary_operator:index + 1; 54, binary_operator:index + 1; 55, call; 56, identifier:x; 57, attribute; 58, attribute; 59, attribute; 60, argument_list; 61, None; 62, None; 63, identifier:index; 64, integer:1; 65, identifier:index; 66, integer:1; 67, identifier:index; 68, integer:1; 69, identifier:len; 70, argument_list; 71, identifier:project; 72, identifier:groups; 73, identifier:x; 74, identifier:submissions; 75, identifier:groups; 76, identifier:index; 77, identifier:group; 78, identifier:groups
code: def prev_next_group(project, group):
      """Return adjacent group objects or None for the given project and group.
  
      The previous and next group objects are relative to sort order of the
      project's groups with respect to the passed in group.
  
      """
      # TODO: Profile and optimize this query if necessary
      groups = sorted(x for x in project.groups if x.submissions)
      try:
          index = groups.index(group)
      except ValueError:
          return None, None
      prev_group = groups[index - 1] if index > 0 else None
      next_group = groups[index + 1] if index + 1 < len(groups) else None
      return prev_group, next_group