Skip to content

Commit e217305

Browse files
author
Xinxi
committed
dataclass.py
1 parent 4350f48 commit e217305

5 files changed

Lines changed: 108 additions & 0 deletions

File tree

Sprint5/Limits of type checking.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def double(number):
2+
return number * 3
3+
print(double(10))
4+
5+
#this code runs perfectly.

Sprint5/class&objects.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Person:
2+
def __init__(self, name: str, age: int, preferred_operating_system: str):
3+
self.name = name
4+
self.age = age
5+
self.preferred_operating_system = preferred_operating_system
6+
7+
imran = Person("Imran", 22, "Ubuntu")
8+
print(imran.name)
9+
print(imran.address)
10+
11+
eliza = Person("Eliza", 34, "Arch Linux")
12+
print(eliza.name)
13+
print(eliza.address)

Sprint5/dataclass.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from dataclasses import dataclass
2+
from datetime import date
3+
4+
@dataclass(frozen=True)
5+
class Person:
6+
name: str
7+
birth_date: date
8+
preferred_operating_system: str
9+
10+
def is_adult(self) -> bool:
11+
today = date.today()
12+
age = today.year - self.birth_date.year
13+
14+
birthday_has_passed = (today.month, today.day) >= (self.birth_date.month, self.birth_date.day)
15+
16+
if not birthday_has_passed:
17+
age -= 1
18+
19+
return age >= 18
20+
21+
imran1 = Person("Imran", date(2000, 5, 20), "Ubuntu")
22+
imran2 = Person("Imran", date(2000, 5, 20), "Ubuntu")
23+
24+
print(imran1)
25+
print(f"Are they equal? {imran1 == imran2}")
26+
print(f"Is Imran an adult? {imran1.is_adult()}")

Sprint5/methods.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# The advantage of using methods instead of free functions.
2+
# 1. Encapsulation & Organization: methods group behavior directly with the data it operates on the object.
3+
# 2. State management: methods have direct access to 'self'.
4+
# 3. Readibility.
5+
6+
from datetime import date
7+
8+
class Person:
9+
def __init__(self, name: str, birth_date: date, preferred_operating_system: str):
10+
self.name = name
11+
self.birth_date = birth_date
12+
self.preferred_operating_system = preferred_operating_system
13+
14+
def is_adult(self) -> bool:
15+
today = date.today()
16+
age = today.year - self.birth_date.year
17+
birthday_has_passed: bool = (today.month, today.day) >= (self.birth_date.month, self.birth_date.day)
18+
19+
if not birthday_has_passed:
20+
age -= 1
21+
return age >= 18
22+
23+
imran = Person("Imran", date(2000, 5, 20), "Ubuntu")
24+
print(f"Is Imran an adult? {imran.is_adult()}")

Sprint5/mypy.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import Dict
2+
3+
# Added Type Annotations
4+
def open_account(balances: Dict[str, int], name: str, amount: int) -> None:
5+
balances[name] = amount
6+
7+
def sum_balances(accounts: Dict[str, int]) -> int:
8+
total = 0
9+
for name, pence in accounts.items():
10+
print(f"{name} had balance {pence}")
11+
# If 'pence' isn't an int, this will fail or cause issues later
12+
total += pence
13+
return total
14+
15+
def format_pence_as_string(total_pence: int) -> str:
16+
if total_pence < 100:
17+
return f"{total_pence}p"
18+
# Use // for floor division to ensure 'pounds' is an int
19+
pounds = total_pence // 100
20+
pence = total_pence % 100
21+
return f"£{pounds}.{pence:02d}"
22+
23+
balances: Dict[str, int] = {
24+
"Sima": 700,
25+
"Linn": 545,
26+
"Georg": 831,
27+
}
28+
29+
# BUGS FIXED HERE:
30+
# Changed 9.13 (float) to 913 (int)
31+
# Changed "£7.13" (str) to 713 (int)
32+
open_account(balances, "Tobi", 913)
33+
open_account(balances, "Olya", 713)
34+
35+
total_pence = sum_balances(balances)
36+
37+
# BUG FIXED HERE: Corrected the function name call
38+
total_string = format_pence_as_string(total_pence)
39+
40+
print(f"The bank accounts total {total_string}")

0 commit comments

Comments
 (0)