-
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 1 commit
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 was deleted.
Oops, something went wrong.
Empty file.
111 changes: 111 additions & 0 deletions
111
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,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.") | ||
|
||
|
||
|
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") |
4 changes: 4 additions & 0 deletions
4
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,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 |
Oops, something went wrong.
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