Recently I started a project. My goal was it to have a script, which, once launched, could be able to control actions on the hosts computer if an instruction was send via email. (I wanted this so I could start tasks which take a long time to complete while I’m away from home)
I started programming and not long after I could send emails, receive emails and analyze their content and take actions responding to the content in the email.
I did this by using an input dictionary, which looked like this:
contents_of_the_email = "!screenn!wait 5n!hotkey alt tab" def wait(sec): print(f"I did nothing for {sec} seconds!") def no_operation(): print("Nothing") def screenshot(): print("I took an image of the screen and send it to your email adress!") def hotkey(*args): print(f"I pressed the keys {', '.join(args)} at the same time") FUNCTIONS = { '':no_operation, '!screen': screenshot, '!hotkey': hotkey, '!wait': wait } def call_command(command): function, *args = command.split(' ') FUNCTIONS[function](*args) for line in contents_of_the_email.split("n"): call_command(line)
In total I have around 25 functions each with its own response. I replaced the actual code for the commands with simple print statements as they are not needed to understand or replicate my problem.
I then wanted to add aliases for the command, so for example you would be able to type “!ss” instead of “!screen”. I did achieve this using another line in the dictionary:
FUNCTIONS = { '':no_operation, '!screen': screenshot, '!ss':screenshot, '!hotkey': hotkey, '!wait': wait }
But I didn’t like this. It would fill up the whole dictionary if I did it for every alias I am planning to add and it would make my code very messy.
Is there any way to define aliases for commands separately and still keep the dictionary clean and simple? I would desire something like this in a separate aliases.txt
file:
screen: "!screen", "!ss","!screenshot","!image" wait: "!wait","!pause","!sleep","!w" hotkey: "!hk","!tk"
If this is possible in python I would really appreciate to know!
Advertisement
Answer
You can use following solution:
import json contents_of_the_email = "!screenn!wait 5n!hotkey alt tab" def wait(sec): print(f"I did nothing for {sec} seconds!") def no_operation(): print("Nothing") def screenshot(): print("I took an image of the screen and send it to your email address!") def hotkey(*args): print(f"I pressed the keys {', '.join(args)} at the same time") # FUNCTIONS DICT FROM JSON with open("aliases.json") as json_file: aliases_json = json.load(json_file) FUNCTIONS = {} for func_name, aliases in aliases_json.items(): FUNCTIONS.update({alias: globals()[func_name] for alias in aliases}) def call_command(command): function, *args = command.split(' ') FUNCTIONS[function](*args) for line in contents_of_the_email.split("n"): call_command(line)
aliases.json:
{ "screenshot": ["!screen", "!ss","!screenshot","!image"], "wait": ["!wait","!pause","!sleep","!w"], "hotkey": ["!hk","!tk", "!hotkey"] }
is that what you looking for?