Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web summit demo #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions web_summit_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import sqlite3
from datetime import datetime

# Database connection setup
conn = sqlite3.connect('enterprise_inventory.db')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion(medium-importance): Consider using a context manager to handle the database connection to ensure it is closed properly.

cursor = conn.cursor()

# Ensure that the needed table exists
cursor.execute('''
CREATE TABLE IF NOT EXISTS inventory (
product_id INTEGER PRIMARY KEY,
product_name TEXT NOT NULL,
quantity INTEGER NOT NULL,
last_updated TIMESTAMP NOT NULL
)
''')
conn.commit()

# A class to represent the inventory
class Inventory:
def __init__(self):
self._products = self._load_products()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion(medium-importance): Loading all products into memory might not be scalable for a large inventory. Consider fetching data on demand.


def _load_products(self):
cursor.execute("SELECT * FROM inventory")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion(medium-importance): Using 'SELECT *' is generally discouraged as it may lead to unnecessary data transfer if not all columns are needed.

return {row[0]: {'product_name': row[1], 'quantity': row[2], 'last_updated': row[3]} for row in cursor.fetchall()}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion(medium-importance): Consider using named tuples or a custom class for better readability and maintainability of the row data structure.


def add_product(self, product_name, quantity):
if not isinstance(product_name, str) or not isinstance(quantity, int):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion(low-importance): Type checking for arguments can be improved by using Python's type hinting feature.

raise ValueError("Invalid input types for product name or quantity.")

if quantity <= 0:
raise ValueError("Quantity must be positive.")

now = datetime.now()
cursor.execute("INSERT INTO inventory (product_name, quantity, last_updated) VALUES (?, ?, ?)",
(product_name, quantity, now))
conn.commit()
self._products[cursor.lastrowid] = {'product_name': product_name, 'quantity': quantity, 'last_updated': now}

def remove_product(self, product_id):
if product_id not in self._products:
raise ValueError("Product ID does not exist in inventory.")

cursor.execute("DELETE FROM inventory WHERE product_id = ?", (product_id,))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion(medium-importance): It's good practice to check the affected rows after a DELETE operation to ensure a product was actually removed.

conn.commit()
del self._products[product_id]

def update_quantity(self, product_id, quantity_change):
if product_id not in self._products:
raise ValueError("Product ID does not exist in inventory.")

new_quantity = self._products[product_id]['quantity'] + quantity_change

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion(medium-importance): Consider adding a check to ensure that the quantity_change does not result in a negative quantity before updating the database.

if new_quantity < 0:
raise ValueError("Cannot have negative quantity in inventory.")

now = datetime.now()
cursor.execute("UPDATE inventory SET quantity = ?, last_updated = ? WHERE product_id = ?",
(new_quantity, now, product_id))
conn.commit()
self._products[product_id].update({'quantity': new_quantity, 'last_updated': now})

def get_product_info(self, product_id):
return self._products.get(product_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion(low-importance): Returning None for a non-existent product_id might be ambiguous. Consider raising an exception or returning a more descriptive result.


def __del__(self):
conn.close()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo issue(high-importance): The del method is not a reliable place for cleanup like closing database connections. Consider using context managers or explicit close calls.


def main():
inventory = Inventory()

# Add a few products
inventory.add_product('Laptop', 10)
inventory.add_product('Mouse', 50)
inventory.add_product('Keyboard', 20)

# Remove a product
inventory.remove_product(2)

# Update quantity
inventory.update_quantity(1, -2)

# Print information about a product
print(inventory.get_product_info(1))

if __name__ == "__main__":
main()