Last active
December 11, 2022 09:54
-
-
Save spookyahell/a790e2a8ab8b1f24144333ba46a44fa7 to your computer and use it in GitHub Desktop.
Dumb vs smart functions (python): How eval can be helpful to remove a bunch of "repetitive code"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Using the smart method saves over 75% by line numbers ... | |
with chars/bytes it's 394 vs. 1220 chars which is "only" saves 67% of code | |
(All types of comments and extra lines were removed before comparision)''' | |
def convert_color_markdown(inp): | |
accepted_opts = 'black|red|green|magenta|cyan|blue|yellow|white|reset|reset_all|dim|normal|bright' | |
find_markdowns = re.findall(fr'(\[([sbf])=({accepted_opts})])', inp)#~ print(find_markdowns) | |
for markdown in find_markdowns: | |
type_lookup = {'f':'Fore','b':'Back','s':'Style'} #~ type, color = markdown[0].strip('[]').split('=') | |
inp = inp.replace(markdown[0], eval(f'colorama.{type_lookup[markdown[1]]}.{markdown[2].upper()}')) | |
return inp | |
# CODE BELOW IS "STUPID", CHECK ABOVE for "SMART" VERSION | |
#~ def convert_color_markdown(inp): | |
#~ # Handle Foreground colors | |
#~ out = inp.replace('[f=black]', colorama.Fore.BLACK) #1 | |
#~ out = out.replace('[f=red]', colorama.Fore.RED) #2 | |
#~ out = out.replace('[f=green]', colorama.Fore.GREEN) #3 | |
#~ out = out.replace('[f=magenta]', colorama.Fore.MAGENTA) #4 | |
#~ out = out.replace('[f=cyan]', colorama.Fore.CYAN) #5 | |
#~ out = out.replace('[f=blue]', colorama.Fore.BLUE) #6 | |
#~ out = out.replace('[f=yellow]', colorama.Fore.YELLOW) #7 | |
#~ out = out.replace('[f=white]', colorama.Fore.WHITE) #8 | |
#~ out = out.replace('[f=reset]', colorama.Fore.RESET) #9 | |
#~ # Handle Background colors | |
#~ out = out.replace('[b=black]', colorama.Back.BLACK) #1 | |
#~ out = out.replace('[b=red]', colorama.Back.RED) #2 | |
#~ out = out.replace('[b=green]', colorama.Back.GREEN) #3 | |
#~ out = out.replace('[b=yellow]', colorama.Back.YELLOW) #4 | |
#~ out = out.replace('[b=blue]', colorama.Back.BLUE) #5 | |
#~ out = out.replace('[b=magenta]', colorama.Back.MAGENTA) #6 | |
#~ out = out.replace('[b=cyan]', colorama.Back.CYAN) #7 | |
#~ out = out.replace('[b=white]', colorama.Back.WHITE) #8 | |
#~ out = out.replace('[b=reset]', colorama.Back.RESET) #9 | |
#~ # Handle Styles | |
#~ out = out.replace('[s=dim]', colorama.Style.DIM) #1 | |
#~ out = out.replace('[s=normal]', colorama.Style.NORMAL) #2 | |
#~ out = out.replace('[s=bright]', colorama.Style.BRIGHT) #3 | |
#~ out = out.replace('[s=reset]', colorama.Style.RESET_ALL)#4 | |
#~ return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
YAY, finally. Or better yet YAY-SN.
Yet Another Yawn-y—Sleepless Night.
Sigh. But at least I spent some of the early morning of it working on this.