New Project

In this project, you’ll develop a Python application that monitors stock prices in real time and notifies the user when significant changes occur. The project will leverage Python’s object-oriented programming capabilities, incorporate debugging and testing strategies, and utilize version control workflows. Below is a comprehensive guide designed for the Discourse forum.

────────────────────────── 1. Objective
Develop a Python-based application that:
• Fetches or simulates real-time stock price data
• Uses an object-oriented design for maintainability
• Integrates a notification or alert mechanism when thresholds are breached
• Includes debugging, unit testing, and version control best practices

────────────────────────── 2. Learning Outcomes
After completing this project, you will be able to:
• Design modular Python applications using object-oriented programming
• Implement various debugging and testing techniques to ensure code quality
• Utilize Git for version control to manage project changes
• Understand how to simulate real-time data and work with potentially asynchronous tasks

────────────────────────── 3. Pre-requisite Skills
Before starting, ensure you are comfortable with:
• Basic Python syntax and data structures
• Concepts of object-oriented programming (classes, inheritance, etc.)
• Familiarity with using Git and GitHub (or another version control platform)
• Basic knowledge of Python’s debugging tools and unit testing frameworks

────────────────────────── 4. Skills Gained
Through this project, you will gain:
• Advanced Python programming techniques
• Practical experience in object-oriented design patterns
• Techniques for debugging and unit testing in Python
• Competence in Git for collaborative and solo software development

────────────────────────── 5. Tools Explored
During this project, you will explore:
• Python 3.x (programming language)
• Git (version control system)
• unittest or pytest (Python testing frameworks)
• Optionally, requests or a simulation module (for fetching stock data)
• An Integrated Development Environment (IDE) such as VSCode or PyCharm

────────────────────────── 6. Steps and Tasks

────────────────────────── Step 1: Project Setup and Version Control Initialization

• Task: Initialize a Git repository, create a virtual environment, and set up project structure
• Code Snippet:

Terminal commands:

Create project directory and enter it

mkdir StockPriceNotifier
cd StockPriceNotifier

Initialize Git repository

git init

Set up a virtual environment

python3 -m venv env
source env/bin/activate # On Windows: env\Scripts\activate

Create recommended directories and files

mkdir src tests
touch src/init.py
touch README.md requirements.txt

────────────────────────── Step 2: Design the Stock Data Model with OOP

• Task: Build an object-oriented model to represent stock data
• Code Snippet:

File: src/stock.py

class Stock: def init(self, symbol, price): self.symbol = symbol self.price = price

   def update_price(self, new_price):
       """Updates the stock price and returns the change."""
       change = new_price - self.price
       self.price = new_price
       return change

   def __str__(self):
       return f"Stock(symbol={self.symbol}, price={self.price})"

if name == “main”: # Sample usage for debugging/testing apple = Stock(“AAPL”, 150) print(apple) print(“Price change:”, apple.update_price(155))

────────────────────────── Step 3: Implement the Notification System

• Task: Develop a notifier class using OOP to alert users when significant changes occur
• Code Snippet:

File: src/notifier.py

class Notifier: def init(self, threshold): self.threshold = threshold # e.g., notify if price change exceeds this value

   def check_and_notify(self, stock, change):
       if abs(change) >= self.threshold:
           self.notify(stock, change)

   def notify(self, stock, change):
       # For this example, simply output a print statement. Replace with email, SMS, etc.
       direction = "increased" if change > 0 else "decreased"
       print(f"Alert: {stock.symbol} has {direction} by {abs(change)} to {stock.price}!")

if name == “main”: # Testing notifier directly for debugging from stock import Stock apple = Stock(“AAPL”, 150) notifier = Notifier(threshold=4) price_change = apple.update_price(156) notifier.check_and_notify(apple, price_change)

────────────────────────── Step 4: Simulate Real-Time Data Fetching

• Task: Create a simulation mechanism to mimic real-time stock price updates
• Code Snippet:

File: src/simulator.py

import random import time from stock import Stock from notifier import Notifier

class StockSimulator: def init(self, stock, notifier, interval=2, iterations=5): self.stock = stock self.notifier = notifier self.interval = interval self.iterations = iterations

   def simulate(self):
       for _ in range(self.iterations):
           # Simulate a random price change
           new_price = round(self.stock.price + random.uniform(-5, 5), 2)
           change = self.stock.update_price(new_price)
           print(f"Updated {self.stock.symbol} to {self.stock.price}")
           self.notifier.check_and_notify(self.stock, change)
           time.sleep(self.interval)

if name == “main”: apple = Stock(“AAPL”, 150) notifier = Notifier(threshold=3) simulator = StockSimulator(apple, notifier) simulator.simulate()

────────────────────────── Step 5: Debugging and Unit Testing

• Task: Write unit tests to validate the functionality of Stock and Notifier classes
• Code Snippet:

File: tests/test_stock.py

import unittest from src.stock import Stock

class TestStock(unittest.TestCase): def setUp(self): self.stock = Stock(“GOOGL”, 2000)

   def test_update_price_increase(self):
       change = self.stock.update_price(2005)
       self.assertEqual(change, 5)
       self.assertEqual(self.stock.price, 2005)

   def test_update_price_decrease(self):
       change = self.stock.update_price(1995)
       self.assertEqual(change, -5)
       self.assertEqual(self.stock.price, 1995)

if name == ‘main’: unittest.main()

File: tests/test_notifier.py

import unittest from src.stock import Stock from src.notifier import Notifier

class TestNotifier(unittest.TestCase): def setUp(self): self.stock = Stock(“TSLA”, 600) self.threshold = 10 # Override notify to capture messages for testing instead of printing self.notifications = class TestNotifierClass(Notifier): def notify(inner_self, stock, change): direction = “increased” if change > 0 else “decreased” message = f"{stock.symbol} {direction} by {abs(change)} to {stock.price}" self.notifications.append(message) self.notifier = TestNotifierClass(threshold=self.threshold)

   def test_notification_trigger(self):
       change = self.stock.update_price(615)
       self.notifier.check_and_notify(self.stock, change)
       self.assertTrue(len(self.notifications) > 0)

   def test_no_notification(self):
       change = self.stock.update_price(605)
       # With a threshold of 10, change of 5 should not trigger a notification
       self.notifier.check_and_notify(self.stock, change)
       self.assertEqual(len(self.notifications), 0)

if name == ‘main’: unittest.main()

────────────────────────── Step 6: Final Integration and Commit

• Task: Integrate all modules, perform a final debugging session, and commit the code
• Code Snippet / Steps:

Terminal commands:

Run unit tests to ensure everything is working

python -m unittest discover -s tests

Add all files to Git

git add .

Commit with an informative message

git commit -m “Initial commit: Added stock model, notifier, simulator, and tests”

Optionally, you could continue improving the project by integrating external APIs (e.g., Alpha Vantage, IEX Cloud) for real-time data, implementing asynchronous I/O with asyncio for better performance, or adding more intricate notification systems.

────────────────────────── Congratulations on building a comprehensive, real-time stock price notifier using Python! This project not only sharpens your programming skills in object-oriented design and debugging but also gives you practical experience in version control with Git. Enjoy coding and refining your notifier further!

Access the Code-Along for this Skill-Builder Project to join discussions, utilize the t3 AI Mentor, and more.