Posts: 1,298
Threads: 38
Joined: Sep 2016
So I started what I thought would be a pretty simple project, but through perseverance was able to turn it into something complicated. It's rather long, 227 lines, and works with Python 3.4 and 3.6, on Windows 10 and OpenSuse 42.2 Linux. You will need the third party libraries "colorama" and "cursor", both of which can be installed with "pip". Right now, the display is in "cheater" mode, in that it shows the numbers, but if you're nerdy enough, you can change it to display any single character you want (within reason) (no, I did not try the unicode 'Pile of Poo'). I could only think of using a counter (default is 1 minute) and using "Control + C" to exit before the timer runs out. I tried to make it as a gui, but, well I have no talent what so ever in that regard and it rapidly turned into a major cluster f--k, so a plain terminal is what you get
Any suggestions would be appreciated. Enjoy
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
import colorama as cr
import cursor
# Hide cursor to eliminate artifacts
cursor.hide()
def hours(hrs1, hrs2):
display = cr.Fore.RED
h_10 = cr.Cursor.POS(5, 13) # 0
h_11 = cr.Cursor.POS(5, 11) # 2^0
h_12 = cr.Cursor.POS(5, 9) # 2^1
h_00 = cr.Cursor.POS(8, 13) # 0
h_01 = cr.Cursor.POS(8, 11) # 2^0
h_02 = cr.Cursor.POS(8, 9) # 2^1
h_04 = cr.Cursor.POS(8, 7) # 2^2
h_08 = cr.Cursor.POS(8, 5) # 2^3
# Hours 10's
if hrs1 == "0":
print(display + h_10 + "0")
if hrs1 == "1":
print(display + h_11 + "1")
if hrs1 == "2":
print(display + h_12 + "2")
# Hours units
if hrs2 == "0":
print(display + h_00 + "0")
if hrs2 == "1":
print(display + h_01 + "1")
if hrs2 == "2":
print(display + h_02 + "2")
if hrs2 == "3":
print(display + h_01 + "1")
print(display + h_02 + "2")
if hrs2 == "4":
print(display + h_04 + "4")
if hrs2 == "5":
print(display + h_01 + "1")
print(display + h_04 + "4")
if hrs2 == "6":
print(display + h_02 + "2")
print(display + h_04 + "4")
if hrs2 == "7":
print(display + h_01 + "1")
print(display + h_02 + "2")
print(display + h_04 + "4")
if hrs2 == "8":
print(display + h_08 + "8")
if hrs2 == "9":
print(display + h_01 + "1")
print(display + h_08 + "8")
return
def minutes(min1, min2):
display = cr.Fore.RED
m_10 = cr.Cursor.POS(13, 13) # 0
m_11 = cr.Cursor.POS(13, 11) # 2^0
m_12 = cr.Cursor.POS(13, 9) # 2^1
m_13 = cr.Cursor.POS(13, 7) # 2^2
m_00 = cr.Cursor.POS(16, 13) # 0
m_01 = cr.Cursor.POS(16, 11) # 2^0
m_02 = cr.Cursor.POS(16, 9) # 2^1
m_04 = cr.Cursor.POS(16, 7) # 2^2
m_08 = cr.Cursor.POS(16, 5) # 2^3
# Minutes 10's
if min1 == "0":
print(display + m_10 + "0")
if min1 == "1":
print(display + m_11 + "1")
if min1 == "2":
print(display + m_12 + "2")
if min1 == "3":
print(display + m_11 + "1")
print(display + m_12 + "2")
if min1 == "4":
print(display + m_13 + "4")
if min1 == "5":
print(display + m_11 + "1")
print(display + m_13 + "4")
# Minutes units
if min2 == "0":
print(display + m_00 + "0")
if min2 == "1":
print(display + m_01 + "1")
if min2 == "2":
print(display + m_02 + "2")
if min2 == "3":
print(display + m_01 + "1")
print(display + m_02 + "2")
if min2 == "4":
print(display + m_04 + "4")
if min2 == "5":
print(display + m_01 + "1")
print(display + m_04 + "4")
if min2 == "6":
print(display + m_02 + "2")
print(display + m_04 + "4")
if min2 == "7":
print(display + m_01 + "1")
print(display + m_02 + "2")
print(display + m_04 + "4")
if min2 == "8":
print(display + m_08 + "8")
if min2 == "9":
print(display + m_01 + "1")
print(display + m_08 + "8")
return
def seconds(sec1, sec2):
display = cr.Fore.RED
s_10 = cr.Cursor.POS(21, 13) # 0
s_11 = cr.Cursor.POS(21, 11) # 2^0
s_12 = cr.Cursor.POS(21, 9) # 2^1
s_13 = cr.Cursor.POS(21, 7) # 2^2
s_00 = cr.Cursor.POS(24, 13) # 0
s_01 = cr.Cursor.POS(24, 11) # 2^0
s_02 = cr.Cursor.POS(24, 9) # 2^1
s_04 = cr.Cursor.POS(24, 7) # 2^2
s_08 = cr.Cursor.POS(24, 5) # 2^3
# Seconds 10's
if sec1 == "0":
print(display + s_10 + "0")
if sec1 == "1":
print(display + s_11 + "1")
if sec1 == "2":
print(display + s_12 + "2")
if sec1 == "3":
print(display + s_11 + "1")
print(display + s_12 + "2")
if sec1 == "4":
print(display + s_13 + "4")
if sec1 == "5":
print(display + s_11 + "1")
print(display + s_13 + "4")
# Seconds units
if sec2 == "0":
print(display + s_00 + "0")
if sec2 == "1":
print(display + s_01 + "1")
if sec2 == "2":
print(display + s_02 + "2")
if sec2 == "3":
print(display + s_01 + "1")
print(display + s_02 + "2")
if sec2 == "4":
print(display + s_04 + "4")
if sec2 == "5":
print(display + s_01 + "1")
print(display + s_04 + "4")
if sec2 == "6":
print(display + s_02 + "2")
print(display + s_04 + "4")
if sec2 == "7":
print(display + s_01 + "1")
print(display + s_02 + "2")
print(display + s_04 + "4")
if sec2 == "8":
print(display + s_08 + "8")
if sec2 == "9":
print(display + s_01 + "1")
print(display + s_08 + "8")
time.sleep(1) # Needed to cut down "flicker" as best as possible
return
##########################################
def main():
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
hours(str_t[0], str_t[1])
minutes(str_t[2], str_t[3])
seconds(str_t[4], str_t[5])
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()
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 2,953
Threads: 48
Joined: Sep 2016
Dec-30-2016, 10:55 PM
(This post was last modified: Dec-30-2016, 10:55 PM by wavic.)
200+ lines of code... You should put it on github or somewhere else as a file.
I don't feel it like a binary. No 0 1 sequence. But is a quite interesting idea. :D
I have another one. Or two. Hex clock. Percentage clock ( instead of hours, minutes and seconds percent from 24 hours as HH, percent from 60 minutes for MM and so on)
00%:90% for 00:54 as it is now. No seconds here
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
i like the % clock idea ... basically base 100
could just have a clock with a choosable number base
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 591
Threads: 26
Joined: Sep 2016
Dec-31-2016, 05:23 AM
(This post was last modified: Dec-31-2016, 05:24 AM by Mekire.)
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()
Posts: 1,298
Threads: 38
Joined: Sep 2016
Sweet sassy molassey. First, it runs fine. Second, I am in awe that you were able to reduce it to so few lines of code. I will have to look at it more closely later today and see if I can follow along with what you did. Just amazing.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 591
Threads: 26
Joined: Sep 2016
Mucked around with it a bit more and tried to clean things up (also changed display to binary):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
import cursor
import colorama as cr
TAB = " " * 4
DISPLAY = cr.Style.BRIGHT + cr.Fore.RED
ON = cr.Style.BRIGHT + cr.Fore.GREEN
OFF = DISPLAY
COLUMN_POSITIONS = (5, 8, 13, 16, 21, 24)
def get_cursor_locations(column):
locations = {2**i : cr.Cursor.POS(column, 9-2*i) for i in range(4)}
locations[0] = cr.Cursor.POS(column, 11)
return locations
def render(digit, cursors):
binary = "{:0<4}".format(bin(digit)[:1:-1])
for i,bit in enumerate(binary):
color = ON if int(bit) else OFF
print("{}{}{}".format(color, cursors[2**i], bit))
def startup():
cursor.hide()
cr.init()
def cleanup(message):
print(DISPLAY + "\n" * 8)
print(message)
cursor.show()
cr.deinit()
def run(seconds):
for _ in range(seconds):
os.system('cls' if os.name == 'nt' else 'clear')
time_digits = map(int, time.strftime("%H%M%S"))
print(TAB.join([DISPLAY, "HRS", " MIN", " SEC"]))
for digit, column in zip(time_digits, COLUMN_POSITIONS):
render(digit, get_cursor_locations(column))
time.sleep(1)
def main():
startup()
seconds_to_run = 500
try:
run(seconds_to_run)
cleanup("{} second timer expired.".format(seconds_to_run))
except KeyboardInterrupt:
cleanup("Program stopped.")
if __name__ == "__main__":
main() Anyway, not trying to steal your thunder; just having a bit of fun.
Posts: 1,298
Threads: 38
Joined: Sep 2016
Nice. And pretty holiday colors  . The one thing I notice is the lack of row 5 (0), so midnight 00:00:00 won't show up.
Quote:Anyway, not trying to steal your thunder; just having a bit of fun.
Steal all the thunder you want; you get some fun and I get some educating, it's all good
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 591
Threads: 26
Joined: Sep 2016
Jan-01-2017, 12:13 AM
(This post was last modified: Jan-01-2017, 12:21 AM by Mekire.)
(Dec-31-2016, 10:17 PM)sparkz_alot Wrote: The one thing I notice is the lack of row 5 (0), so midnight 00:00:00 won't show up. Midnight should work just fine. It will be all zeros.
Anyway, latest version. Flickering fixed; update speed increased so we don't miss some seconds (as could happen before); deciseconds added; some overall simplifications:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
import cursor
import colorama as cr
from datetime import datetime
TAB = " " * 4
UPDATE_SPEED = 1 / 60.0
DISPLAY = cr.Style.BRIGHT + cr.Fore.RED
ON = cr.Style.BRIGHT + cr.Fore.GREEN
OFF = DISPLAY
COLUMN_POSITIONS = (5, 7, 13, 15, 21, 23, 29)
START_ROW = 3
def get_cursor_locations(column):
return [cr.Cursor.POS(column, START_ROW+2*i) for i in range(4)]
def render(digit, cursors):
binary = bin(digit)[2:].zfill(4)
for bit, cursor in zip(binary, cursors):
color = ON if int(bit) else OFF
print("{}{}{}".format(color, cursor, bit))
def startup():
cursor.hide()
cr.init()
def cleanup(message):
print(DISPLAY + "\n" * 8)
print(message)
cursor.show()
cr.deinit()
def run(seconds):
print(TAB.join([DISPLAY, "HRS", " MIN", " SEC"]))
start_time = time.clock()
while time.clock() - start_time < seconds:
time_digits = map(int, datetime.now().strftime("%H%M%S%f"))
for digit, column in zip(time_digits, COLUMN_POSITIONS):
render(digit, get_cursor_locations(column))
time.sleep(UPDATE_SPEED)
def main():
startup()
seconds_to_run = 60
try:
run(seconds_to_run)
cleanup("{} second timer expired.".format(seconds_to_run))
except KeyboardInterrupt:
cleanup("Program stopped.")
if __name__ == "__main__":
main()
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
turn the volume up, there be thunder emanating from the board!
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 1,298
Threads: 38
Joined: Sep 2016
Fantastic and I like the way the flicker is completely gone.
I did make 2 minor changes (my turn to apologize  ) .
First, I added the 'clear screen' to your 'startup' function:
def startup():
os.system('cls' if os.name == 'nt' else 'clear')
cursor.hide()
cr.init() and second, I added "DS" to the new deci-second column:
print(TAB.join([DISPLAY, "HRS", " MIN", " SEC", " DS"])) Great to see how you turned my clunker into a Rolls Royce. :-D
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
|