diff --git a/.github/workflows/format.yaml b/.github/workflows/format.yaml index 47c901c3..8b137891 100644 --- a/.github/workflows/format.yaml +++ b/.github/workflows/format.yaml @@ -1,30 +1 @@ -name: Python Linting and formatting with Ruff -on: - push: - branches: - - main - pull_request: - branches: - - main - -jobs: - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - token: ${{ secrets.PAT }} - - - uses: actions/setup-python@v4 - with: - python-version: '3.x' - - name: Install Ruff - run: | - python -m pip install --upgrade pip - pip install ruff - - run: | - ruff format - - uses: stefanzweifel/git-auto-commit-action@v4 - with: - commit_message: 'Code formatted with Ruff' diff --git a/.vscode/settings.json b/.vscode/settings.json index 6a38d59c..f0bed7bd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,8 +3,8 @@ // "python.envFile": "${workspaceFolder}/.env",, // Test settings "python.testing.pytestEnabled": true, - // "python.testing.unittestEnabled": false, + // "python.testing.unittestEnabled": false, "python.testing.cwd": "${workspaceFolder}/tests", "python.defaultInterpreterPath": "${workspaceFolder}/2025/typescript/lokalise/.venv/bin/python", // "python.analysis.typeCheckingMode": "off" -} \ No newline at end of file +} diff --git a/2022/async/after-1/iot/service.py b/2022/async/after-1/iot/service.py index 22040d13..b7e70678 100644 --- a/2022/async/after-1/iot/service.py +++ b/2022/async/after-1/iot/service.py @@ -1,4 +1,4 @@ -import asyncio +# import asyncio import random import string from typing import Protocol diff --git a/2022/async/after-2/iot/service.py b/2022/async/after-2/iot/service.py index bacc2775..d7daa9cd 100644 --- a/2022/async/after-2/iot/service.py +++ b/2022/async/after-2/iot/service.py @@ -1,7 +1,7 @@ import asyncio import random import string -from typing import Any, Awaitable, Protocol +from typing import Protocol from iot.message import Message, MessageType diff --git a/2025/go/error_handling.py b/2025/go/error_handling.py index f900baaf..e48e0b5e 100644 --- a/2025/go/error_handling.py +++ b/2025/go/error_handling.py @@ -1,20 +1,20 @@ def read_and_sum_integers(file_path): try: - with open(file_path, "r") as file: - sum = 0 + with open(file_path, "r", encoding="utf-8") as file: + total = 0 for line in file: try: num = int(line.strip()) - sum += num + total += num except ValueError as e: raise ValueError( f"Invalid content in file: '{line.strip()}'" ) from e - return sum + return total except FileNotFoundError as e: raise FileNotFoundError(f"File not found: {file_path}") from e - except Exception as e: - raise RuntimeError(f"An unexpected error occurred: {e}") from e + except OSError as e: + raise RuntimeError(f"An unexpected OS error occurred: {e}") from e def main(): @@ -23,13 +23,8 @@ def main(): try: total = read_and_sum_integers(file_path) print(f"Sum of integers: {total}") - except FileNotFoundError as e: - print(f"Error: {e}") - except ValueError as e: + except (FileNotFoundError, ValueError, RuntimeError) as e: print(f"Error: {e}") - except Exception as e: - print(f"Unexpected error: {e}") - if __name__ == "__main__": main() diff --git a/2025/simple/before.py b/2025/simple/before.py index c8e5c61c..f55cab0c 100644 --- a/2025/simple/before.py +++ b/2025/simple/before.py @@ -33,12 +33,14 @@ def take_a_holiday(self, payout: bool) -> None: @dataclass class HourlyEmployee(Employee): + """Represents an hourly employee.""" hourly_rate: float = 50 amount: int = 10 @dataclass class SalariedEmployee(Employee): + """Represents a salaried employee who receives a fixed monthly salary payment.""" monthly_salary: float = 5000 @@ -61,18 +63,23 @@ def __init__(self) -> None: self.employees: list[Employee] = [] def add_employee(self, employee: Employee) -> None: + """Add an employee to the company.""" self.employees.append(employee) def find_managers(self) -> list[Employee]: + """Find all employees with the role of manager.""" return [e for e in self.employees if e.role == "manager"] def find_vice_presidents(self) -> list[Employee]: + """Find all employees with the role of vice-president.""" return [e for e in self.employees if e.role == "vice-president"] def find_support_staff(self) -> list[Employee]: + """Find all employees with the role of support.""" return [e for e in self.employees if e.role == "support"] def pay_employee(self, employee: Employee) -> None: + """Pay an employee.""" if isinstance(employee, SalariedEmployee): print( f"Paying employee {employee.name} a monthly salary of ${employee.monthly_salary}." @@ -100,6 +107,10 @@ class Notification(ABC): def send(self, employee: Employee, message: str) -> None: pass + @abstractmethod + def is_enabled(self) -> bool: + """Check if notifications are enabled.""" + class EmailNotification(Notification): """Email notification implementation.""" @@ -107,6 +118,9 @@ class EmailNotification(Notification): def send(self, employee: Employee, message: str) -> None: print(f"Sending email to {employee.name}: {message}") + def is_enabled(self) -> bool: + return True + class SMSNotification(Notification): """SMS notification implementation.""" @@ -114,6 +128,9 @@ class SMSNotification(Notification): def send(self, employee: Employee, message: str) -> None: print(f"Sending SMS to {employee.name}: {message}") + def is_enabled(self) -> bool: + return True + class NotificationFactory: """Factory for creating notifications.""" @@ -122,10 +139,9 @@ class NotificationFactory: def get_notification(method: str) -> Notification: if method == "email": return EmailNotification() - elif method == "sms": + if method == "sms": return SMSNotification() - else: - raise ValueError("Invalid notification method") + raise ValueError("Invalid notification method") def main() -> None: diff --git a/2025/simple/notification_fn.py b/2025/simple/notification_fn.py index f1ce958c..154140d7 100644 --- a/2025/simple/notification_fn.py +++ b/2025/simple/notification_fn.py @@ -32,17 +32,20 @@ def take_a_holiday(self, payout: bool) -> None: @dataclass class HourlyEmployee(Employee): + """Represents an hourly employee.""" hourly_rate: float = 50 amount: int = 10 @dataclass class SalariedEmployee(Employee): + """Represents a salaried employee who receives a fixed monthly salary payment.""" monthly_salary: float = 5000 @dataclass class Freelancer(Employee): + """Represents a freelancer who receives a fixed hourly rate for a fixed amount of hours.""" hourly_rate: float = 50 amount: int = 10 retainer: float = 1000 @@ -50,6 +53,7 @@ class Freelancer(Employee): @dataclass class Intern(Employee): + """Represents an intern who receives a fixed monthly salary payment.""" monthly_salary: float = 1000 @@ -60,18 +64,23 @@ def __init__(self) -> None: self.employees: list[Employee] = [] def add_employee(self, employee: Employee) -> None: + """Add an employee to the company.""" self.employees.append(employee) def find_managers(self) -> list[Employee]: + """Find all employees with the role of manager.""" return [e for e in self.employees if e.role == "manager"] def find_vice_presidents(self) -> list[Employee]: + """Find all employees with the role of vice-president.""" return [e for e in self.employees if e.role == "vice-president"] def find_support_staff(self) -> list[Employee]: + """Find all employees with the role of support.""" return [e for e in self.employees if e.role == "support"] def pay_employee(self, employee: Employee) -> None: + """Pay an employee.""" if isinstance(employee, SalariedEmployee): print( f"Paying employee {employee.name} a monthly salary of ${employee.monthly_salary}." @@ -93,10 +102,12 @@ def pay_employee(self, employee: Employee) -> None: def send_email(employee: Employee, message: str) -> None: + """Send an email to an employee.""" print(f"Sending email to {employee.name}: {message}") def send_sms(employee: Employee, message: str) -> None: + """Send an SMS to an employee.""" print(f"Sending SMS to {employee.name}: {message}") diff --git a/2025/typehints/generic_return.py b/2025/typehints/generic_return.py index f071c3e5..ff411fe2 100644 --- a/2025/typehints/generic_return.py +++ b/2025/typehints/generic_return.py @@ -9,7 +9,9 @@ def main() -> None: items = [100, 200, 300] discount = 0.2 discounted_items = calculate_discounts(items, discount) + # type: ignore print(len(discounted_items)) # type issue here + # type: ignore print(discounted_items[0]) # type issue here diff --git a/2025/typehints/pyproject.toml b/2025/typehints/pyproject.toml index 002780a8..7417f176 100644 --- a/2025/typehints/pyproject.toml +++ b/2025/typehints/pyproject.toml @@ -1,12 +1,10 @@ [project] name = "typehints" version = "0.0.1" -authors = [{name = "ArjanCodes"}] -license = {text = "MIT"} +authors = [{ name = "ArjanCodes" }] +license = { text = "MIT" } requires-python = ">=3.12" -dependencies = [ - "mypy>=1.15.0", -] +dependencies = ["mypy>=1.15.0"] [tool.pytest.ini_options] pythonpath = "src"