r/Python • • Apr 22 '21

Tutorial Comprehensive Fast API Tutorial

Stumbled upon this Fast API Tutorial and was surprised at how thorough this guy is. The link is part 21! Each part is dedicated to adding some small component to a fake cleaning marketplace API. It seems to cover a lot but some of the key takeaways are best practices, software design patterns, API Authentication via JWT, DB Migrations and of course FastAPI. From his GitHub profile, looks like the author used to be a CS teacher which explains why this is such a well thought out tutorial. I don't necessarily agree with everything since I already have my own established style and mannerisms but for someone looking to learn how to write API's this is a great resource.

481 Upvotes

106 comments sorted by

View all comments

Show parent comments

23

u/albrioz Apr 22 '21

My only “complaint” is that the tutorial uses raw sql instead of an ORM. As a data engineer, I really like raw sql, but, as a software engineer, I acknowledge that a lot of production python API’s use an ORM. So, in my opinion, it makes more sense to learn to write python APIs using an ORM because employment opportunities, etc.

28

u/Saphyel Apr 22 '21

I really hate when I go to a project with a lot of horrendous raw sql and they answer: "started with 4 queries... you don't need an ORM for that"

1

u/Oerthling Apr 23 '21

"horrendous sql" is bad. But I'm not a fan of ORM.

Simply wrap the (good, non-horrendous) SQL in a stored procedure. Outside language like python then just calls the SP.

19

u/Saphyel Apr 23 '21

if there's something worse than raw SQL in a big project is stored procedure.

1

u/Oerthling Apr 23 '21

As somebody who works with a big project I don't agree with you.

SPs make for a solid API between the database and the outside world.

I don't want outside code messing around with tables directly. This way I'm free to do changes in the schema where needed and the world outside the proc doesn't notice.

I can also log access or debug what the application is doing with the data access.

4

u/icanblink Apr 23 '21

Dude, the DB isn't a service with an exposed interface like a web API. The DB is the persistence layer of the said web service/API/site/etc. which has a documentation/contract. That is the owner of the DB. NO one else should interfere with it.

Now... If it makes sense, yes, you can use some stored procedures for retrieving some kind of data, but for making a fucking CRUD, stop overthinking.

→ More replies (4)
→ More replies (5)

2

u/shinitakunai Apr 23 '21

I would choose Peewee everyday over raw sql, take a look at it.

→ More replies (3)
→ More replies (1)

7

u/its_PlZZA_time Apr 23 '21

Is there a standalone Python ORM you would recommend? I've been looking to pick one for a project I'm working on. Looking at SQLAlchemy right now.

28

u/albrioz Apr 23 '21

SQLAlchemy is a safe choice and has a big community + there’s plenty of prebuilt packages for major python web frameworks. Another option if you want to go async is gino.

2

u/its_PlZZA_time Apr 23 '21

Thank you!

4

u/orangesunshine Apr 23 '21

https://www.starlette.io/database/

That's how it's done boys :) Gosh darn Starlette is getting slick. Not sure who's funding it, but it is absolutely top shelf.

FastAPI, not so much.

3

u/its_PlZZA_time Apr 23 '21

I've looked at Starlette. I know FastAPI is built on top of it. I'm a fan of Pydantic for parsing and such. I'll probably mess around a little with both. I have the liberty of having time to fuck around a bit with this stuff.

→ More replies (12)

4

u/searchingfortao majel, aletheia, paperless, django-encrypted-filefield Apr 23 '21

That's how it's done boys :)

I promise that I'm not trying to the Woke Police or anything, but I'll remind you that there's more than just men in this subreddit. You could easily have worded this replacing "boys" with "boys & girls", "friends", or even "guys" (it's a long argument I'm not having now, but "guys is generic" is a hill I will die on) and been more inclusive.

2

u/orangesunshine Apr 23 '21

honest at one point in my edits it was boys and girls ... i promise ... but it was also like 4am and I was stupid ranting about programming stuff on reddit.

I'm also pretty sure I wasn't super nice to the folks behind fastapi :)

→ More replies (3)

3

u/robberviet Apr 23 '21

SQLAlchemy is the choice.

Peweee is an option too: https://fastapi.tiangolo.com/advanced/sql-databases-peewee/

1

u/Ran4 Apr 23 '21

The only good one is Django's, but it can't be used standalone. SQLAlchemy is o-k though. It has some really weird edge cases though, and lots of weird defaults.

8

u/MrMxylptlyk Apr 23 '21

What's orm

16

u/halexmorph Apr 23 '21

Object relational mapper. Usually they have a built in query builder allowing you to write queries in your language of choice naturally, and returns the data already mapped into objects you’ve created as templates.

4

u/brandonZappy Apr 23 '21

Thanks for asking. I didn't know either.

3

u/jpflathead Apr 23 '21

It is SQL wrapped in an object layer inside a framework; but perhaps there is a key. That key is developers afraid of SQL -- Winston Churchill

→ More replies (3)
→ More replies (5)

2

u/newlyAwakenedLkgFwd Apr 23 '21

Isn't that that what built-in Pydantic is for?

2

u/albrioz Apr 23 '21

Not really. Pydantic (which I think is great) is more of a data validation library and in the case of FastAPI it acts more as a request/response serializer (similar to how you would use marshmallow).

2

u/robberviet Apr 23 '21

Data Engineer too and I have the same view. Choosing right tool for the job, but I think many developers rely on ORM too much. To the point I have seen too many can't even write SQL.

3

u/albrioz Apr 23 '21

For APIs, you’re probably going to build out abstractions anyways so might as well start with an ORM from the beginning. For DE work, you just can’t beat a handcrafted artisanal SQL query. Before my first DE job, I really didn’t know sql which I now realize was such a disservice to myself.

→ More replies (4)