from re import sub


def macro_name(key):
  """Converts a string into a Zabbix LLD macro"""

  macro = key.upper()                     # uppercase
  macro = sub(r'[\s_-]+', '_', macro)     # replace whitespace with underscore
  macro = sub(r'[^a-zA-Z_]+', '', macro)  # strip illegal chars
  macro = sub(r'[_]+', '_', macro)        # reduce duplicates
  macro = ('{#' + macro + '}')            # encapsulate in {#}

  return macro