code: def get_single_result(
      entity,
      query,
      results,
      log=None,
      show_results=1,
      raise_on_error=True,
      sort_by_date=True,
  ):
      if not results:
          if not raise_on_error:
              return None
  
          raise ValueError(
              "No {entity}s found when searching for `{query}`".format(
                  **locals()
              )
          )
  
      if len(results) > 1:
          if show_results:
              if not log:
                  log = get_logger()
              if show_results > 1:
                  log.warning(
                      "{num} {entity} found when searching for `{query}`"
                      " (showing first {show_results} {entity}s follow)".format(
                          num=len(results), **locals()
                      )
                  )
              else:
                  log.warning(
                      "{num} {entity} found when searching for `{query}`".format(
                          num=len(results), **locals()
                      )
                  )
  
          if sort_by_date:
              relative_time = get_epoch_beginning_of_time()
              # sort results based on timestamp and return the newest one
              if hasattr(results[0], "last_update"):
                  results = sorted(
                      results,
                      key=lambda x: int(
                          (
                              x.last_update - relative_time
                          ).total_seconds()
                          if x.last_update
                          else 0
                      ),
                      reverse=True,
                  )
              elif hasattr(results[0], "created"):
                  results = sorted(
                      results,
                      key=lambda x: int(
                          (x.created - relative_time).total_seconds()
                          if x.created
                          else 0
                      ),
                      reverse=True,
                  )
  
          if show_results and log:
              for i, obj in enumerate(
                  o if isinstance(o, dict) else o.to_dict()
                  for o in results[:show_results]
              ):
                  selected = (
                      "Selected" if i == 0 else "Additionally found"
                  )
                  log.warning(
                      "{selected} {entity} `{obj[name]}` (id={obj[id]})".format(
                          **locals()
                      )
                  )
  
          if raise_on_error:
              raise ValueError(
                  "More than one {entity}s found when searching for ``{query}`".format(
                      **locals()
                  )
              )
  
      return results[0]
  
imports: import getpass
  import re
  from _socket import gethostname
  from datetime import datetime
  from typing import Optional, Any
  from ..backend_api.services import projects, queues
  from ..debugging.log import get_logger, LoggerRoot
label_desc: Use image node_id 9 for calling a global function with example usage: get_single_result(entity, query, results, log, show_results, raise_on_error, sort_by_date) and returns: results, None