-
Notifications
You must be signed in to change notification settings - Fork 2
My solution for final project #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import time | ||
import threading | ||
|
||
|
||
def set_timer(): | ||
"""Setting the timer in seconds""" | ||
timer = int(input("Enter a time in seconds: ")) | ||
while True: | ||
if timer > 0: | ||
return timer | ||
else: | ||
print("Value of timer must be greater than 0.") | ||
|
||
|
||
def get_format_input(): | ||
"""Setting the format for display countdown""" | ||
print("\nChoose time format:") | ||
print("1. Complete (DD:HH:MM:SS)") | ||
print("2. Hour:Minute:Seconds") | ||
print("3. Minute:Seconds") | ||
print("4. Seconds") | ||
|
||
while True: | ||
format_select = int(input("\nEnter a time format: ")) | ||
if format_select in [1, 2, 3, 4]: | ||
return format_select | ||
else: | ||
print("Selection must be 1 to 4.") | ||
|
||
|
||
def format_display_timer(seconds, format_selected): | ||
"""Formatting the display countdown""" | ||
minutes, seconds = divmod(seconds, 60) | ||
hours, minutes = divmod(minutes, 60) | ||
days, hours = divmod(hours, 24) | ||
if format_selected == 1: | ||
return '{:02d}:{:02d}:{:02d}:{:02d}'.format(days, hours, minutes, seconds) | ||
if format_selected == 2: | ||
total_hours = days * 24 + hours | ||
return '{:02d}:{:02d}:{:02d}'.format(total_hours, minutes, seconds) | ||
if format_selected == 3: | ||
total_minutes = (days * 24 * 60) + (hours * 60) + minutes | ||
return '{:02d}:{:02d}'.format(total_minutes, seconds) | ||
if format_selected == 4: | ||
return '{:04d}'.format(seconds) | ||
|
||
|
||
def countdown_logic(initial_seconds, format_selected, pause_event, stop_event, reset_event): | ||
"""Countdown logic""" | ||
seconds = initial_seconds | ||
while seconds >= 0: | ||
if stop_event.is_set(): | ||
return | ||
|
||
if reset_event.is_set(): | ||
seconds = initial_seconds | ||
reset_event.clear() | ||
print(f"\nReset to: {format_display_timer(seconds, format_selected)}") | ||
|
||
if not pause_event.is_set(): | ||
print(f"\r{format_display_timer(seconds, format_selected)} ", end="", flush=True) | ||
time.sleep(1) | ||
seconds -= 1 | ||
else: | ||
time.sleep(0.1) # While paused | ||
if not stop_event.is_set(): | ||
print("\nTimer finished.") | ||
|
||
|
||
def handle_commands(pause, stop, reset): | ||
"""Logic for threading commands""" | ||
print("Command available: start, pause, stop, reset") | ||
|
||
while True: | ||
command = input("\n>>>").strip().lower() | ||
if command == "start": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try to use |
||
pause.clear() | ||
print("Timer started") | ||
elif command == "pause": | ||
pause.set() | ||
print("Timer paused") | ||
elif command == "reset": | ||
reset.set() | ||
print("Timer reset") | ||
elif command == "stop": | ||
stop.set() | ||
print("Timer stopped") | ||
break | ||
else: | ||
print("Command not available") | ||
|
||
|
||
def start_timer(): | ||
"""Function to start the timer """ | ||
seconds = set_timer() | ||
format_selected = get_format_input() | ||
|
||
pause = threading.Event() | ||
stop = threading.Event() | ||
reset = threading.Event() | ||
|
||
timer_thread = threading.Thread(target=countdown_logic, args=(seconds, format_selected, pause, stop, reset)) | ||
timer_thread.start() | ||
|
||
handle_commands(pause, stop, reset) | ||
|
||
timer_thread.join() | ||
print("\nTimer finished.") | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from datetime import datetime | ||
|
||
|
||
def current_date_time(): | ||
return datetime.now().strftime('%d/%m/%Y %H:%M:%S') | ||
|
||
|
||
def day_of_the_week(): | ||
return datetime.now().strftime('%A') | ||
|
||
|
||
def day_of_the_year(): | ||
return datetime.now().strftime('%j') | ||
|
||
|
||
def week_number(): | ||
return datetime.now().strftime('%W') | ||
|
||
|
||
def show_date_time(): | ||
print("--- DATA AND TIME INFORMATION ---") | ||
print(f"\nDate and time: {current_date_time()}") | ||
print(f"Day of the week: {day_of_the_week()}") | ||
print(f"Day of the year: {day_of_the_year()}") | ||
print(f"Week number: {week_number()}\n") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from main import main | ||
|
||
if __name__ == '__main__': | ||
main() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the entrypoint would be |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
def show_menu(): | ||
"""Function to display menu""" | ||
print("--- Simple Time Display ---") | ||
print("1. Display time with additional date information") | ||
print("2. Time zone conversion") | ||
print("3. Set a countdown") | ||
print("4. World clock collection") | ||
print("5. Exit") | ||
|
||
|
||
def choose_menu(): | ||
"""function to select in menu""" | ||
choice = input("Select an option: ").strip() | ||
return choice | ||
|
||
|
||
def elaborate_choice(choice): | ||
"""function to elaborate choice""" | ||
while choice in ["1", "2", "3", "4", "5"]: | ||
if choice == "1": | ||
from current_time import show_date_time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try to use black and isort for code formatting |
||
show_date_time() | ||
elif choice == "2": | ||
from timezone_conversion import show_timezone_converted | ||
show_timezone_converted() | ||
elif choice == "3": | ||
from countdown_display import start_timer | ||
start_timer() | ||
elif choice == "4": | ||
from world_clock import world_clock | ||
world_clock() | ||
elif choice == "5": | ||
print("Thank you for using this program!") | ||
exit() | ||
else: | ||
print("Invalid selection.") | ||
|
||
def main(): | ||
"""Main function""" | ||
while True: | ||
show_menu() | ||
choice = choose_menu() | ||
elaborate_choice(choice) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from datetime import datetime | ||
from zoneinfo import ZoneInfo | ||
|
||
|
||
def get_timezones_available(): | ||
"""Function for retrieving available timezones.""" | ||
return { | ||
'1': ('Europe/Rome', 'Rome, Italy'), | ||
'2': ('Europe/London', 'London, United Kingdom'), | ||
'3': ('America/New_York', 'New York, USA'), | ||
'4': ('America/Los_Angeles', 'Los Angeles, USA'), | ||
'5': ('Asia/Tokyo', 'Tokyo, Japan'), | ||
'6': ('Asia/Shanghai', 'Shanghai, China'), | ||
'7': ('Australia/Sydney', 'Sydney, Australia'), | ||
'8': ('Europe/Paris', 'Paris, France'), | ||
'9': ('Asia/Dubai', 'Dubai, UAE'), | ||
'10': ('America/Sao_Paulo', 'São Paulo, Brazil') | ||
} | ||
|
||
|
||
def show_timezones(): | ||
"""function to display available timezones.""" | ||
time_zones = get_timezones_available() | ||
for timezone, (tz, names) in time_zones.items(): | ||
print(f"{timezone:2s}: {names}") | ||
|
||
|
||
def get_timezone_selection(): | ||
"""Function for to select available timezones.""" | ||
timezones = get_timezones_available() | ||
while True: | ||
selection = input(f"\nChoose a timezone to convert (1:{len(timezones)}): ").strip() | ||
if selection in timezones: | ||
return timezones[selection] | ||
else: | ||
print("Invalid selection") | ||
|
||
|
||
def show_timezone_converted(): | ||
"""Function that displays the timezone selected by user """ | ||
print("\n--- Time Zone Conversion ---") | ||
print("\nSelect a time zone to see the current time in that part of the world") | ||
print("\nAvailable timezones:") | ||
show_timezones() | ||
time_zone = get_timezone_selection() | ||
display_time = datetime.now(ZoneInfo(time_zone[0])) | ||
print(f"\nCurrent time in {time_zone[1]}: {display_time.strftime('%H:%M:%S')}") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
from datetime import datetime | ||
from zoneinfo import ZoneInfo | ||
|
||
|
||
def timezone_list(): | ||
"""Function that returns a list of all timezone names.""" | ||
return { | ||
'1': ('Europe/Rome', 'Rome, Italy'), | ||
'2': ('Europe/London', 'London, United Kingdom'), | ||
'3': ('America/New_York', 'New York, USA'), | ||
'4': ('America/Los_Angeles', 'Los Angeles, USA'), | ||
'5': ('Asia/Tokyo', 'Tokyo, Japan'), | ||
'6': ('Asia/Shanghai', 'Shanghai, Cina'), | ||
'7': ('Australia/Sydney', 'Sydney, Australia'), | ||
'8': ('Europe/Paris', 'Parigi, France'), | ||
'9': ('Asia/Dubai', 'Dubai, UAE'), | ||
'10': ('America/Sao_Paulo', 'San Paolo, Brazil') | ||
} | ||
|
||
|
||
def display_available_timezones(): | ||
print('Available timezones:') | ||
for key, (tz, label) in timezone_list().items(): | ||
print(f'{key}: {label}') | ||
|
||
|
||
def select_timezone(): | ||
timezones = timezone_list() | ||
selection = input(f"Which timezone would you like to select (1: {len(timezones)})? ").strip() | ||
if selection in timezones: | ||
return timezones[selection] | ||
|
||
else: | ||
print("That's not a valid timezone!") | ||
return None | ||
|
||
|
||
def add_clock(clocks): | ||
"""Function that adds a clock to the clocks list.""" | ||
timezone = select_timezone() | ||
if timezone and timezone not in clocks: | ||
clocks.append(timezone) | ||
print(f'Added {timezone[1]}') | ||
elif timezone: | ||
print(f'Timezone {timezone[1]} already exists') | ||
return clocks | ||
|
||
|
||
def get_clock_to_remove(clocks): | ||
"""Function that returns the timezone name to remove.""" | ||
while True: | ||
try: | ||
index = int(input(f"Which timezone would you like to remove (1: {len(clocks)})? ")) | ||
if 0 <= index < len(clocks): | ||
return index | ||
else: | ||
print("That's not a valid timezone!") | ||
except ValueError: | ||
print("That's not a valid timezone!") | ||
|
||
|
||
def remove_clock(clocks): | ||
"""Function that removes a clock from the clocks list.""" | ||
if not clocks: | ||
print("No clocks to remove") | ||
return | ||
|
||
print("--- REMOVE CLOCK ---") | ||
display_world_clocks(clocks) | ||
|
||
index = get_clock_to_remove(clocks) | ||
removed = clocks.pop(index-1) | ||
print(f'Removed {removed[1]}') | ||
|
||
|
||
|
||
|
||
def display_world_clocks(clocks): | ||
"""Function that displays the clocks list.""" | ||
print("\n--- WORLD CLOCK ---") | ||
for tz_str, label in clocks: | ||
now = datetime.now(ZoneInfo(tz_str)) | ||
print(f'{label:25s}: {now.strftime("%Y-%m-%d %H:%M:%S")}') | ||
print("============") | ||
|
||
def world_clock(): | ||
"""Function that displays the world clock.""" | ||
clocks = [] | ||
while True: | ||
print("\nMenu:") | ||
print("1. Show timezone clocks") | ||
print("2. Add timezone clocks") | ||
print("3. Remove timezone clocks") | ||
print("4. Exit") | ||
choiche = input("Select an option: ").strip() | ||
if choiche == '1': | ||
if clocks: | ||
display_world_clocks(clocks) | ||
else: | ||
print("No clocks added yet") | ||
|
||
elif choiche == '2': | ||
display_available_timezones() | ||
add_clock(clocks) | ||
|
||
elif choiche == '3': | ||
remove_clock(clocks) | ||
|
||
elif choiche == '4': | ||
print("Quitting") | ||
break | ||
else: | ||
print("That's not a valid option!") | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this func contains a bug, if you enter a
number <= 0
it starts an infinite loop (and this is the reason why we have to avoid infinite loops) without exit caused by absence of another input inside the loop.This changed version would work correctly:
while (timer := int(input("Enter a time in seconds: "))) <= 0:
print("Value of timer must be greater than 0.")
return timer