Squish Record and play in python - python-3.x

While recording activities in an application through squish in python, I want some wait time in between consecutive activities.
Which function should I use?

You can use the snooze function to suspend test execution for a certain time.
In general however, fixed-time delays are fragile and depend a lot on the system on which the test is executed (and the load of the system). A better approach might be to use the waitFor function to wait for some condition.
For instance, this code acquires a reference to a QPushButton object with the text OK and then suspends test execution until the buttom becomes disabled:
button = waitForObject("{type='QPushButton' text='OK'}")
waitFor(lambda: not button.enabled)

You can use sleep function. For example to put the script sleep for 2 seconds . (Eg:- sleep(2)). Dont forget to import datatime libraries . (Eg:- import time)

# going to sleep for 2 seconds
snooze(2)

Related

Executing a function periodically on accurate and precise intervals

I want to implement an accurate and precise countdown timer for my application. I started with the most simple implementation, which was not accurate at all.
loop {
// Code which can take upto 10 ms to finish
...
let interval = std::time::Duration::from_millis(1000);
std::thread::sleep(interval);
}
As the code before the sleep call can take some time to finish, I cannot run the next iteration at the intended interval. Even worse, if the countdown timer is run for 2 minutes, the 10 milliseconds from each iteration add up to 1.2 seconds. So, this version is not very accurate.
I can account for this delay by measuring how much time this code takes to execute.
loop {
let start = std::time::Instant::now();
// Code which can take upto 10 ms to finish
...
let interval = std::time::Duration::from_millis(1000);
std::thread::sleep(interval - start.elapsed());
}
Even though this seems to precise up to milliseconds, I wanted to know if there is a way to implement this which is even more accurate and precise and/or how it is usually done in software.
For precise timing, you basically have to busy wait: while time.elapsed() < interval {}. This is also called "spinning" (you might have heard of "spin lock"). Of course, this is far more CPU intensive than using the OS-provided sleep functionality (which often transitions the CPU in some low power mode).
To improve upon that slightly, instead of doing absolutely nothing in the loop body, you could:
Call thread::yield_now().
Call std::hint::spin_loop()
Unfortunately, I can't really tell you what timing guarantees these two functions give you. But from the documentation it seems like spin_loop will result in more precise timing.
Also, you very likely want to combine the "spin waiting" with std::thread::sleep so that you sleep the majority of the time with the latter method. That saves a lot of power/CPU-resources. And hey, there is even a crate for exactly that: spin_sleep. You should probably just use that.
Finally, just in case you are not aware: for several use cases of these "timings", there are other functions you can use. For example, if you want to render a frame every 60th of a second, you want to use some API that synchronizes your loop with the refresh rate/v-blanking of the monitor directly, instead of manually sleeping.

How many seconds left for a sleeping process to "wake up"

I would like to know if there is a way to know how many seconds are left for a sleeping process (in 'S' status) to "wake up" in LINUX.
For example, a python process I put to sleep using the sleep method.
from time import sleep
sleep(60)
Thanks!
A process will sleep until an interrupt is sent. This may be from a software signal or some hardware via the o/s.
Depending on what you are trying to achieve, looking at this from the operating level might be an overkill. Instead consider solving it within your code, by splitting your sleep in multiple sleep statements and giving a update that way.
from time import sleep
for i in range(0,60):
sleep(1)
# Give an update status via i
print(f"I've slept {i} seconds")
Replace the print with something that gives an update to the OS, parses a value to a different programm etc, depending on your needs. This would be easier than trying to figure this out on an OS level.

MATLAB code, linked to an EXCEL DDE, doesn't pause. What should I do?

I'm linking a MATLAB code to a DDE application in Excel.
It works fine, but whenever I try to run the code, the operations are done without respecting pauses each time. This part gets paused in the right way, as it should be:
channel = ddeinit('excel','C:\VTScada\NewApplication\Application.xlsx')
%Inserire valori iniziali e finali
s_start = input('s_start','s')
pause(1)
When I run the remaning code, like this for example, it's like MATLAB sums all the values of pauses. Then, after some time, it runs all the code in a very rapid way, without respecting pauses each time.
ddepoke(channel,'r18c2',1)
pause(10)
ddepoke(channel,'r18c2',0)
ddepoke(channel,'r18c2',1)
pause(10)
ddepoke(channel,'r18c2',0)
I tried to solve this problem using the code
pause('on')
pause(10)
pause('off')
but it turns out that in this particular case, MATLAB doesn't respect pauses at all. It seems to worsen the situation. What should I do?
The Matlab pause command means more than just "wait for N seconds"; it interacts with the graphics pipeline and stuff.
This is a hack, but try doing this instead of pause:
java.lang.Thread.sleep(10 * 1000);
That's a lower-level operation that will temporarily stop the program's execution in a more unconditional manner. The * 1000 is there because sleep takes its input in milliseconds instead of seconds.
I solved the problem simpling by removing the '{}' after the if, and adding the 'Java.lang.Thread.sleep()' method, instead of the Pause() method.
Thanks to everyone for the help, in any case

I am running python code and the class takes several minutes to run. Is there a way to pause the code mid-run and edit the code?

I am running a class that takes several minutes. There are many times that it is about halfway done when I realize that I would like to print something. Is there a way to add in a print statement without having to stop the run and add it in if the specific function has not run yet? All I have really been able to find is sleep statements, but those would need to have been added before the run and I can't edit the code using a sleep timer.
You cannot pause the execution of the code and overwrite the code to reflect the changes in the forthcoming execution steps. The python file is compiled into a .pyc file which is currently getting executed. Hence even if you manage to pause it somehow, the current execution will resume the execution of the previous executable itself. also the use of sleep function is not actually stopping the execution. The code is still executing.
A good way to go about doing this is to setup logical breakpoints in your code rather than using multiple print statements throughout. You may have a look at this or this to know how to set break points. Although I would recommend using the VsCode debugger for the task.
Thus in conclusion, you cannot pause the script and make edits such that they reflect upon resuming the script's execution. Technically you can stall the execution of the script but that would never allow you to achieve what you intend to do.
The print statement is not your logger; use logging instead
Here are the docs: https://docs.python.org/3/library/logging.html
Here's a good example on how to use Logging: https://realpython.com/python-logging/
Start putting logger statements throughout your code.
a way to pause the code mid-run
Yes, it's called a breakpoint. You'll need to set a breakpoint somewhere in your code. It can even be a conditional breakpoint in that the breakpoint only triggers when a certain condition is met.
You can then look at the variable values in your program's stack.
PyCharm is very good at all of this. I highly recommend it.

whether to use job scheduler or sleep() function

I am confused whether to use cron job scheduler or use sleep function in the program itself. There are questions on this previously but I seem to have some different requirements form them.
I need some information from the previous run of the program so if I use cron to schedule
job I would have to store that information at some place and re-read it next time(this can make the program less scale-able if the size of this information grows).
I can also use sleep() but that will be using resources.
I will need to re-run the program every 10 mins or so. Which one is better to use.
Is there any other nice way of doing it which I may be missing.
In general you should use cron whenever you can for something like this.
The only problem I could foresee is if your program somehow took longer than 10 minutes to run, cron is going to call the next execution 10 minutes later anyway. This creates a really long race condition basically, where if you did sleep it would only start sleeping after the previous execution ended.
But assuming your program will take less time to run, I say go with cron.

Resources