DEV Community

Brad
Brad

Posted on

Python Inventory Tracker: Automatic Low-Stock Alerts in 50 Lines

Python Inventory Tracker: Automatic Low-Stock Alerts in 50 Lines

Manual inventory tracking breaks down fast. This script tracks stock in SQLite and alerts you before you run out.

import sqlite3
from datetime import datetime

class InventoryTracker:
    def __init__(self):
        self.db = sqlite3.connect('inventory.db')
        self.db.execute('''CREATE TABLE IF NOT EXISTS stock
            (id INTEGER PRIMARY KEY, name TEXT, qty INTEGER, reorder INTEGER, cost REAL)''')
        self.db.commit()

    def add(self, name, qty, reorder_at, unit_cost):
        self.db.execute('INSERT INTO stock VALUES (NULL,?,?,?,?)', (name, qty, reorder_at, unit_cost))
        self.db.commit()

    def consume(self, item_id, amount):
        self.db.execute('UPDATE stock SET qty = qty - ? WHERE id = ?', (amount, item_id))
        self.db.commit()
        row = self.db.execute('SELECT name, qty, reorder FROM stock WHERE id=?', (item_id,)).fetchone()
        if row[1] <= row[2]:
            print(f"⚠️  LOW STOCK: {row[0]} ({row[1]} left, reorder at {row[2]})")

    def report(self):
        rows = self.db.execute('SELECT name, qty, qty*cost as val FROM stock').fetchall()
        print(f"\n📦 Inventory Report - {datetime.now().strftime('%Y-%m-%d')}")
        for name, qty, val in rows:
            icon = "🔴" if qty < 10 else "🟢"
            print(f"  {icon} {name}: {qty} units (${val:.2f})")
        print(f"  Total: ${sum(r[2] for r in rows):.2f}")

# Example
inv = InventoryTracker()
inv.add("Office Paper (ream)", 50, 10, 8.99)
inv.add("Printer Ink", 5, 3, 24.99)
inv.consume(2, 3)  # Triggers low stock alert
inv.report()
Enter fullscreen mode Exit fullscreen mode

What you get

  • SQLite storage — no database server needed
  • Automatic alerts — know before you run out
  • Instant valuation — see the dollar value of your stock

This is one of 50+ scripts in the Python Business Automation Toolkit.

One-time purchase, lifetime use: https://lukassbrad.gumroad.com/l/ugeka

Top comments (0)