简体   繁体   English

使用命令临时更改python脚本

[英]Change python script with command temporarily

So I have a script called controller.py that has commands that interface with a front end on a website. 因此,我有一个名为controller.py的脚本,该脚本具有与网站前端连接的命令。 I am looking for a way that when a command is run, for example: 我正在寻找一种方法,当运行命令时,例如:

               if command == 'F':
                drivingSpeed = drivingSpeedActuallyUsed
                for motorIndex in range(2):
                    runMotor(motorIndex, forward[motorIndex])
                time.sleep(straightDelay)

in controller.py, it changes a line in another script called send_video.py for a few seconds then change back to the original 在controller.py中,它会在另一个名为send_video.py的脚本中将一行更改几秒钟,然后更改回原始

overlayCommand = '-vf transpose=2,transpose=2,dynoverlay=overlayfile=/home/pi/runmyrobot/images/hud.png:check_interval=500'

I need the line in send_video.py to change the directory of the image. 我需要send_video.py中的行来更改图像的目录。 Another issue I am having is that the send_video.py only updates on reboot, I need to tell it to update automatically. 我遇到的另一个问题是send_video.py仅在重新启动时更新,我需要告诉它自动更新。

Changing a line in code automatically is very unusual and far from what we call "best practice". 自动更改代码行是非常不寻常的,与我们所谓的“最佳实践”相去甚远。 This is called self-modifying code and was the topic of research done several decades ago. 这称为自修改代码,是几十年前完成的研究主题。 Since then it has been dropped almost completely and is now regarded an ugly hack in all but the very strangest circumstances. 从那以后,它几乎被完全丢弃,现在在最奇怪的情况下,在所有情况下都被视为丑陋的骇客。

What is typically done to achieve your goal is to pass parameters and use them in the other code. 通常要实现目标的目的是传递参数并在其他代码中使用它们。

Your wording is a bit unclear (I don't understand that part about updating a file, for example), so I'm not sure if I address your issue correctly, but I'll give it a try in a general approach: 您的措辞有点不清楚(例如,我不了解有关更新文件的那部分),因此不确定是否可以正确解决您的问题,但可以尝试使用一般方法:

When your code in controller.py calls the code in send_video.py it will call a function. controller.py的代码调用send_video.py的代码时,它将调用一个函数。 To this function is should pass an argument which is the line. 此函数应该传递一个参数,即该行。 The function in send_video.py should use the passed parameter to determine which line it should use (the default one or the special one). send_video.py的函数应使用传递的参数来确定应使用哪一行(默认行或特殊行)。

Feel free to improve your question by showing us more of your code or ask further questions if this does not help enough. 请向我们显示更多代码,以改善您的问题,或者如果这样做还不够帮助,请提出其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM