Last active
May 13, 2020 09:04
-
-
Save NP-chaonay/26ba5f6c401a1d941cb9133b61da5cfe to your computer and use it in GitHub Desktop.
Specialized-purpose timer #1 : It can adjust "from-to duration" and when exit the program, returns the recent-counting and accumulated time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import time | |
start=time.time() | |
end=time.time() | |
latency=end-start | |
import signal,subprocess,sys,threading,os,random | |
def is_str_is_integer(string): | |
if string[0]=='-': | |
n=1 | |
else: n=0 | |
for char in string[n:]: | |
if char in ['0','1','2','3','4','5','6','7','8','9']: continue | |
else: return False | |
return True | |
def sec_to_hms(sec): | |
tmp=sec%3600 | |
result=[None,None,None] | |
result[0]=int(sec//3600) | |
result[1]=int(tmp//60) | |
result[2]=round(tmp%60) | |
return result | |
def endProg(signum,frame): | |
global Is_SIGTERM_Handler_Running | |
if not Is_SIGTERM_Handler_Running: Is_SIGTERM_Handler_Running=True | |
else: return | |
end=time.time() | |
print() | |
new_duration=(end-start-latency) | |
total_duration=new_duration+from_[0]*3600+from_[1]*60+from_[2] | |
result=sec_to_hms(new_duration) | |
print('New Time used :',':'.join(map(str,result))) | |
result=sec_to_hms(total_duration) | |
print('Total Time used :',':'.join(map(str,result))) | |
exit() | |
def sndAlert(): | |
for i in range(3): | |
print('\a',end='',flush=True) | |
time.sleep(0.75) | |
# Playing song as ringtone for alerting | |
while True: | |
subprocess.run(executable='mplayer',args=['mplayer','-speed',str(random.randint(5,20)/10),'/home/np-chaonay/Music/Created/Auld-Lang-Syne_Gay-Gordon_Remix_YouTube-from-darrin42.flac'],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,stdin=subprocess.DEVNULL) | |
try: to=sys.argv[1] | |
except Exception: to='0:50:0' | |
try: from_=sys.argv[2] | |
except Exception: from_='0:0:0' | |
if to in ['','.']: to='0:50:0' | |
if from_ in ['','.']: from_='0:0:0' | |
diff=[None,None,None] | |
to=to.split(':') | |
from_=from_.split(':') | |
if len(to)!=3 or len(from_)!=3: raise ValueError('Inputted arguments should be in format "<H>:<M>:<S>".') | |
for num in to: | |
if not is_str_is_integer(num): raise ValueError('Inputted arguments should be in format "<H>:<M>:<S>".') | |
for num in from_: | |
if not is_str_is_integer(num): raise ValueError('Inputted arguments should be in format "<H>:<M>:<S>".') | |
to=list(map(int,to)) | |
from_=list(map(int,from_)) | |
for num in to: | |
if num<0: raise ValueError('Inputted arguments should be positive or zero.') | |
for num in from_: | |
if num<0: raise ValueError('Inputted arguments should be positive or zero.') | |
diff[0]=int(to[0])-int(from_[0]) | |
diff[1]=int(to[1])-int(from_[1]) | |
diff[2]=int(to[2])-int(from_[2]) | |
diff_sec=diff[0]*3600+diff[1]*60+diff[2] | |
os.environ['PULSE_PROP']='media.role=userh' | |
Is_SIGTERM_Handler_Running=False | |
signal.signal(signal.SIGINT,endProg) | |
Thread1=threading.Thread(target=print,args=['Timer has passed the specific duration.'],daemon=True) | |
Thread2=threading.Thread(target=sndAlert,daemon=True) | |
# Sending GUI notification | |
Thread3=threading.Thread(target=subprocess.run,kwargs={'executable':'notify-send','args':['notify-send','-i','/usr/share/icons/Humanity/apps/22/clock.svg','Specialized Timer','Timer has passed the specific duration.'],'stdout':subprocess.DEVNULL,'stderr':subprocess.DEVNULL,'stdin':subprocess.DEVNULL},daemon=True) | |
print('Count from :',':'.join(map(str,from_))) | |
print('Count To :',':'.join(map(str,to))) | |
while True: | |
end=time.time() | |
if end-start>=(diff_sec): | |
Thread1.start(); Thread2.start(); Thread3.start() | |
break | |
else: | |
try: time.sleep((diff_sec-(end-start))/2) | |
except Exception: continue | |
while True: | |
time.sleep(900) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment