Skip to content

Commit 220c313

Browse files
committed
iterables
1 parent 13f7677 commit 220c313

File tree

6 files changed

+108
-5
lines changed

6 files changed

+108
-5
lines changed
1.48 KB
Binary file not shown.

API/main.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,74 @@
1-
from fastapi import FastAPI
1+
from fastapi import FastAPI, Response, status, HTTPException
22
from fastapi.params import Body
33
from pydantic import BaseModel
44
from typing import Optional
5+
from random import randrange
56
app = FastAPI()
67

78
class Post(BaseModel):
89
title: str
910
content: str
1011
published: bool = True
1112
rating: Optional[int] = None
13+
my_posts = [{"title": "title of post 1", "content": "content of post 1", "id": 1}, {"title": "favorite foods", "content": "pizza", "id": 2}]
14+
15+
def find_post(id):
16+
for p in my_posts:
17+
if p["id"] == id:
18+
return p
19+
20+
def find_index_post(id):
21+
for i, p in enumerate(my_posts):
22+
if p['id'] == id:
23+
return i
1224

1325
@app.get("/")
1426
def root():
1527
return {"message": "Welcome to my API"}
1628

1729
@app.get("/posts")
1830
def get_posts():
19-
return {"data": "this is your post"}
31+
return {"data": my_posts}
2032

21-
@app.post("/posts")
33+
@app.post("/posts", status_code=status.HTTP_201_CREATED)
2234
def create_post(post: Post):
23-
print(post.dict())
24-
return {"data": post}
35+
post_dict = post.dict()
36+
post_dict['id'] = randrange(0, 100000)
37+
my_posts.append(post_dict)
38+
return {"data": post_dict}
39+
40+
# @app.get("/posts/latest")
41+
# def get_latest_post():
42+
# post = my_posts[len(my_posts)-1]
43+
# return {"detail":post}
44+
45+
@app.get("/posts/{id}")
46+
def get_posts(id: int):
47+
48+
post = find_post(id)
49+
if not post:
50+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"post with id: {id} was not found")
51+
return{"post_detail": post}
52+
53+
#deleting a post,
54+
@app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT)
55+
def delete_posts(id: int):
56+
index = find_index_post(id)
57+
if index == None:
58+
raise HTTPException(status_code=status.HTTP_204_NO_CONTENT,detail=f"post with id: {id} does not exist")
59+
my_posts.pop(index)
60+
return Response(status_code=status.HTTP_204_NO_CONTENT)
61+
#update post
62+
63+
@app.put("posts/{id}")
64+
def update_post(id: int, post:Post):
65+
index = find_index_post(id)
66+
if index == None:
67+
raise HTTPException(status_code=status.HTTP_204_NO_CONTENT,detail=f"post with id: {id} does not exist")
68+
post_dict = post.dict()
69+
post_dict['id'] = id
70+
my_posts[index] = post_dict
71+
print(post)
72+
return {'data': post_dict}
73+
2574

iterables/dict.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Alishia = {"fName": "Aisha", "lName": "Juma"}
2+
3+
for key in Alishia:
4+
print(key, Alishia[key])

iterables/fibonacci.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class FibGenerator:
2+
#initialize
3+
def __init__(self):
4+
self.first = 0
5+
self.second = 1
6+
7+
#iterator
8+
def __iter__(self):
9+
return self
10+
11+
#next
12+
def __next__(self):
13+
fibNum = self.first + self.second
14+
self.first = self.second
15+
self.second = fibNum
16+
return fibNum
17+
18+
fibSeq = FibGenerator()
19+
for i in range(10):
20+
print("Fib: ", next(fibSeq))

iterables/iterator.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Alphabet:
2+
3+
def __init__(self):
4+
self.letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
5+
self.index = -1
6+
#iterator method
7+
def __iter__(self):
8+
return self
9+
#next method
10+
def __next__(self):
11+
if self.index >= len(self.letters) - 1:
12+
raise StopIteration
13+
self.index +=1
14+
return self.letters[self.index]
15+
#custom object
16+
alpha = Alphabet()
17+
for letter in alpha:
18+
print(letter)
19+

iterables/test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#iterable-stored sequence of value-list of values and can act as object
2+
#act as an object that produce objects one at a time
3+
4+
#iterator is different from iterables
5+
#oject with a method __iter__ __next __ return the next value from a sequence of vvalues
6+
sampstr = iter("Sample")
7+
print("Char : ", next(sampstr))
8+
print("Char : ", next(sampstr))
9+
print("Char : ", next(sampstr))
10+
print("Char : ", next(sampstr))
11+
print("Char : ", next(sampstr))

0 commit comments

Comments
 (0)