Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary Clock
#4
Can't promise you I didn't make any mistakes here, but something to think about in terms of reducing a lot of that redundant code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
import os
import time
import cursor
import colorama as cr
 

COLUMN_POSITIONS = (5, 8, 13, 16, 21, 24)
 

def get_cursor_locations(column):
    locations = {2**i : cr.Cursor.POS(column, 11-2*i) for i in range(5)}
    locations[0] = cr.Cursor.POS(column, 13)
    return locations


def render(display, digit, cursors):
    binary = bin(int(digit))[:1:-1]
    if not any(int(bit) for bit in binary):
        print("{}{}{}".format(display, cursors[0], 0))
    else:
        for i,bit in enumerate(binary):
            if int(bit):
                print("{}{}{}".format(display, cursors[2**i], 2**i))


def main():
    cursor.hide()
    try:
        for c in range(60):    # Currently set for 1 min. (1 hr = 3600, 24 hrs = 86400)
            os.system('cls' if os.name == 'nt' else 'clear')
 
            # Get current time: hours (24 hour clock), minutes, seconds) and put in a list
            t_now = [time.strftime("%H"), time.strftime("%M"), time.strftime("%S")]
            str_t = "".join(t_now)
 
            # Initialize and set some colors for colorama
            cr.init()
            off = cr.Back.BLACK
            on = cr.Fore.RED
 
            # Print Header
            tab = " " * 4
            print(off)
            print(on + tab + 'HRS' + tab + ' MIN' + tab + ' SEC' + "\n")
 
            # Run clock
            display = cr.Fore.RED
            for digit, column in zip(str_t, COLUMN_POSITIONS):
                render(display, digit, get_cursor_locations(column))
            
            time.sleep(1)
    except KeyboardInterrupt:
        print("\n" * 8)
        print("Program stopped")
        print(cr.deinit())
        cursor.show()  # Show cursor again
        exit(0)
 
    print("\n" * 8)
    print(cr.deinit())
    cursor.show()    # Show cursor again

 
if __name__ == "__main__":
    main()
Reply


Messages In This Thread
Binary Clock - by sparkz_alot - Dec-30-2016, 08:07 PM
RE: Binary Clock - by wavic - Dec-30-2016, 10:55 PM
RE: Binary Clock - by Skaperen - Dec-31-2016, 03:42 AM
RE: Binary Clock - by Mekire - Dec-31-2016, 05:23 AM
RE: Binary Clock - by sparkz_alot - Dec-31-2016, 02:14 PM
RE: Binary Clock - by Mekire - Dec-31-2016, 04:51 PM
RE: Binary Clock - by sparkz_alot - Dec-31-2016, 10:17 PM
RE: Binary Clock - by Mekire - Jan-01-2017, 12:13 AM
RE: Binary Clock - by Skaperen - Jan-01-2017, 07:55 AM
RE: Binary Clock - by sparkz_alot - Jan-01-2017, 03:23 PM
RE: Binary Clock - by wavic - Jan-01-2017, 06:39 PM
RE: Binary Clock - by sparkz_alot - Jan-02-2017, 03:02 PM
RE: Binary Clock - by wavic - Jan-02-2017, 03:29 PM
RE: Binary Clock - by sparkz_alot - Jan-02-2017, 03:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Playing around with a clock's seconds in tkinter menator01 1 2,501 Feb-12-2022, 04:32 PM
Last Post: menator01
  Simple wxpython digital clock menator01 2 4,103 Feb-20-2021, 05:47 PM
Last Post: menator01
  My Attempt at an alarm clock menator01 0 2,466 May-15-2020, 06:28 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020