Skip to content

Commit 8533377

Browse files
Merge branch 'master' into fastapi-python-web-apis
2 parents 3e30f88 + ddcd360 commit 8533377

File tree

240 files changed

+4324
-71
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

240 files changed

+4324
-71
lines changed

crud-operations/crud_fastapi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from crud_sql_alchemy import Bird, init_db
22
from crud_sql_alchemy import Session as SessionLocal
3-
from fastapi import Depends, FastAPI, HTTPException
43
from pydantic import BaseModel, ConfigDict
54
from sqlalchemy import select
65
from sqlalchemy.orm import Session
76

7+
from fastapi import Depends, FastAPI, HTTPException
8+
89
app = FastAPI()
910
init_db()
1011

debug-python-errors/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Debug Common Python Errors
2+
3+
This folder provides the code examples for the Real Python tutorial [How to Debug Common Python Errors](https://realpython.com/debug-python-errors/).

debug-python-errors/cat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cat = "Siamese"
2+
3+
print(cat)

debug-python-errors/fruit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def capitalize_fruit_names(fruits):
2+
capitalized_fruit_names = []
3+
cleaned = [fruit if isinstance(fruit, str) else "" for fruit in fruits]
4+
5+
for fruit in cleaned:
6+
capitalized_fruit_names.append(fruit.capitalize())
7+
8+
return capitalized_fruit_names
9+
10+
11+
if __name__ == "__main__":
12+
print(capitalize_fruit_names(["apple", "BANANA", "cherry", "maNgo"]))

debug-python-errors/palindromes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def find_palindromes(text):
2+
# Split sentence into words
3+
words = text.split()
4+
5+
# Remove punctuation and convert to lowercase
6+
normalized_words = [
7+
"".join(filter(str.isalnum, word)).lower() for word in words
8+
]
9+
10+
# Check for palindromes
11+
return [word for word in normalized_words if word == word[::-1]]
12+
13+
14+
if __name__ == "__main__":
15+
print(
16+
find_palindromes("Dad plays many solos at noon, and sees a racecar.")
17+
)

debug-python-errors/test_fruit.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import unittest
2+
3+
from fruit import capitalize_fruit_names
4+
5+
6+
class TestFruit(unittest.TestCase):
7+
def test_empty_list(self):
8+
"""with empty list"""
9+
self.assertEqual(capitalize_fruit_names([]), [])
10+
11+
def test_lowercase(self):
12+
"""with lowercase strings"""
13+
self.assertEqual(
14+
capitalize_fruit_names(["apple", "banana", "cherry"]),
15+
["Apple", "Banana", "Cherry"],
16+
)
17+
18+
def test_uppercase(self):
19+
"""with uppercase strings"""
20+
self.assertEqual(
21+
capitalize_fruit_names(["APPLE", "BANANA", "CHERRY"]),
22+
["Apple", "Banana", "Cherry"],
23+
)
24+
25+
def test_mixed_case(self):
26+
"""with mixed case strings"""
27+
self.assertEqual(
28+
capitalize_fruit_names(["mAnGo", "grApE"]),
29+
["Mango", "Grape"],
30+
)
31+
32+
def test_non_string_element(self):
33+
"""with a mix of integer and string elements"""
34+
self.assertEqual(
35+
capitalize_fruit_names([123, "banana"]),
36+
["", "Banana"],
37+
)
38+
39+
40+
if __name__ == "__main__":
41+
unittest.main()

fastapi-url-shortener/source_code_final/shortener_app/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import validators
2-
from fastapi import Depends, FastAPI, HTTPException, Request
3-
from fastapi.responses import RedirectResponse
42
from sqlalchemy.orm import Session
53
from starlette.datastructures import URL
64

5+
from fastapi import Depends, FastAPI, HTTPException, Request
6+
from fastapi.responses import RedirectResponse
7+
78
from . import crud, models, schemas
89
from .config import get_settings
910
from .database import SessionLocal, engine

fastapi-url-shortener/source_code_step_2/shortener_app/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import secrets
22

33
import validators
4+
from sqlalchemy.orm import Session
5+
46
from fastapi import Depends, FastAPI, HTTPException, Request
57
from fastapi.responses import RedirectResponse
6-
from sqlalchemy.orm import Session
78

89
from . import models, schemas
910
from .database import SessionLocal, engine

fastapi-url-shortener/source_code_step_3/shortener_app/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import validators
2+
from sqlalchemy.orm import Session
3+
24
from fastapi import Depends, FastAPI, HTTPException, Request
35
from fastapi.responses import RedirectResponse
4-
from sqlalchemy.orm import Session
56

67
from . import crud, models, schemas
78
from .database import SessionLocal, engine

fastapi/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Get Started With FastAPI
2+
3+
This repository contains code snippets discussed in the associated tutorial [Get Started With FastAPI](https://realpython.com/get-started-with-fastapi/).
4+
5+
## Installation
6+
7+
The recommended way to install FastAPI is with the [`[standard]`](https://github.com/fastapi/fastapi/blob/16d75d90eb96976a57b94cc24e4018859cd54c4d/pyproject.toml#L60) extra dependencies. This ensures you get all the tools you need for developing an API without having to hunt down additional packages later:
8+
9+
```console
10+
$ python -m pip install "fastapi[standard]"
11+
```
12+
13+
The quotes around `"fastapi[standard]"` ensure the command works correctly across different [terminals](https://realpython.com/terminal-commands/) and operating systems. With the command above, you install several useful packages, including the [FastAPI CLI](https://fastapi.tiangolo.com/fastapi-cli/) and [uvicorn](https://www.uvicorn.org/), an [ASGI](https://en.wikipedia.org/wiki/Asynchronous_Server_Gateway_Interface) server for running your application.
14+
15+
You can also use the `requirements.txt` file in this folder and run `python -m pip install -r requirements.txt` to install the standard dependencies of FastAPI.

0 commit comments

Comments
 (0)