python delay a program for n seconds [duplicate] - python

This question already has answers here:
How do I make a time delay? [duplicate]
(13 answers)
Closed 3 years ago.
is there a function in python that delays a program from running for n number of seconds?
I am trying to achieve something like this:
print("Hello")
delay(5) #delays the program by 5 seconds
print("World!")
Any help will be greatly appreciated, Thank you!

Sure!
import time
print("Hello")
time.sleep(5) #delays the program by 5 seconds
print("World!")

Related

How would I be able to have certain lines of code be executed randomly? [duplicate]

This question already has answers here:
Percentage chance to make action
(4 answers)
Closed 10 months ago.
I feel this should be an obvious answer, however, I am having issues coding a personal project of mine. How would I be able to have certain lines of code be executed randomly?
Not from the project itself but the principles are still, say for example I would want the following code to be executed every 1/1000 times or so.
print("Lucky!")
How would I exactly be able to do that?
Set a trace function that will have a 1/1000 chance to print:
import random, sys
def trace(frame, event, arg):
if random.random() < 1/1000:
print("Lucky!")
return trace
sys.settrace(trace)
You're welcome to test it with a 1/2 chance.
Generate a random integer in the range [0,1000). Pick any single value in the range, and check for equality to see if you should print.
import random
def lucky():
if random.randrange(1000) == 42:
print("Lucky!")

How can I set time in this Python code? [duplicate]

This question already has answers here:
How do I get a Cron like scheduler in Python?
(9 answers)
Closed 4 years ago.
def initialize(context):
g.security = '000001.XSHE'
set_benchmark('000300.XSHG')
def handle_data(context,data):
security = g.security
order(security, 100)
I want operation this code at 10:00 o'clock, how can write the code?
You can use the sched module or simply loop through datetime, although as AChampion points out, on a *nix platform, it's easier to use cron.
This stackoverflow question does into more detail about sched and datetime.
Essentially:
from datetime import datetime as dt
while True:
if dt.now().hour == 10: # for 10 o'clock
initialize(context)
handle_data(context,data)
time.sleep(60) # Minimum interval between task executions
else:
time.sleep(10) # The else clause is not necessary but would prevent the program to keep the CPU busy.

Pausing a while loop [duplicate]

This question already has answers here:
How do I make a time delay? [duplicate]
(13 answers)
Closed 8 years ago.
I have a query for a small program that I am running. I was wondering if there was any way to pause a while loop in python for an amount of time? Say for example if I wanted it to print something out, then pause for say (1) second, then print it out again?
There is not really a point to this program I am more doing it for something to do while bored.
I have checked other questions and none seemed to really answer what I was asking.
import time
while ...
print your line
time.sleep(1) # sleep for one second
time sleep method
Yes. There is a function called sleep that does exactly what you want:
while true:
print "Hello!\n"
time.sleep(1)

Python 2.x ; function to wait a few second for continue [duplicate]

This question already has answers here:
How do I make a time delay? [duplicate]
(13 answers)
Closed 8 years ago.
I've just made a program, but I want to make a function in one place just to wait a few seconds to continue working. Could you tell me how does that function look?
for example
print 'I know'
(HERE wait few seconds to continue working)
print 'I am so layman with my trivial question'
import time
print 'I know'
time.sleep(1) #number of seconds to wait
print 'I am so layman with my trivial question'
Documentation

Python calling plot from R script, plot does not stay [duplicate]

This question already has answers here:
R: building a simple command line plotting tool/Capturing window close events
(1 answer)
Running R Scripts with Plots
(5 answers)
Closed 9 years ago.
I am using Python which calls a plot from R. Python issues the Rscript command. Everything works, except that the plot instantly disappears.
I tried several things on the R side:
par(ask=TRUE)
Sys.sleep(5)
par does not work this way; it will just ignore it.
With sleep the problem is that R will hang the python script for 5 seconds while sleeping, but also that sometimes I want to be able to close the plot immediately: when I do python just keeps waiting until the 5 seconds are over.
Could it be Python related, or is there a solution in R?
Minimum Working Example:
R:
foo.R
plot(1:10)
Sys.sleep(5)
Python:
foo.py
import os
os.system("Rscript foo.R")
Thanks to mathematical-coffee as from the comments and thanks to Dirk for a similar answer in another thread:
> library(tcltk)
> tk_messageBox(message="Press a key")

Categories