-
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
Open
Danielef12
wants to merge
3
commits into
tomorrowdevs-projects:main
Choose a base branch
from
Danielef12:solution/final-project
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
130 changes: 130 additions & 0 deletions
130
projects/final-project/solution/danieleFiocca/simple_time_display/countdown_display.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import threading | ||
import time | ||
|
||
|
||
def set_timer(): | ||
"""Setting the timer in seconds""" | ||
while (timer := int(input("Enter time in seconds: "))) <= 0: | ||
print("Invalid time. Time must be greater than 0.") | ||
return timer | ||
|
||
|
||
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 not stop.is_set(): | ||
command = input("\n>>>").strip().lower() | ||
match command: | ||
case "start": | ||
pause.clear() | ||
print("time started") | ||
case "pause": | ||
pause.set() | ||
print("time paused") | ||
case "reset": | ||
reset.set() | ||
print("time reset") | ||
case "stop": | ||
stop.set() | ||
print("time stopped") | ||
|
||
case _: | ||
print("Invalid command") | ||
"""if command == "start": | ||
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.") |
25 changes: 25 additions & 0 deletions
25
projects/final-project/solution/danieleFiocca/simple_time_display/current_time.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
48 changes: 48 additions & 0 deletions
48
projects/final-project/solution/danieleFiocca/simple_time_display/index.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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 | ||
|
||
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) |
4 changes: 4 additions & 0 deletions
4
projects/final-project/solution/danieleFiocca/simple_time_display/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from index import main | ||
|
||
if __name__ == "__main__": | ||
main() |
49 changes: 49 additions & 0 deletions
49
projects/final-project/solution/danieleFiocca/simple_time_display/timezone_conversion.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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')}") |
116 changes: 116 additions & 0 deletions
116
projects/final-project/solution/danieleFiocca/simple_time_display/world_clock.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
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") | ||
choice = input("Select an option: ").strip() | ||
if choice == "1": | ||
if clocks: | ||
display_world_clocks(clocks) | ||
else: | ||
print("No clocks added yet") | ||
|
||
elif choice == "2": | ||
display_available_timezones() | ||
add_clock(clocks) | ||
|
||
elif choice == "3": | ||
remove_clock(clocks) | ||
|
||
elif choice == "4": | ||
print("Quitting") | ||
break | ||
else: | ||
print("That's not a valid option!") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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