003/Python-Project

/Python-Project/Python_Project/04_Python-Project/02_Functions/03_Functions/01_Functions/04_Functions.py
# Python-Project
Python Project

/Python-Project/Python_Project/04_Python-Project/01_Tuples/02_Tuples/01_Tuples.py
from datetime import datetime, timedelta
from typing import Optional

class DateCalculator:
def __init__(self, date: str):
self.date = datetime.strptime(date, "%Y-%m-%d")

def add_days(self, days: int) -> str:
"""Return the date after adding the given number of days."""
return self.date + timedelta(days=days)

def add_weeks(self, weeks: int) -> str:
"""Return the date after adding the given number of weeks."""
return self.date + timedelta(weeks=weeks)

def add_months(self, months: int) -> str:
"""Return the date after adding the given number of months."""
# Convert the date to a datetime object
date = datetime.strptime(self.date, "%Y-%m-%d")
# Calculate the end of the month
end_of_month = date.replace(day=28) + timedelta(days=4) # this will remove the last day of the month
next_month = end_of_month + timedelta(days=2) # this will remove the last day of the next month
# Calculate the number of days in the next month
next_month_days = next_month.day
# Calculate the total number of days in the next month
total_days = 0
for i in range(1, next_month_days + 1):
total_days += i
# Calculate the number of days to add to the date to get the end of the next month
days_to_add = total_days - date.timetuple().tm_mday + 1
# Add the number of days to the date
return date + timedelta(days=days_to_add)

def add_years(self, years: int) -> str:
"""Return the date after adding the given number of years."""
return self.date.replace(year=self.date.year + years)

def main():
date_calculator = DateCalculator("2023-01-01")
print(date_calculator.add_days(10))
print(date_calculator.add_weeks(4))
print(date_calculator.add_months(3))
print(date_calculator.add_years(1))

if __name__ == "__main__":
main()

/Python-Project/Python_Project/04_Python-Project/01_Tuples/01_Tuples/01_Tuples.py
from typing import Optional, Union

def find_max_value(numbers: Optional[Union[list, tuple]], index: Optional[int] = 0) -> Optional[Union[int, float]]:
"""Find the maximum value in a list or tuple of numbers."""
if numbers is None:
return None
elif isinstance(numbers, (list, tuple)):
if index >= len(numbers) or index < 0:
return None
else:
current_max = numbers[index]
for num in numbers:
if num > current_max:
current_max = num
return current_max
else:
return None

def find_min_value(numbers: Optional[Union[list, tuple]], index: Optional[int] = 0) -> Optional[Union[int, float]]:
"""Find the minimum value in a list or tuple of numbers."""
if numbers is None:
return None
elif isinstance(numbers, (list, tuple)):
if index >= len(numbers) or index < 0:
return None
else:
current_min = numbers[index]
for num in numbers:
if num < current_min:
current_min = num
return current_min
else:
return None

if __name__ == '__main__':
# Test the find_max_value and find_min_value functions
numbers = [1, 2, 3, 4, 5]
print(find_max_value(numbers, 2))
print(find_min_value(numbers, 2))

# Test with None as input
print(find_max_value(None, 2))
print(find_min_value(None, 2))

# Test with an empty list or tuple
print(find_max_value([]))
print(find_min_value(()))

# Test with a non-numeric list or tuple
print(find_max_value(["a", "b", "c"]))
print(find_min_value(["a", "b", "c"]))

/Python-Project/Python_Project/04_Python-Project/04_Functions/01_Functions/05_Functions.py
import random
import string

def generate_password(length: int = 8):
"""Generate a random password of a given length."""
if length < 1:
raise ValueError("Length must be at least 1.")
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password

def main():
print(generate_password(16))

if __name__ == '__main__':
main()

/Python-Project/Python_Project/04_Python-Project/02_Functions/02_Functions/04_Functions.py
# Python-Project
Python Project

/Python-Project/Python_Project/04_Python-Project/01_Tuples/03_Tuples/03_Tuples.py
import sys

def reverse_string(string