The time module
The time module allows the user to directly get the time, in seconds, since 1970 (See: https://docs.python.org/3/library/time.html). This means that we can subtract the time before from time after to see how long it has been, namely how long it took the user to finish the typing test. From there, it is as easy as printing the result. You can round the time using int to get a purely seconds result without milliseconds.
The code
# Import the time library
import time
# Calculate the start time
start = time.time()
# Code here
# Calculate the end time and time taken
end = time.time()
length = end - start
# Show the results : this can be altered however you like
print("It took", length, "seconds!")
Answer from Larry the Llama on Stack OverflowThe time module
The time module allows the user to directly get the time, in seconds, since 1970 (See: https://docs.python.org/3/library/time.html). This means that we can subtract the time before from time after to see how long it has been, namely how long it took the user to finish the typing test. From there, it is as easy as printing the result. You can round the time using int to get a purely seconds result without milliseconds.
The code
# Import the time library
import time
# Calculate the start time
start = time.time()
# Code here
# Calculate the end time and time taken
end = time.time()
length = end - start
# Show the results : this can be altered however you like
print("It took", length, "seconds!")
You can use the built-in time library:
import time
str_to_type = "The cat is catching a mouse."
start_time = time.perf_counter()
print("Type: '" + str_to_type + "'.")
typedstring = input()
if typedstring == str_to_type:
end_time = time.perf_counter()
run_time = end_time - start_time
print("You typed '" + str_to_type + "' in", str(run_time), "seconds.")
Videos
Try this:
t = threading.Timer(30.0, self.continousUpdate, [contractId],{} )
When you read self.continuousUpdate, the method is already bound to the object, even if you don't call it yet. You don't need to pass self again.
The reason the second version "ignores the thread" is that you call the method inside an argument to the Timer call, so it runs (and tries to call itself again) before the Timer ever gets started. That's why threading functions have you pass the function and its arguments separately (so it can call the function itself when it's ready).
Incidentally, you spelled "continuous" wrong.
Just remove the self parameter.
Here is a full working solution:
import threading
class DataCollect():
def __init__(self):
pass
def hello(self):
print("hello, world")
t = threading.Timer(5.0, self.hello)
t.start()
dataCollect = DataCollect()
dataCollect.hello()
This depends on your code logic. But from the code supplied, most probably it is Timer from threading module, so you just have to add this at the top of your code
from threading import Timer
Documentation is here: threading.Timer
Even though this is probably not the case here given the context, it is also possible that the Timer is from the timeit module which allows to run tests on the quickness of execution.
That is, from timeit import Timer
https://docs.python.org/3/library/timeit.html
Can someone explain to me like Im 5 becuase I feel like Im being left being in my course.
Essentially i have an excerise to create a Timer class with start and end method that does the following in the test file
t = Timer() t.start()
a whole bunch of code from other exercises
t.end()
My thing I have no clue how to begin and I have watch videos a few times.
So far although very basic, I have just initialized my code solution and assigned my start and end
class Timer(object):
def __init__(self, start, end):
self.start = float(start)
self.end = float(end)
def start(self):
self.start = time.time()
def end(self):
self.end = time.time()