向用户输入添加命令
Add commands to user input
最近我开始了一个项目。我的目标是拥有一个脚本,一旦启动,如果通过电子邮件发送指令,就可以控制主机上的操作。 (我想要这个,这样我就可以在出门在外时开始需要很长时间才能完成的任务)
我开始编程,不久之后我就可以发送电子邮件、接收电子邮件并分析其内容并对电子邮件中的内容采取行动。
我这样做的方式是这样的:
我首先尝试使用不带前缀的命令,但这会导致错误,所以我添加了“!”在可以执行的每个命令的前面。然后,每当有新行时,我都会拆分电子邮件的内容。
contents_of_the_email ="!screen\n!wait 5\n!hotkey alt tab\n"
# takes a screenshot waits 5 seconds and presses alt tab
lines = contents_of_the_email.count("\n")
contents_of_the_email = contents_of_the_email.split("\n")
for i in range(lines):
if "!screen" in contents_of_the_email[i]:
print("I took a screenshot and sent it to the specified email!")
elif "!wait " in contents_of_the_email[i]:
real = contents_of_the_email[i].split("!wait ")
print(f"I slept for {real[1]} seconds!")
elif "!hotkey " in contents_of_the_email[i]:
real = contents_of_the_email[i].split(" ")
print(f"I pressed the keys {real[1]} and {real[2]} at the same time!")
我用 print 语句替换了命令的实际代码,因为不需要它们来理解我的问题或重新创建它。
现在这变得非常混乱。我添加了大约 20 个命令,但它们开始发生冲突。我主要通过更改命令的名称来修复它,但它仍然是意大利面条代码。
我想知道是否有更优雅的方式向我的程序添加命令,我想要这样的方式:
def takeScreenshot():
print("I took a screenshot!")
addNewCommand(trigger="!screen",callback=takeScreenshot())
如果这在 python 中可行,我将不胜感激!谢谢 :D
您是否考虑过使用字典来调用您的函数?
def run_A():
print("A")
# code...
def run_B():
print("B")
# code...
def run_C():
print("C")
# code...
inputDict = {'a': run_A, 'b': run_B, 'c': run_C}
# After running the above
>>> inputDict['a']()
A
尝试使用 built-in 模块 cmd:
import cmd, time
class ComputerControl(cmd.Cmd):
def do_screen(self, arg):
pass # Screenshot
def do_wait(self, length):
time.sleep(float(length))
def do_hotkey(self, arg):
...
inst = ComputerControl()
for line in contents_of_the_email.splitlines():
line = line.lstrip("!") # Remove the '!'
inst.onecmd(line)
您可以尝试这样的操作:
def take_screenshot():
print("I took a screenshot!")
def wait(sec):
print(f"I waited for {sec} secs")
def hotkey(*args):
print(f"I pressed the keys {', '.join(args)} at the same time")
def no_operation():
print("Nothing")
FUNCTIONS = {
'!wait': wait,
'!screen': take_screenshot,
'!hotkey': hotkey,
'': no_operation
}
def call_command(command):
function, *args = command.split(' ')
FUNCTIONS[function](*args)
contents_of_the_email = "!screen\n!wait 5\n!hotkey alt tab\n"
# takes a screenshot waits 5 seconds and presses alt tab
for line in contents_of_the_email.split("\n"):
call_command(line)
只需将函数和字典定义移动到单独的模块中。
最近我开始了一个项目。我的目标是拥有一个脚本,一旦启动,如果通过电子邮件发送指令,就可以控制主机上的操作。 (我想要这个,这样我就可以在出门在外时开始需要很长时间才能完成的任务)
我开始编程,不久之后我就可以发送电子邮件、接收电子邮件并分析其内容并对电子邮件中的内容采取行动。 我这样做的方式是这样的: 我首先尝试使用不带前缀的命令,但这会导致错误,所以我添加了“!”在可以执行的每个命令的前面。然后,每当有新行时,我都会拆分电子邮件的内容。
contents_of_the_email ="!screen\n!wait 5\n!hotkey alt tab\n"
# takes a screenshot waits 5 seconds and presses alt tab
lines = contents_of_the_email.count("\n")
contents_of_the_email = contents_of_the_email.split("\n")
for i in range(lines):
if "!screen" in contents_of_the_email[i]:
print("I took a screenshot and sent it to the specified email!")
elif "!wait " in contents_of_the_email[i]:
real = contents_of_the_email[i].split("!wait ")
print(f"I slept for {real[1]} seconds!")
elif "!hotkey " in contents_of_the_email[i]:
real = contents_of_the_email[i].split(" ")
print(f"I pressed the keys {real[1]} and {real[2]} at the same time!")
我用 print 语句替换了命令的实际代码,因为不需要它们来理解我的问题或重新创建它。
现在这变得非常混乱。我添加了大约 20 个命令,但它们开始发生冲突。我主要通过更改命令的名称来修复它,但它仍然是意大利面条代码。
我想知道是否有更优雅的方式向我的程序添加命令,我想要这样的方式:
def takeScreenshot():
print("I took a screenshot!")
addNewCommand(trigger="!screen",callback=takeScreenshot())
如果这在 python 中可行,我将不胜感激!谢谢 :D
您是否考虑过使用字典来调用您的函数?
def run_A():
print("A")
# code...
def run_B():
print("B")
# code...
def run_C():
print("C")
# code...
inputDict = {'a': run_A, 'b': run_B, 'c': run_C}
# After running the above
>>> inputDict['a']()
A
尝试使用 built-in 模块 cmd:
import cmd, time
class ComputerControl(cmd.Cmd):
def do_screen(self, arg):
pass # Screenshot
def do_wait(self, length):
time.sleep(float(length))
def do_hotkey(self, arg):
...
inst = ComputerControl()
for line in contents_of_the_email.splitlines():
line = line.lstrip("!") # Remove the '!'
inst.onecmd(line)
您可以尝试这样的操作:
def take_screenshot():
print("I took a screenshot!")
def wait(sec):
print(f"I waited for {sec} secs")
def hotkey(*args):
print(f"I pressed the keys {', '.join(args)} at the same time")
def no_operation():
print("Nothing")
FUNCTIONS = {
'!wait': wait,
'!screen': take_screenshot,
'!hotkey': hotkey,
'': no_operation
}
def call_command(command):
function, *args = command.split(' ')
FUNCTIONS[function](*args)
contents_of_the_email = "!screen\n!wait 5\n!hotkey alt tab\n"
# takes a screenshot waits 5 seconds and presses alt tab
for line in contents_of_the_email.split("\n"):
call_command(line)
只需将函数和字典定义移动到单独的模块中。