Change python script with command temporarily - python

So I have a script called controller.py that has commands that interface with a front end on a website. 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
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. Another issue I am having is that the send_video.py only updates on reboot, I need to tell it to update automatically.

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. 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).
Feel free to improve your question by showing us more of your code or ask further questions if this does not help enough.

Related

Edit and run a sequence of scripts in a less-manual way [Python]

I am trying to find a way to edit and run a sequence of python scripts in a less manual way.
For context, I am running a series of simulations, which consist in running three codes in order 10 times, making minor changes to each code every time. The problem I am encountering is that this process leads to easy mistakes and chaotic work.
These are the type of edits I have to make to each code.
- Modify input/output file name
- Change value of a parameter
What is the best practice to deal with this? I imagine that the best idea would be to write another python script that does all this. Is there a way to edit other python codes, from within a code, and run them?
I don't intend or want anyone to write a code for me. I just need to be pointed in a general direction. I have searched for ways to 'automatize' codes, but haven't yet been successful in finding a solution to my query (mainly the editing part).
Thanks!
The thing that can change (files or parameter values) should be able to be either passed in or injected. Could be from a command line parameter, configuration file, or method argument. This is the "general direction" I offer.

How can a Py script call a "setup/configure" function only on first-run?

I've been learning Python and decided to make a note-taking utility that runs in bash. I've worked out the basic 'guts' and I want to add a feature that allows a new user to configure their notes (for instance, set the directory where new note files are stored).
I realize this means running a 'install/config' function that is only called the first time the user runs the script (or until they configure it). I don't know what this concept is called, and after some research, cannot find anything about it w/Python.
I'm using argparse. You call the python script from the shell and can optionally use it with arguments. If it would help to see my code, please let me know and I'll format it (it's long and needs to be edited a bit if I want to post). Thanks.
tl;dr How do you run a function only once in Python (either first time code is executed, or until the function's purpose - in this case, setting a file path - is fulfilled)?

How to programmatically execute/step through Python code line by line

I am trying to find a way that I can have a program step through Python code line by line and do something with the results of each line. In effect a debugger that could be controlled programmatically rather than manually. pdb would be exactly what I am looking for if it returned its output after each step as a string and I could then call pdb again to pickup where I left off. However, instead it outputs to stdout and I have to manually input "step" via the keyboard.
Things I have tried:
I am able to redirect pdb's stdout. I could redirect it to a second
Python program which would then process it. However, I cannot
figure out how to have the second Python program tell pdb to
step.
Related to the previous one, if I could get pdb to step all the way
through to the end (perhaps I could figure out something to spoof a
keyboard repeatedly entering "step"?) and redirect the output to a
file, I could then write another program that acted like it was
stepping through the program when it was actually just reading the
file line by line.
I could use exec to manually run lines of Python code. However,
since I would be looking at one line at a time, I would need to
manually detect and handle things like conditionals, loops, and
function calls which quickly gets very complicated.
I read some posts that say that pdb is implemented using
sys.settrace. If nothing else works I should be able to recreate
the behavior I need using this.
Is there any established/straight forward way to implement the behavior that I am looking for?
sys.settrace() is the fundamental building block for stepping through Python code. pdb is implemented entirely in Python, so you can just look at the module to see how it does things. It also has various public functions/methods for stepping under program control, read the library reference for your version of Python for details.
I read some posts that say that pdb is implemented using sys.settrace.
If nothing else works I should be able to recreate the behavior I need
using this.
Don't view this as a last resort. I think it's the best approach for what you want to accomplish.

Is it possible to examine the inner statements of a function?

Working from the command line I wrote a function called go(). When called it receives input asking the user for a directory address in the format drive:\directory. No need for extra slashes or quotes or r literal qualifiers or what have you. Once you've provided a directory, it lists all the non-hidden files and directories under it.
I want to update the function now with a statement that stores this location in a variable, so that I can start browsing my hierarchy without specifying the full address every time.
Unfortunately I don't remember what statements I put in the function in the first place to make it work as it does. I know it's simple and I could just look it up and rebuild it from scratch with not too much effort, but that isn't the point.
As someone who is trying to learn the language, I try to stay at the command line as much as possible, only visiting the browser when I need to learn something NEW. Having to refer to obscure findings attached to vaguely related questions to rediscover how to do things I've already done is very cumbersome.
So my question is, can I see the contents of functions I have written, and how?
Unfortunately no. Python does not have this level of introspection. Best you can do is see the compiled byte code.
The inspect module details what information is available at runtime: https://docs.python.org/3.5/library/inspect.html

Execute function body ignoring certain lines without comments (Python)?

I have a couple of functions written in a single Python file. They perform a sequence of steps on a file-based dataset.
My workflow:
After I finished coding a part of the function's body, I run the function to see how it goes.
It may break at a certain point.
I fix the code and re-run the function.
The problem is that when I re-run the function, it will execute the lines that were already completed successfully. Yet I want to be able to start not from the beginning but rather from an arbitrary point. This is because the whole function runs for several minutes and it would be a waste time to wait for it to complete.
I could implement "checks" to see whether this operation is required (e.g., don't create a file if it already exists), but this would imply adding a lot of new validation code (e.g., make sure that the existing file does contain the content needed); in reality, my function will be run on a dataset in known format and the whole function should be executed.
The most obvious solution is to comment out the parts that were executed successfully, but it's a hustle and I got tired of commenting and uncommenting parts as I move forwards and the function gets larger.
Are there are any better approaches than commenting out lines for ignoring certain part of the function's body when executing?
I am on Wing IDE if this has something to do with the debugging tricks in an IDE itself.
Wing can move the program counter to a different line in the function via the right-click popup menu, but you'd need to do this every time you run the function. I think a better approach is to refactor the function into smaller functions -- then you can comment out or conditionalize only the function calls. You could also write tests that call some of the functions and not others.

Categories