Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions python_fundamentals/exercises/day18-field-alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ def change_first_name(self, first_nm: str) -> None:
print(exc)

try:
# `populate_by_name=True` allows construction with snake_case names;
# the `# type: ignore` comments were unused and flagged by the type checker,
# so remove them.
emp2: Employee = Employee(
first_name="swaroop", # type: ignore
last_name="dixit", # type: ignore
monthly_salary=20000, # type: ignore
first_name="swaroop",
last_name="dixit",
monthly_salary=20000,
)
print(emp2.model_dump())
except ValidationError as exc:
Expand Down
10 changes: 6 additions & 4 deletions python_fundamentals/exercises/day_24_decimal/product_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ class Invoice(BaseModel):
def subtotal_total(self) -> Decimal:
total: Decimal = Decimal("0")
for product in self.products:
total += product.subtotal
# call the computed_field so static type-checkers see a Decimal result
total += product.subtotal()
return total

@computed_field
def grand_total(self) -> Decimal:
subtotal: Decimal = self.subtotal_total
gst_amount = subtotal * (self.gst_percentage / 100)
return Decimal(subtotal + gst_amount)
subtotal: Decimal = self.subtotal_total()
# divide by Decimal to avoid mixing floats with Decimals
gst_amount = subtotal * (self.gst_percentage / Decimal("100"))
return subtotal + gst_amount


p1: Product = Product(name="Sugar", price=Decimal("20.00"), quantity=1)
Expand Down
8 changes: 4 additions & 4 deletions python_fundamentals/exercises/day_25_date_datetime/employee.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class Employee(BaseModel):
print(f"Python Representaion of Employee Object: {tejas_dixit.model_dump()}")
print(f"Pydantic Representaion of Employee Object: {tejas_dixit.model_dump_json()}")

vignesh_gawali: Employee = Employee(
emp_name="Vignesh Gawali",
joining_date="2024-12-12", # Intentional: testing Pydantic parsing
created_at="0000-12-12 12:12", # Intentional: testing Pydantic parsing
# Use model_validate for string inputs to avoid static type errors and to
# keep intentional parsing tests.
vignesh_gawali = Employee.model_validate(
{"emp_name": "Vignesh Gawali", "joining_date": "2024-12-12", "created_at": "2024-12-12 12:12"}
)

print(f"Python Representaion of Employee Object: {vignesh_gawali.model_dump()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ def format_emp() -> None:

format_emp()

another_emp: Employee = Employee(
emp_name="Tejas Dixit",
joining_date="2024-12-29",
# Intentional: Pydantic will conert it into date format
created_at="2024-12-28 12:12",
# Intentional: Pydantic model will convert it into datetime format
# Use model_validate to pass string inputs (so static checkers don't flag arg-type).
another_emp = Employee.model_validate(
{"emp_name": "Tejas Dixit", "joining_date": "2024-12-29", "created_at": "2024-12-28 12:12"}
)

print(
Expand All @@ -56,6 +53,5 @@ def format_emp() -> None:

print(f"ADMIN View: {another_emp.model_dump(exclude={'joining_date'})}")


except (ValueError, ValidationError) as exc:
print(exc)
8 changes: 4 additions & 4 deletions python_fundamentals/exercises/pydantic-extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class EventLog(BaseModel):
print("=" * 60)

try:
event = EventLog(
event_id=0,
event_type="Error",
event_description="ValidationError",
# Use model_validate when passing extra fields via a mapping so static type
# checkers don't complain about unexpected keyword arguments.
event = EventLog.model_validate(
{"event_id": 0, "event_type": "Error", "event_description": "ValidationError"}
)

print(event.model_dump())
Expand Down
Loading