Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dot notation question #10

Open
badlydrawnrob opened this issue Jan 24, 2025 · 3 comments
Open

Dot notation question #10

badlydrawnrob opened this issue Jan 24, 2025 · 3 comments

Comments

@badlydrawnrob
Copy link

badlydrawnrob commented Jan 24, 2025

@Youngestdev Hi there, I'm working my way through your book and enjoying it so far, but I have a simple question about the syntax (it's been a long time since I've used Python) ... by default item = {"id": 1} dictionary can only be accessed using item["id"] syntax, yet the examples in the book use dot notation (item.id).

  1. Are we accessing raw json here?
  2. Or is there something special that FastApi is doing in the background:

I'm assuming our data in chapter 02 is using the todo_list (a list of ToDo dictionaries) and converting requests from json to dict (which we're accessing).

I can't seem to find this information anywhere.

@badlydrawnrob
Copy link
Author

badlydrawnrob commented Jan 24, 2025

It seems that the following PUT:

for todo in todo_list:
        if todo["id"] == id:
            todo[".item"] = todo_data # relace with the request body

            return { "message": "To-do updated successfully" }

Fails with the ["id"] key accessor (returns Internal Server Error), so it must be raw json we're working with here? Now I'm a little confused!

@Youngestdev
Copy link
Collaborator

Youngestdev commented Jan 24, 2025

Hi, @badlydrawnrob!

I'm glad you're enjoying the book so far.

For the todos, we're making use of Pydantic Basemodel to represent the schema hence the use of dot notation to access the fields.

class Todo(BaseModel):
    id: int
    item: str

For the PUT method you have, the error is as a result of the wrong way you're accessing the Todo data which is a Basemodel object instance and not a dictionary. You can reference the code in the folder here for clarification.

Let me know if this answers your question.

@badlydrawnrob
Copy link
Author

badlydrawnrob commented Jan 24, 2025

@Youngestdev Thanks for the speedy reply.

I think I understand now, so basically the json is being converted to a ToDo or whatever class you set up, and you create an object:

from pydantic import BaseModel

class ToDo(BaseModel):
    id: int
    item: str

# json
one = { "id": 1, "item": "Finish the chapter" }
# our model
todo = ToDo(**one)

And then you're accessing the attributes with dot notation:

>>> todo
ToDo(id=1, item='Finish the chapter')
>>> todo.item
'Finish the chapter'

I'm so used to using typed functional (Haskell) I've totally forgot how object-oriented languages work 🤦‍♂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants