West Midlands | 26 March SDC | Iswat Bello | Sprint 5 | prep exercises for Object-Oriented Programming and Type safety#509
Open
Iswanna wants to merge 16 commits intoCodeYourFuture:mainfrom
Conversation
- Add .venv to ignore Python virtual environments - Add *.class to ignore Java compiled class files
- Add exercise demonstrating the importance of type hints - Include buggy double() function that multiplies by 3 instead of 2 - Document the bug and propose two possible fixes
- Create exercise file demonstrating why type hints matter
- Include half(), double(), and second() functions
- Add example showing string multiplication vs numeric multiplication
- Document expected behavior: double("22") returns "2222" due to string repetition
- Include question and answer explaining the type-dependent behavior
- Fix open_account() parameter: change 'balance' to 'balances' (NameError bug) - Add missing type annotations to all function parameters and return types - Run through mypy to validate all type hints are correct - Verify code runs without errors
- Create Person class with type-annotated __init__ method - Add name (str), age (int), and preferred_operating_system (str) attributes - Create example instances (imran and eliza) - Intentionally access undefined 'address' attribute to demonstrate mypy error detection - Document the mypy error and explain the missing attribute issue
- Create Person class with type-annotated __init__ method - Add name (str), age (int), and preferred_operating_system (str) attributes - Implement is_adult() function that correctly accesses Person.age attribute - Add is_adult_wrong_attribute() function that intentionally accesses non-existent Person.address - Demonstrate mypy error detection for invalid attribute access - Show how mypy validates correct vs incorrect property references
- Replace age (int) parameter with date_of_birth (datetime.date) - Convert is_adult() from free function to instance method - Implement dynamic age calculation based on current date - Account for whether birthday has occurred this year - Add type hints for all parameters and return types - Include example usage with imran and eliza instances
- Create Person class with @DataClass(frozen=True) decorator - Use datetime.date for date_of_birth instead of int for age - Replace manual __init__ with declarative field definitions - Implement is_adult() method with dynamic age calculation - Account for whether birthday has occurred this year - Add type hints for all attributes and methods - Include example usage with imran and eliza instances - Demonstrate immutable dataclass with frozen=True
- Create Person dataclass with @DataClass(frozen=True) decorator - Use List["Person"] generic type for children attribute (self-referencing) - Implement age() method that calculates age from date_of_birth - Add example Person instances: fatma, aisha, and imran - Implement print_family_tree() function to display family hierarchy - Demonstrate practical use of generics for type-safe collections - Show how mypy validates generic type parameters
…types - Change Person.preferred_operating_system from str to List[str] - Rename field to preferred_operating_systems (plural) - Update find_possible_laptops() to check if OS is in list using 'in' operator - Add example Person instances with multiple preferred operating systems - Add sample Laptop instances for testing - Demonstrate practical use of generic List types with dataclasses - Run through mypy to validate all type annotations are correct
…enums - Create OperatingSystem Enum with MACOS, ARCH, and UBUNTU values - Define Person dataclass with preferred_operating_system as Enum type - Define Laptop dataclass with operating_system as Enum type - Implement find_possible_laptops() function to match laptops to person preferences - Add library inventory: 4 sample laptops with different operating systems - Add sample people with OS preferences - Demonstrate type-safe enum usage vs string-based approach - Display matching laptops for each person
- Add create_person_from_input() function for interactive person creation - Prompt user for name, age, and preferred operating system - Display available OS options from OperatingSystem Enum - Validate user input with try/except for invalid OS choices - Create Person instance and find matching laptops - Display matching laptops for newly created person - Maintain existing hardcoded people and laptop matching - Add interactive section at end to test user input functionality
- Add newline before possible laptops output for better readability - Add summary message showing count of available laptops - Display the operating system name using enum.value property - Improve user feedback with clearer output format
…thods on a Parent object!
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Learners, PR Template
Self checklist
Changelist
Complete implementation of 12 preparatory exercises covering Python fundamentals, including classes, inheritance, type hints, generics, and enums.
What's New
Files Changed
.pyexercise files.mddocumentation fileAll Tests Pass ✓