Python
Putting it together
Combine the Blog class with a menu loop.
Put all the pieces together
In the final lesson you combine all pieces into a working command-line flow. You define a show_menu function; inside it you create a blog, add three posts and call the search. You print the post count and the search result.
In a real app this flow would branch into menu choices; here we use fixed data and verify the output. The point is to see how the classes and methods work together.
Exercise Write the show_menu function
The classes above are ready. Now write def show_menu():. Inside it create blog = Blog(), add three posts (two by "Ayşe", the third by "Ali"). Then add print("Post count:", len(blog.posts)) and print("Ayşe's posts:", len(blog.search_by_author("Ayşe"))). Finally call show_menu().
Show example solution
from dataclasses import dataclass, field
@dataclass
class Post:
title: str = ""
content: str = ""
author: str = ""
comments: list = field(default_factory=list)
def add_comment(self, comment):
self.comments.append(comment)
@dataclass
class Comment:
author: str = ""
text: str = ""
@dataclass
class Blog:
posts: list = field(default_factory=list)
def add_post(self, post):
self.posts.append(post)
def search_by_author(self, author):
return [p for p in self.posts if p.author == author]
def show_menu():
blog = Blog()
blog.add_post(Post(title="First", author="Ayşe"))
blog.add_post(Post(title="Second", author="Ali"))
blog.add_post(Post(title="Third", author="Ayşe"))
print("Post count:", len(blog.posts))
print("Ayşe's posts:", len(blog.search_by_author("Ayşe")))
show_menu()
Review of the project
In this project you defined three classes: Post, Comment and Blog. Each does one thing well: Blog manages the collection, Post holds comments, and Comment only carries data.
You used a list comprehension in the search method — a pattern shorter than a loop with a condition. Thanks to dataclasses you declared many fields without writing a constructor by hand. This structure scales to larger apps.
Test yourself
-
What does integrating the project achieve?
- All pieces work together; a complete app emerges
- Less code is written; shortcuts activate
- Classes become redundant and can be deleted
- The program automatically speeds up
Show answer
All pieces work together; a complete app emerges — Integration builds a complete flow where every piece works together.
-
Complete the menu call
____()
Show answer
show_menu()
-
Match the project classes
- Blog
- Manages the post collection
- Post
- Holds one post and its comments
- Comment
- Carries comment data
- search_by_author
- Searches by author
Next lesson: open