-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Type: Enhancement
Summary of PR: This PR introduces a new script for managing an inventory database with basic operations like adding, removing, and updating products.
General PR suggestions
- Consider encapsulating database operations within a separate class or module to separate concerns and improve maintainability.
- Implement error handling for database operations to ensure the application can recover from or properly report database-related issues.
- Use context managers for database connections to ensure they are properly closed even in the event of an error.
from datetime import datetime | ||
|
||
# Database connection setup | ||
conn = sqlite3.connect('enterprise_inventory.db') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return self._products.get(product_id) | ||
|
||
def __del__(self): | ||
conn.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# A class to represent the inventory | ||
class Inventory: | ||
def __init__(self): | ||
self._products = self._load_products() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self._products = self._load_products() | ||
|
||
def _load_products(self): | ||
cursor.execute("SELECT * FROM inventory") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
def _load_products(self): | ||
cursor.execute("SELECT * FROM inventory") | ||
return {row[0]: {'product_name': row[1], 'quantity': row[2], 'last_updated': row[3]} for row in cursor.fetchall()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return {row[0]: {'product_name': row[1], 'quantity': row[2], 'last_updated': row[3]} for row in cursor.fetchall()} | ||
|
||
def add_product(self, product_name, quantity): | ||
if not isinstance(product_name, str) or not isinstance(quantity, int): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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,)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self._products[product_id].update({'quantity': new_quantity, 'last_updated': now}) | ||
|
||
def get_product_info(self, product_id): | ||
return self._products.get(product_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.