-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClockTimer.py
More file actions
152 lines (127 loc) · 4.94 KB
/
ClockTimer.py
File metadata and controls
152 lines (127 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
##new file
import time
import threading
'''user = int(input('enter time to sleep: ')) + 1;
for i in range(1,user):
print(i)
time.sleep(1);
print("wake up"); '''
'''print(time.ctime(0)); ##epoch time from time 0
print(time.ctime(2000)); ##2000 seconds after epoch time
print(time.time()); ##will return current time in seconds'''
##blackbox
'''###to convert this into hours and minutes we can use the following code
hours = time.time() // 3600 #number of hours since epoch
minutes = (time.time() - hours * 3600) // 6
print("%d:%d" % (hours, minutes))'''
##current readable date
#print(time.ctime(time.time())); ##epoch returns the time when the comp thinks time started, which accepts an argument, today's time in seconds
##another way to get current date
#print(time.localtime()); #although this is not a readable format.
'''##readable string format time, needs 2 arguments
##print(time.strftime(format,time-object)); #syntax for this method, you specify the format using identifiers, e.g
time_object = time.localtime()
local_time = time.strftime("%B %d %Y %H: %M: %S" , time_object)
## B = month name, d=day, y=year, H = hour, M= month, S= second
print(local_time)'''
####THREADING
'''#to print the number of threads running in the background
print(threading.active_count());
##to print a list of all the threads that are running we use
print(threading.enumerate());'''
##tasks to be done sequntially
'''
def wake_Up():
time.sleep(3) ##time it took for me cfinally wake up
print("I'm awake now")
def breakfast():
time.sleep(5) ##how long I take to eat my breakfat
print("finished Breakfast")
def study():
time.sleep(4)
print("Finished studying")
we can have the three task running concurrently as if multitasking ----This is called Multithreading
currently we have one thread that is running these three functions, we can hav multiple threads, where each will be in
charge of each task, then we have our main thread running in the background to complete the program, this is how we'll do it
t1 = threading.Thread(target=wake_Up, args=()); #you include the argument if the function has any
t1.start()
t2 = threading.Thread(target=breakfast, args=())
t2.start();
t3 = threading.Thread(target=study);
t3.start(); '''
##this program with multiple threads will take a shorter time to complete, considering each thread has its own objective
#to accomplish. The main one will not wait around for the other threads to complete their tasks.
###THREAD SYNCHRONIZATION
'''this is where our main thread waits around for other threads to synchronize and join, to complete their execution,
for it to move on with its own instruction set, we use the join function
t1.join()
t2.join()
t3.join()
wake_Up()
breakfast()
study()
##these tasks will run sequentially
print(threading.active_count());
print(threading.enumerate)
print(time.perf_counter()) #will return the time it took for the main thread to complete its set of instruction '''
'''our main thread is not in charge of executing the three functions, but instead in charge of creating three additional
threads and then calling the active count function,the enumarate function and perf_counter function'''
##understanding basis of multithreading
'''a function that will occur the same time as we wait for user input'''
'''done = False
def work():
count = 0
while not done:
time.sleep(1)
count += 1
print(count)
#print('press enter to exit loop')
threading.Thread(target=work).start()
user = input('press enter to exit loop')
done = True'''
'''example of a code from chat gpt explaning threading concept of how multiple files are downloaded concurrently'''
'''import threading
import time
def download_file(file_name):
print(f"Downloading {file_name}... Started")
time.sleep(2) # Simulating the download process
print(f"Downloading {file_name}... Completed")
# List of files to download
files_to_download = ["file1.txt", "file2.txt", "file3.txt"]
# Create a thread for each file download
threads = []
for file in files_to_download:
thread = threading.Thread(target=download_file, args=(file,))
threads.append(thread)
thread.start()
# Wait for all threads to finish
for thread in threads:
thread.join()
print("All downloads completed.") '''
'''CLOCK TIMER'''
##when we're counting down backwards
'''user = int(input('enter time in seconds: '))
count = user
while count!= 1:
time.sleep(1)
count -=1
print(count)
print('time is up!') '''
'''#testing
count = int(input('enter number'));
print(count)
second = count % 60
print(second) '''
##counting down onwards
#user = int(input('enter time in seconds: '))
count = int(input('enter time in seconds: '))
while count > 0:
count -=1
time.sleep(1)
seconds = (count % 60)
minutes = int((count / 60)%60)
hours = int((count/ 3600) % 60)
#print( f" {hours:02}:{minutes:02}:{seconds:02}")
time_format = "{:02}: {:02}: {:02}".format(hours, minutes, seconds)
print(time_format)
print('time is up!')