def substitute_keywords(string, user_id, context):
 KEYWORD_FUNCTION_MAP = {'%%USER_ID%%': (lambda : anonymous_id_from_user_id(user_id)), '%%USER_FULLNAME%%': (lambda : context.get('name')), '%%COURSE_DISPLAY_NAME%%': (lambda : context.get('course_title')), '%%COURSE_END_DATE%%': (lambda : context.get('course_end_date'))}
 for key in KEYWORD_FUNCTION_MAP.keys():
  if (key in string):
   substitutor = KEYWORD_FUNCTION_MAP[key]
   string = string.replace(key, substitutor())
 return string

def substitute_keywords ( string , user_id , context ) : KEYWORD_FUNCTION_MAP = { '%%USER_ID%%' : ( lambda : anonymous_id_from_user_id ( user_id ) ) , '%%USER_FULLNAME%%' : ( lambda : context . get ( 'name' ) ) , '%%COURSE_DISPLAY_NAME%%' : ( lambda : context . get ( 'course_title' ) ) , '%%COURSE_END_DATE%%' : ( lambda : context . get ( 'course_end_date' ) ) } for key in KEYWORD_FUNCTION_MAP . keys ( ) : if ( key in string ) : substitutor = KEYWORD_FUNCTION_MAP [ key ] string = string . replace ( key , substitutor ( ) ) return string
replaces all %%-encoded words using keyword_function_map mapping functions iterates through all keywords that must be substituted and replaces them by calling the corresponding functions stored in keyword_function_map .