2
\$\begingroup\$

The idea may sound stupid, but this is what I got to know that you should take a break at regular intervals to not to strain your eyes.

So the idea is to take break of 2 minutes for every 20 minutes of work and a break of 10 minutes for every one hour. People when working sometimes works continuously without monitoring the amount of time they spend on a single stretch. Hope this make some sense and reason for developing it in python is to improve my coding skills.

#cat lock_sys.py 
import time
import subprocess

TIME = 20
BREAK1 = 2
BREAK2 = 10
COUNT = 3

def verify_pkg():

    #check if package is installed on the server
    check_pkg = subprocess.Popen(["apt", "-qq", "list", "gnome-screensaver"], stdout=subprocess.PIPE)
    pkg_avil = "installed" in check_pkg.communicate()[0]

    #if not installed, install the package
    if not pkg_avil:

        install_pkg = subprocess.Popen(["apt-get", "install", "-y", "gnome-screensaver"], stdout=subprocess.PIPE)
        check_pkg = "Setting up gnome-screensaver"    
        if check_pkg in install_pkg.communicate()[0]:
            return True

    elif pkg_avil:
        return True

def start_timer(timer):
    while timer:
        print(time.strftime("%Y-%m-%d %H:%M:%S"))
        time.sleep(60)
        timer -= 1
    return 0

def take_break(break_time):
    if verify_pkg(): 

        print("TAKE A BREAK OF {} minutes".format(break_time)) 

        #lock the screen 
        lock = subprocess.call(["/usr/bin/gnome-screensaver-command", "-l"])
        start_timer(break_time)        

        #unlock the screen
        unlock = subprocess.call(["/usr/bin/gnome-screensaver-command", "-d"])


if __name__ == "__main__":

    count = 1 
    while True:
        print("20 minute timer started with break {} minutes".format(BREAK1))
        start_timer(TIME)

        #count of 3 will make an hour. i.e 20*3=60 min
        if count == COUNT:
            print("HOUR timer started with break {} minutes".format(BREAK2))
            take_break(BREAK2)
            #intialize the count to 1 after the hourly break
            count = 1
        else:

            take_break(BREAK1)
            count += 1

Any improvements on this code are welcome.

\$\endgroup\$
2
  • \$\begingroup\$ At first sight: no shebang line and very Ubuntu-oriented - it's worth mentioning that. \$\endgroup\$ Jun 19, 2017 at 7:18
  • \$\begingroup\$ yes, this is a tested code..can you let me know what is the issue you are facing ?? \$\endgroup\$ Jun 19, 2017 at 8:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.