Skip to content

Instantly share code, notes, and snippets.

@klrkdekira
Created July 9, 2020 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klrkdekira/36a043c6e2fa0193a26b6c833101788e to your computer and use it in GitHub Desktop.
Save klrkdekira/36a043c6e2fa0193a26b6c833101788e to your computer and use it in GitHub Desktop.
def tobold(text):
return md.bold(text)
# using tobold == md.bold
# because tobold(text) == md.bold(text)
# fun fact, you can alias the function
# tobold = md.bold
# unless you want to pre fill parameter
def header(text):
return md.header(text, 1)
def toitalic(text):
return md.italics(text)
def tostrike(text):
return md.strikethrough(text)
def tomono(text):
return md.bold(text)
def format_msg(tag,msg):
switcher={
'b': tobold(msg),
'i': toitalic(msg),
's': tostrike(msg),
}
# switcher will create a copy of the result of each
# even if you're not using it
return switcher.get(tag,msg)
# switcher can be outside since it only need to be initialised once
# also most importantly switcher is not intended to be mutated
switcher = {
'b': md.bold,
'i': md.italics,
's': md.strikethrough,
}
def format_msg_minimal(tag, msg):
func = switcher.get(tag)
if not func:
return msg # not formatted
return func(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment