Epython Lab
6.11K subscribers
680 photos
31 videos
104 files
1.31K links
Welcome to Epython Lab, where you can get resources to learn, one-on-one trainings on machine learning, business analytics, and Python, and solutions for business problems.

Buy ads: https://telega.io/c/epythonlab
Download Telegram
While moving from other web frameworks to ๐…๐š๐ฌ๐ญ๐€๐๐ˆ, one question came to my mind:



๐–๐ก๐ฒ ๐๐จ ๐ˆ ๐ง๐ž๐ž๐ ๐€๐๐ˆ๐‘๐จ๐ฎ๐ญ๐ž๐ซ ๐ฐ๐ก๐ž๐ง ๐…๐š๐ฌ๐ญ๐€๐๐ˆ ๐š๐ฅ๐ซ๐ž๐š๐๐ฒ ๐ฅ๐ž๐ญ๐ฌ ๐ฆ๐ž ๐๐ž๐Ÿ๐ข๐ง๐ž ๐ซ๐จ๐ฎ๐ญ๐ž๐ฌ ๐๐ข๐ซ๐ž๐œ๐ญ๐ฅ๐ฒ?



For a small project, this is enough:



@๐’‚๐’‘๐’‘.๐’ˆ๐’†๐’•("/๐’…๐’๐’„๐’–๐’Ž๐’†๐’๐’•๐’”")

๐’‚๐’”๐’š๐’๐’„ ๐’…๐’†๐’‡ ๐’ˆ๐’†๐’•_๐’…๐’๐’„๐’–๐’Ž๐’†๐’๐’•๐’”():

...

So why introduce APIRouter?



The answer became clearer when I started thinking about a real application.



As the project grows, I may have authentication, users, documents, search, AI processing, and many more endpoints.



Keeping all of these routes in one place can quickly become difficult to maintain.

Thatโ€™s where APIRouter becomes useful.



It allows me to organize routes by feature and keep the application structure clean.



So my takeaway is simple:



๐…๐š๐ฌ๐ญ๐€๐๐ˆ ๐ฅ๐ž๐ญ๐ฌ ๐ฆ๐ž ๐๐ž๐Ÿ๐ข๐ง๐ž ๐ญ๐ก๐ž ๐ซ๐จ๐ฎ๐ญ๐ž๐ฌ.

๐€๐๐ˆ๐‘๐จ๐ฎ๐ญ๐ž๐ซ ๐ก๐ž๐ฅ๐ฉ๐ฌ ๐ฆ๐ž ๐จ๐ซ๐ ๐š๐ง๐ข๐ณ๐ž ๐ญ๐ก๐ž๐ฆ.



Itโ€™s not required to make FastAPI work. It becomes useful when I want the application to stay organized and maintainable as it grows.



This was one of those small FastAPI concepts that made much more sense once I started looking at the bigger picture.



๐‡๐š๐ฏ๐ž ๐ฒ๐จ๐ฎ ๐ก๐š๐ ๐ญ๐ก๐ž ๐ฌ๐š๐ฆ๐ž ๐ช๐ฎ๐ž๐ฌ๐ญ๐ข๐จ๐ง ๐ฐ๐ก๐ž๐ง ๐ฆ๐จ๐ฏ๐ข๐ง๐  ๐ญ๐จ ๐…๐š๐ฌ๐ญ๐€๐๐ˆ?



๐Ÿ”— ๐†๐ข๐ญ๐‡๐ฎ๐›: https://github.com/epythonlab2/ai-document-api



๐ŸŽฅ ๐…๐š๐ฌ๐ญ๐€๐๐ˆ ๐œ๐จ๐ฎ๐ซ๐ฌ๐ž: https://www.youtube.com/watch?v=0SLLG2Z_Htw&list=PLQNCas8_eikM



#FastAPI #Python #APIRouter #BackendDevelopment #SoftwareArchitecture #APIDevelopment #PythonDeveloper
๐Ÿ‘2
The FastAPI Course Github (AI Document API) Project is just updated
Add database connection setup and document model definition
- Implement database connection using SQLAlchemy and environment variables
- Create Document model with fields for title, description, category, and priority
- Update .gitignore to include .env file
- Add required dependencies in pyproject.toml and uv.lock


๐Ÿ”— ๐†๐ข๐ญ๐‡๐ฎ๐›: https://github.com/epythonlab2/ai-document-api

๐ŸŽฅ ๐…๐š๐ฌ๐ญ๐€๐๐ˆ ๐œ๐จ๐ฎ๐ซ๐ฌ๐ž: https://www.youtube.com/watch?v=0SLLG2Z_Htw&list=PLQNCas8_eikM
While learning FastAPI, I came across another question: ๐–๐ก๐ฒ ๐๐จ ๐ˆ ๐ง๐ž๐ž๐ ๐๐ฒ๐๐š๐ง๐ญ๐ข๐œ ๐ฐ๐ก๐ž๐ง ๐๐ฒ๐ญ๐ก๐จ๐ง ๐š๐ฅ๐ซ๐ž๐š๐๐ฒ ๐ก๐š๐ฌ ๐ญ๐ฒ๐ฉ๐ž ๐ก๐ข๐ง๐ญ๐ฌ?

For example, I can write:

๐‘‘๐‘’๐‘“ ๐‘๐‘Ÿ๐‘’๐‘Ž๐‘ก๐‘’_๐‘ข๐‘ ๐‘’๐‘Ÿ(๐‘›๐‘Ž๐‘š๐‘’: ๐‘ ๐‘ก๐‘Ÿ, ๐‘Ž๐‘”๐‘’: ๐‘–๐‘›๐‘ก):
...

Python already tells me that name should be a string and age should be an integer.

So what does ๐๐ฒ๐๐š๐ง๐ญ๐ขc add?

The difference became clear when working with API request data.
An API receives data from outside the application, and I need more than just type hints. I need to validate that the data has the expected structure and values.

Thatโ€™s where Pydantic becomes useful.

With FastAPI, I can define a model:

๐‘๐‘™๐‘Ž๐‘ ๐‘  ๐‘ˆ๐‘ ๐‘’๐‘Ÿ๐ถ๐‘Ÿ๐‘’๐‘Ž๐‘ก๐‘’(๐ต๐‘Ž๐‘ ๐‘’๐‘€๐‘œ๐‘‘๐‘’๐‘™):

๐‘›๐‘Ž๐‘š๐‘’: ๐‘ ๐‘ก๐‘Ÿ ๐‘Ž๐‘”๐‘’: ๐‘–๐‘›๐‘ก

FastAPI can then use that model to validate incoming request data automatically.

So my takeaway is:

๐๐ฒ๐ญ๐ก๐จ๐ง ๐ญ๐ฒ๐ฉ๐ž ๐ก๐ข๐ง๐ญ๐ฌ ๐๐ž๐ฌ๐œ๐ซ๐ข๐›๐ž ๐ฐ๐ก๐š๐ญ ๐ˆ ๐ž๐ฑ๐ฉ๐ž๐œ๐ญ.

๐๐ฒ๐๐š๐ง๐ญ๐ข๐œ ๐ก๐ž๐ฅ๐ฉ๐ฌ ๐ฏ๐š๐ฅ๐ข๐๐š๐ญ๐ž ๐ฐ๐ก๐š๐ญ ๐ˆ ๐š๐œ๐ญ๐ฎ๐š๐ฅ๐ฅ๐ฒ ๐ซ๐ž๐œ๐ž๐ข๐ฏ๐ž.

This is another small concept that became much clearer once I started building a real FastAPI application.

๐–๐ก๐š๐ญ ๐ฐ๐š๐ฌ ๐ฒ๐จ๐ฎ๐ซ ๐Ÿ๐ข๐ซ๐ฌ๐ญ ๐ข๐ฆ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง ๐จ๐Ÿ ๐๐ฒ๐๐š๐ง๐ญ๐ข๐œ ๐ฐ๐ก๐ž๐ง ๐ฒ๐จ๐ฎ ๐ฌ๐ญ๐š๐ซ๐ญ๐ž๐ ๐ฎ๐ฌ๐ข๐ง๐  ๐…๐š๐ฌ๐ญ๐€๐๐ˆ?

๐Ÿ”— ๐†๐ข๐ญ๐‡๐ฎ๐›: https://github.com/epythonlab2/ai-document-api

๐ŸŽฅ ๐…๐š๐ฌ๐ญ๐€๐๐ˆ ๐œ๐จ๐ฎ๐ซ๐ฌ๐ž: https://www.youtube.com/watch?v=0SLLG2Z_Htw&list=PLQNCas8_eikM
As I build my ๐€๐ˆ ๐ƒ๐จ๐œ๐ฎ๐ฆ๐ž๐ง๐ญ ๐€๐๐ˆ ๐ฐ๐ข๐ญ๐ก ๐…๐š๐ฌ๐ญ๐€๐๐ˆ, Iโ€™m realizing that building an API is about much more than writing endpoints.


Getting an endpoint to work is just the beginning. A real application needs a solid structure and several pieces working together.

That means thinking about:

- Validating incoming data

- Organizing routes and business logic

- Connecting to a database

- Handling errors properly

- Implementing authentication and permissions

- Testing and deploying the application


Each step introduces a new challenge and a new concept to learn.

Thatโ€™s why Iโ€™m building this project step by step as part of my ๐…๐š๐ฌ๐ญ๐€๐๐ˆ ๐ฌ๐ž๐ซ๐ข๐ž๐ฌ. My goal is to go beyond isolated examples and demonstrate how these concepts come together in a practical AI application.


๐๐ž๐œ๐š๐ฎ๐ฌ๐ž ๐ฅ๐ž๐š๐ซ๐ง๐ข๐ง๐  ๐š ๐Ÿ๐ซ๐š๐ฆ๐ž๐ฐ๐จ๐ซ๐ค ๐ข๐ฌ ๐จ๐ง๐ž ๐ญ๐ก๐ข๐ง๐ . ๐Š๐ง๐จ๐ฐ๐ข๐ง๐  ๐ก๐จ๐ฐ ๐ญ๐จ ๐ฎ๐ฌ๐ž ๐ข๐ญ ๐ญ๐จ ๐›๐ฎ๐ข๐ฅ๐ ๐š ๐ซ๐ž๐š๐ฅ ๐š๐ฉ๐ฉ๐ฅ๐ข๐œ๐š๐ญ๐ข๐จ๐ง ๐ข๐ฌ ๐š๐ง๐จ๐ญ๐ก๐ž๐ซ.

๐Ÿ”— GitHub repository: https://github.com/epythonlab2/ai-document-api

๐ŸŽฅ Explore the full course series: https://www.youtube.com/watch?v=0SLLG2Z_Htw&list=PLQNCas8_eikM


๐–๐ก๐š๐ญ ๐๐จ ๐ฒ๐จ๐ฎ ๐Ÿ๐ข๐ง๐ ๐ฆ๐จ๐ฌ๐ญ ๐œ๐ก๐š๐ฅ๐ฅ๐ž๐ง๐ ๐ข๐ง๐  ๐ฐ๐ก๐ž๐ง ๐ญ๐ฎ๐ซ๐ง๐ข๐ง๐  ๐š ๐ฌ๐ข๐ฆ๐ฉ๐ฅ๐ž ๐€๐๐ˆ ๐ข๐ง๐ญ๐จ ๐š ๐œ๐จ๐ฆ๐ฉ๐ฅ๐ž๐ญ๐ž ๐š๐ฉ๐ฉ๐ฅ๐ข๐œ๐š๐ญ๐ข๐จ๐ง?


#FastAPI #Python #APIDevelopment #BackendDevelopment #AIEngineering
๐Ÿ‘3
As I continue building my AI Document API with FastAPI, Iโ€™m reminding myself to look beyond the happy path.

Itโ€™s easy to test an endpoint with valid data and celebrate when everything works.

But what happens when:

- The client sends invalid data?

- The requested document doesn't exist?

- A database operation fails?

- A user tries to access something they aren't authorized to access?

For me, the goal isnโ€™t simply to make each endpoint work. Itโ€™s to understand how it behaves when things go wrong, too.

Thatโ€™s one of the things Iโ€™m exploring as I build this project step by step with FastAPI.

A good API should handle the expected path well and fail predictably when things go wrong.

As developers, we should test more than whether our code works. We should also test how it fails.

When reviewing an API endpoint, what do you check first: input validation, error handling, or authorization?

๐Ÿ”— GitHub project: https://github.com/epythonlab2/ai-document-api

๐ŸŽฅ Follow the FastAPI course series: https://www.youtube.com/watch?v=0SLLG2Z_Htw&list=PLQNCas8_eikM


#FastAPI #Python #APIDevelopment #BackendDevelopment #SoftwareEngineering
Stop Accepting AI Code Without Reading It | Review AI-Generated Code
https://youtu.be/mfqI3w4Npsc
AI-Generated Python Code Is Not Always Correct | Code Review
https://youtu.be/mJLwFf0dHSA
Speed Up Pandas: 3 Performance Tips for Faster Dataframes https://youtu.be/3vX1S2qxDu8
๐Ÿ‘3
๐Ÿš€ Iโ€™m Open to New Opportunities in Python, Machine Learning, and Data Engineering!

Iโ€™m looking to connect with individuals, teams, and organizations where I can use my technical skills to solve real-world problems, share knowledge, and create meaningful impact.

Here are three areas where Iโ€™d love to contribute:

๐Ÿ”น Content Creation & Technical Education
Creating practical, engaging tutorials on Python, Machine Learning, and Data Engineering to help developers learn, build, and grow.

๐Ÿ”น Backend Development & AI Engineering
Building reliable backend systems, ML-powered applications, and APIs using Python, FastAPI, and modern AI/ML technologies.

๐Ÿ”น Mentoring & Knowledge Sharing
Helping aspiring developers strengthen their Python, Machine Learning, and Data Engineering skills through practical guidance and project-based learning.

๐Ÿ’ก What am I looking for?
Opportunities to collaborate on exciting projects, contribute to impactful teams, create educational content, and mentor the next generation of developers.

If youโ€™re hiring, building something interesting, or know someone looking for these skills, Iโ€™d love to connect!

๐Ÿ“ฉ Feel free to send me a message or tag someone who might be interested. Letโ€™s build something meaningful together.

#Python #MachineLearning #DataEngineering #FastAPI #AI #BackendDevelopment #Mentorship #ContentCreation #OpenToOpportunities
๐Ÿ‘3๐ŸŽ‰1
๐ˆ ๐ฌ๐ฉ๐ž๐ž๐ ๐ฎ๐ฉ ๐ฉ๐š๐ง๐๐š๐ฌ ๐›๐ฒ ๐Ÿ๐ŸŽ๐ŸŽ๐ฑ, ๐š๐ง๐ ๐ก๐ž๐ซ๐ž ๐ข๐ฌ ๐ž๐ฑ๐š๐œ๐ญ๐ฅ๐ฒ ๐ก๐จ๐ฐ ๐ฒ๐จ๐ฎ ๐œ๐š๐ง ๐ญ๐จ๐จ. ๐Ÿš€

If your DataFrame manipulation feels lightning-fast on a small sample but crawls to a halt on a million rows, you are likely falling into a few common performance traps.

Here are the 3 core optimizations that drove a massive speedup:



1๏ธโƒฃ ๐ƒ๐ข๐ญ๐œ๐ก .๐š’๐š๐šŽ๐š›๐š›๐š˜๐š ๐šœ() ๐Ÿ๐จ๐ซ ๐•๐ž๐œ๐ญ๐จ๐ซ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง

Row-by-row looping forces Pandas to create a Series object for every row, bypassing C-level optimizations. Let NumPy and Pandas handle your array math natively.



โŒ ๐’๐ฅ๐จ๐ฐ (๐‘๐จ๐ฐ-๐›๐ฒ-๐ซ๐จ๐ฐ ๐ข๐ญ๐ž๐ซ๐š๐ญ๐ข๐จ๐ง):

๐š๐š˜๐š› ๐š’, ๐š›๐š˜๐š  ๐š’๐š— ๐š๐š.๐š’๐š๐šŽ๐š›๐š›๐š˜๐š ๐šœ():

๐š๐š.๐š•๐š˜๐šŒ[๐š’, "๐š๐š˜๐š๐šŠ๐š•"] = ๐š›๐š˜๐š ["๐š™๐š›๐š’๐šŒ๐šŽ"] * ๐š›๐š˜๐š ["๐šš๐šž๐šŠ๐š—๐š๐š’๐š๐šข"]



โœ… ๐Ÿ๐ŸŽ๐ŸŽ๐ฑ ๐…๐š๐ฌ๐ญ๐ž๐ซ (๐•๐ž๐œ๐ญ๐จ๐ซ๐ข๐ณ๐ž๐ ๐จ๐ฉ๐ž๐ซ๐š๐ญ๐ข๐จ๐ง):

๐š๐š["๐š๐š˜๐š๐šŠ๐š•"] = ๐š๐š["๐š™๐š›๐š’๐šŒ๐šŽ"] * ๐š๐š["๐šš๐šž๐šŠ๐š—๐š๐š’๐š๐šข"]



2๏ธโƒฃ ๐’๐ฎ๐ฉ๐ž๐ซ๐œ๐ก๐š๐ซ๐ ๐ž ๐Œ๐ฎ๐ฅ๐ญ๐ข-๐‚๐จ๐ฅ๐ฎ๐ฆ๐ง ๐Œ๐š๐ญ๐ก ๐ฐ๐ข๐ญ๐ก ๐š™๐š.๐šŽ๐šŸ๐šŠ๐š•()

# ๐™ด๐š•๐š’๐š–๐š’๐š—๐šŠ๐š๐šŽ๐šœ ๐š๐šŽ๐š–๐š™๐š˜๐š›๐šŠ๐š›๐šข ๐š–๐šŽ๐š–๐š˜๐š›๐šข ๐šŠ๐š•๐š•๐š˜๐šŒ๐šŠ๐š๐š’๐š˜๐š— ๐š˜๐šŸ๐šŽ๐š›๐š‘๐šŽ๐šŠ๐š

๐š๐š["๐š›๐šŽ๐šœ๐šž๐š•๐š"] = ๐š™๐š.๐šŽ๐šŸ๐šŠ๐š•("๐šŒ๐š˜๐š•๐Ÿท + ๐šŒ๐š˜๐š•๐Ÿธ * ๐šŒ๐š˜๐š•๐Ÿน - ๐šŒ๐š˜๐š•๐Ÿบ", ๐š๐šŠ๐š›๐š๐šŽ๐š=๐š๐š)



3๏ธโƒฃ ๐ƒ๐จ๐ฐ๐ง๐œ๐š๐ฌ๐ญ ๐ƒ๐š๐ญ๐š ๐“๐ฒ๐ฉ๐ž๐ฌ & ๐”๐ฌ๐ž ๐‚๐š๐ญ๐ž๐ ๐จ๐ซ๐ข๐œ๐š๐ฅ๐ฌ

By default, Pandas assigns 64-bit integers/floats and treats string columns as bloated ๐š˜๐š‹๐š“๐šŽ๐šŒ๐š types. Downcasting your numbers and converting repeating strings to ๐šŒ๐šŠ๐š๐šŽ๐š๐š˜๐š›๐šข can shrink your memory footprint by up to 80%โ€”drastically accelerating every subsequent operation.



# ๐™ณ๐š˜๐š ๐š—๐šŒ๐šŠ๐šœ๐š ๐š—๐šž๐š–๐šŽ๐š›๐š’๐šŒ๐šœ & ๐š–๐šŠ๐š™ ๐šœ๐š๐š›๐š’๐š—๐š๐šœ ๐š๐š˜ ๐šŒ๐šŠ๐š๐šŽ๐š๐š˜๐š›๐šข

๐š๐š["๐š’๐š"] = ๐š™๐š.๐š๐š˜_๐š—๐šž๐š–๐šŽ๐š›๐š’๐šŒ(๐š๐š["๐š’๐š"], ๐š๐š˜๐š ๐š—๐šŒ๐šŠ๐šœ๐š="๐š’๐š—๐š๐šŽ๐š๐šŽ๐š›")

๐š๐š["๐š›๐šŽ๐š๐š’๐š˜๐š—"] = ๐š๐š["๐š›๐šŽ๐š๐š’๐š˜๐š—"].๐šŠ๐šœ๐š๐šข๐š™๐šŽ("๐šŒ๐šŠ๐š๐šŽ๐š๐š˜๐š›๐šข")



๐Ÿ’ก ๐“๐ก๐ž ๐“๐š๐ค๐ž๐š๐ฐ๐š๐ฒ: Optimize your code at the vector and memory level before throwing expensive compute clusters or hardware upgrades at the problem.



What is your favorite performance trick for handling large datasets? Letโ€™s share notes in the comments below! ๐Ÿ‘‡



Explore more https://youtu.be/3vX1S2qxDu8



#Python #Pandas #DataScience #DataEngineering #CodeOptimization #MachineLearning #Programming