-
Notifications
You must be signed in to change notification settings - Fork 22
test #2
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
Open
test #2
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
106 changes: 106 additions & 0 deletions
src/utils.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,106 @@ | ||
import shutil | ||
import os | ||
|
||
from datetime import datetime, timedelta | ||
|
||
|
||
def seconds_to_readable_time(seconds): | ||
if not seconds: | ||
return None | ||
""" | ||
Convert milliseconds to readable time | ||
""" | ||
minutes = seconds // 60 | ||
hours = minutes // 60 | ||
|
||
seconds = seconds % 60 | ||
minutes = minutes % 60 | ||
|
||
time_string = "" | ||
|
||
if hours > 0: | ||
time_string += f"{round(hours)}h" | ||
if minutes > 0: | ||
time_string += f"{round(minutes)}m" | ||
if seconds > 0 or time_string == "": | ||
time_string += f"{round(seconds)}s" | ||
|
||
return time_string | ||
|
||
|
||
def format_percentage_change(daily_value, prev_period_value): | ||
if not daily_value or not prev_period_value: | ||
return None | ||
if prev_period_value == 0: | ||
if daily_value > 0: | ||
return "(➚100%)" | ||
elif daily_value < 0: | ||
return "(➘100%)" | ||
else: | ||
return "(≈0%)" | ||
|
||
difference = ((daily_value - prev_period_value) / prev_period_value) * 100 | ||
if difference >= 0.1: | ||
return f"(➚{difference:.1f}%)" | ||
elif difference < 0: | ||
return f"(➘{abs(difference):.1f}%)" | ||
else: | ||
return "(≈0%)" | ||
|
||
|
||
def get_start_end_of_week_by_offset(week_offset) -> tuple: | ||
if not week_offset: | ||
return None | ||
""" | ||
Returns the start (Monday) and end (Sunday) dates of the week determined by a given week offset | ||
relative to the current date. | ||
Args: | ||
week_offset (int): The offset (number of weeks) from the current week. | ||
- week_offset = 0: The current week. | ||
- week_offset = 1: The previous week. | ||
- week_offset = -1: The next week. | ||
Returns: | ||
tuple: A tuple containing the start (Monday) and end (Sunday) dates of the specified week in 'YYYY-MM-DD' format. | ||
""" | ||
today = datetime.today() | ||
start_of_week = ( | ||
today - timedelta(days=today.weekday()) - timedelta(weeks=week_offset) | ||
) | ||
end_of_week = start_of_week + timedelta(days=6) | ||
return start_of_week, end_of_week | ||
|
||
|
||
def free_up_disk_space(to_remove: str): | ||
if not to_remove: | ||
return None | ||
print("Cleaning up...", to_remove) | ||
try: | ||
if os.path.isdir(to_remove): | ||
shutil.rmtree(to_remove) | ||
print(f"Folder {to_remove} has been removed.") | ||
elif os.path.isfile(to_remove): | ||
os.remove(to_remove) | ||
print(f"File {to_remove} has been removed.") | ||
except FileNotFoundError: | ||
print(f"{to_remove} not found.") | ||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
|
||
|
||
def format_bytes(bytes) -> str: | ||
if not bytes: | ||
return None | ||
|
||
units = ["TB", "GB", "MB", "KB"] | ||
sizes = [ | ||
1024**4, # TB | ||
1024**3, # GB | ||
1024**2, # MB | ||
1024**1, # KB | ||
] | ||
|
||
for i in range(len(sizes)): | ||
if bytes >= sizes[i]: | ||
return f"{(bytes / sizes[i]):.2f} {units[i]}" | ||
|
||
return f"{bytes} B" # For values smaller than 1 KB |
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.