West Midlands | 26 March SDC | Iswat Bello | Sprint 5 | prep exercises for Object-Oriented Programming and Type safety#509
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!
There was a problem hiding this comment.
- The requemnets had this part "should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values" but in the code it just returns and does not exit with non-zero code.
- If user puts an invalid input for the age the application crashes, think about how you can handle that better.
There was a problem hiding this comment.
I have updated the code to address your feedback. I added a try/except block to handle the ValueError for the age input to prevent the application from crashing on non-numeric input. I also imported sys so that I could use sys.stderr for error messages and sys.exit(1) to ensure the program terminates with a non-zero exit code as requested.
Thanks for the feedback.
There was a problem hiding this comment.
It is a clean fix, however, because the requrement was "do not change line 17", try to see what you can do for that, we want to access age like a property and not method!
Tip: Have a search on @Property decorator usage.
There was a problem hiding this comment.
I have updated the code to satisfy the requirement of not changing Line 17. By adding the @Property decorator to the age method, I've enabled the calculation to be accessed as a property (child.age) rather than a method call (child.age().
Thank you @behnamkvl for the feedback.
- Add sys import for proper error handling - Change error output to stderr instead of stdout - Replace return statement with sys.exit(1) for proper program termination - Ensures invalid input causes program to exit with error code 1
…anna/Module-Tools into tools/sprint5-prep-exercises
- Format code using Black formatter for consistent style - Add proper spacing around class definitions - Break long lines for better readability - Improve Laptop list formatting with proper indentation - Add blank line at end of file (Python convention) - Maintain all functionality while improving code style - Add comprehensive error handling with try/except blocks - Use sys.stderr for error messages - Add sys.exit(1) for proper error termination
- Add @Property decorator to age() method for attribute-like access - Format long conditional statement across multiple lines - Add blank lines between logical sections for readability - Change child.age() method call to child.age property access - Improve code formatting consistency with Black formatter - Maintain all functionality while improving code style
behnamkvl
left a comment
There was a problem hiding this comment.
Good job on making the changes. The current implementation looks good to me.
Thank you so much for the feedback. |
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 ✓