Last active
October 1, 2023 20:33
-
-
Save Basemera/e645d37448465f6d8549a449be56e8ad to your computer and use it in GitHub Desktop.
Simple implementation of a browser history app to demonstrate application of the doubly linked list data structure. Also added a file with a few tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Creating a simple browser history app using a doubly linked list in Python is a practical project. | |
# Below is a basic implementation of a browser history app that allows you to navigate through visited websites using a doubly linked list: | |
class Node: | |
def __init__(self, url): | |
self.url = url | |
self.prev = None | |
self.next = None | |
def __repr__(self) -> str: | |
return self.url | |
class BrowserHistory: | |
def __init__(self): | |
self.current_page = None | |
def visit(self, url): | |
new_page = Node(url) | |
if self.current_page: | |
new_page.prev = self.current_page | |
self.current_page.next = new_page | |
self.current_page = new_page | |
print("visited_page:", self.current_page) | |
def back(self): | |
if self.current_page and self.current_page.prev: | |
self.current_page = self.current_page.prev | |
print("current_page:", self.current_page) | |
else: | |
print("No page to go back to") | |
print("current_page:", self.current_page) | |
def forward(self): | |
if self.current_page and self.current_page.next: | |
self.current_page = self.current_page.next | |
print("current_page:", self.current_page) | |
else: | |
print("No page to go forward to") | |
print("current_page:", self.current_page) | |
def display_current_page(self): | |
if self.current_page: | |
return f"Current Page: {self.current_page}" | |
else: | |
return "No page is currently open." | |
def main(): | |
browser_history = BrowserHistory() | |
while True: | |
print("\nSimple Browser History App") | |
print("1. Visit a Website") | |
print("2. Go Back") | |
print("3. Go Forward") | |
print("4. Display Current Page") | |
print("5. Exit") | |
choice = input("Enter your choice: ") | |
if choice == "1": | |
url = input("Enter the URL: ") | |
browser_history.visit(url) | |
elif choice == "2": | |
browser_history.back() | |
elif choice == "3": | |
browser_history.forward() | |
elif choice == "4": | |
print(browser_history.display_current_page()) | |
elif choice == "5": | |
print("Exiting the Browser History App.") | |
break | |
else: | |
print("Invalid choice. Please try again.") | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from browser_history_dll import BrowserHistory, Node | |
class TestBrowserHistory(unittest.TestCase): | |
def setUp(self): | |
self.browser_history = BrowserHistory() | |
def test_initial_current_page(self): | |
# Test that the current page is None when the history is empty | |
self.assertIsNone(self.browser_history.current_page) | |
def test_visit_website(self): | |
# Test visiting a website and checking if it becomes the current page | |
self.browser_history.visit("https://www.example.com") | |
self.assertEqual(self.browser_history.current_page.url, "https://www.example.com") | |
def test_back_navigation(self): | |
# Test navigating back in the history | |
self.browser_history.visit("https://www.example.com") | |
self.browser_history.visit("https://www.google.com") | |
self.browser_history.back() | |
self.assertEqual(self.browser_history.current_page.url, "https://www.example.com") | |
def test_forward_navigation(self): | |
# Test navigating forward in the history | |
self.browser_history.visit("https://www.example.com") | |
self.browser_history.visit("https://www.google.com") | |
prev_url = self.browser_history.current_page.prev | |
self.browser_history.current_page = prev_url | |
self.browser_history.forward() | |
self.assertEqual(self.browser_history.current_page.url, "https://www.google.com") | |
def test_display_current_page(self): | |
# Test displaying the current page | |
self.browser_history.visit("https://www.example.com") | |
self.assertEqual(self.browser_history.display_current_page(), "Current Page: https://www.example.com") | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment