Skip to content

Instantly share code, notes, and snippets.

@zkneupper
Last active December 26, 2024 16:35

Revisions

  1. zkneupper revised this gist Dec 22, 2017. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions bedtime.py
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,14 @@

    def computer_sleep(seconds_until_sleep=5, verbose=1):
    """Function will put the computer in sleep mode.
    Implemented for OSX and LINUX. Not yet implemented for WINDOWS.
    Args:
    seconds_until_sleep: The number of seconds until the computer
    goes to sleep.
    verbose: Verbosity mode, 0, 1. verbose=1 displays a countdown.
    """

    range_top = int(seconds_until_sleep * 10) - 1
    @@ -23,7 +26,8 @@ def computer_sleep(seconds_until_sleep=5, verbose=1):
    str(1+(_//10))+ " seconds "+ spinner_2.__next__())
    sys.stdout.flush()
    time.sleep(0.1)
    sys.stdout.write('\r')
    sys.stdout.write("\b")
    sys.stdout.flush()

    sys.stdout.write('\rGoodnight ')
    time.sleep(1)
    @@ -44,4 +48,5 @@ def computer_sleep(seconds_until_sleep=5, verbose=1):
    def spinning_cursor():
    while True:
    for cursor in '|/-\\':
    yield cursor
    yield cursor

  2. zkneupper revised this gist Dec 22, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bedtime.py
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ def computer_sleep(seconds_until_sleep=5, verbose=1):
    os.system("pmset sleepnow")
    else:
    if psutil.LINUX:
    os.system("systemctl hibernate")
    os.system("systemctl suspend")
    else:
    if psutil.WINDOWS:
    sys.exit("computer_sleep Not Implemented for WINDOWS")
  3. zkneupper created this gist Dec 19, 2017.
    47 changes: 47 additions & 0 deletions bedtime.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    import os
    import sys
    import time
    import psutil

    def computer_sleep(seconds_until_sleep=5, verbose=1):
    """Function will put the computer in sleep mode.
    Implemented for OSX and LINUX. Not yet implemented for WINDOWS.
    Args:
    seconds_until_sleep: The number of seconds until the computer
    goes to sleep.
    verbose: Verbosity mode, 0, 1. verbose=1 displays a countdown.
    """

    range_top = int(seconds_until_sleep * 10) - 1
    spinner_1 = spinning_cursor()
    spinner_2 = spinning_cursor()

    if verbose:

    for _ in range(range_top, 0, -1):
    sys.stdout.write("\r" + spinner_1.__next__() + " Sleep in "+ \
    str(1+(_//10))+ " seconds "+ spinner_2.__next__())
    sys.stdout.flush()
    time.sleep(0.1)
    sys.stdout.write('\r')

    sys.stdout.write('\rGoodnight ')
    time.sleep(1)
    sys.stdout.write('\r ')

    else:
    time.sleep(seconds_until_sleep)

    if psutil.OSX:
    os.system("pmset sleepnow")
    else:
    if psutil.LINUX:
    os.system("systemctl hibernate")
    else:
    if psutil.WINDOWS:
    sys.exit("computer_sleep Not Implemented for WINDOWS")

    def spinning_cursor():
    while True:
    for cursor in '|/-\\':
    yield cursor