ECOPRINT / database.py
zainab1234awan's picture
Upload 4 files
aa0f861 verified
Raw
History Blame Contribute Delete
29.6 kB
import json
import os
from typing import List, Optional
from models import Product, ProductCategory
class ProductDatabase:
def __init__(self, json_file: str = "products.json"):
self.json_file = json_file
self.products = self.load_products()
def load_products(self) -> List[Product]:
if os.path.exists(self.json_file):
with open(self.json_file, 'r') as f:
data = json.load(f)
return [Product(**item) for item in data]
return self.create_default_products()
def create_default_products(self) -> List[Product]:
products = [
# Personal Care Products
Product(
id=1,
name="Regular Shampoo",
category=ProductCategory.PERSONAL_CARE,
ingredients=["sodium lauryl sulfate", "parabens", "fragrance", "sodium chloride"],
packaging=["plastic bottle"],
contains_chemicals=True,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=2,
name="Organic Shampoo Bar",
category=ProductCategory.PERSONAL_CARE,
ingredients=["coconut oil", "shea butter", "essential oils", "clay"],
packaging=["cardboard box"],
is_organic=True,
contains_chemicals=False,
water_usage="low",
carbon_footprint="low"
),
Product(
id=3,
name="Plastic Toothbrush",
category=ProductCategory.PERSONAL_CARE,
materials=["plastic", "nylon"],
packaging=["plastic blister pack"],
contains_chemicals=True,
is_recyclable=False,
water_usage="low",
carbon_footprint="medium"
),
Product(
id=4,
name="Bamboo Toothbrush",
category=ProductCategory.PERSONAL_CARE,
materials=["bamboo", "nylon", "cardboard"],
packaging=["cardboard box"],
is_organic=True,
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=5,
name="Plastic Deodorant",
category=ProductCategory.PERSONAL_CARE,
ingredients=["aluminum compounds", "parabens", "propylene glycol", "fragrance"],
packaging=["plastic container"],
contains_chemicals=True,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=6,
name="Natural Crystal Deodorant",
category=ProductCategory.PERSONAL_CARE,
ingredients=["potassium alum", "mineral salts"],
packaging=["cardboard box"],
contains_chemicals=False,
is_organic=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=7,
name="Disposable Razor",
category=ProductCategory.PERSONAL_CARE,
materials=["plastic", "stainless steel"],
packaging=["plastic packaging"],
contains_chemicals=True,
is_recyclable=False,
water_usage="low",
carbon_footprint="medium"
),
Product(
id=8,
name="Safety Razor",
category=ProductCategory.PERSONAL_CARE,
materials=["stainless steel", "brass"],
packaging=["cardboard box"],
contains_chemicals=False,
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
# Food Products
Product(
id=9,
name="Palm Oil Cooking Oil",
category=ProductCategory.FOOD,
ingredients=["palm oil"],
packaging=["plastic bottle"],
contains_palm_oil=True,
water_usage="high",
carbon_footprint="high"
),
Product(
id=10,
name="Olive Oil",
category=ProductCategory.FOOD,
ingredients=["olive oil"],
packaging=["glass bottle"],
contains_palm_oil=False,
is_organic=True,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=11,
name="Plastic-Wrapped Cucumber",
category=ProductCategory.FOOD,
ingredients=["cucumber"],
packaging=["plastic wrap"],
contains_chemicals=False,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=12,
name="Loose Organic Cucumber",
category=ProductCategory.FOOD,
ingredients=["cucumber"],
packaging=["none"],
is_organic=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=13,
name="Coffee Pods (Plastic)",
category=ProductCategory.FOOD,
ingredients=["coffee", "plastic"],
packaging=["plastic pods", "cardboard box"],
contains_chemicals=True,
is_recyclable=False,
water_usage="high",
carbon_footprint="high"
),
Product(
id=14,
name="Ground Coffee (Compostable Bag)",
category=ProductCategory.FOOD,
ingredients=["coffee"],
packaging=["compostable paper bag"],
is_organic=True,
is_recyclable=True,
water_usage="medium",
carbon_footprint="medium"
),
# Beverages
Product(
id=15,
name="Plastic Water Bottle",
category=ProductCategory.BEVERAGE,
materials=["plastic"],
packaging=["plastic bottle"],
is_recyclable=True,
water_usage="medium",
carbon_footprint="high"
),
Product(
id=16,
name="Reusable Stainless Steel Bottle",
category=ProductCategory.BEVERAGE,
materials=["stainless steel"],
packaging=["cardboard box"],
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=17,
name="Aluminum Soda Can",
category=ProductCategory.BEVERAGE,
materials=["aluminum"],
packaging=["aluminum can"],
is_recyclable=True,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=18,
name="Plastic Soda Bottle",
category=ProductCategory.BEVERAGE,
materials=["plastic"],
packaging=["plastic bottle"],
is_recyclable=True,
water_usage="medium",
carbon_footprint="high"
),
Product(
id=19,
name="Glass Milk Bottle",
category=ProductCategory.BEVERAGE,
materials=["glass"],
packaging=["glass bottle"],
is_recyclable=True,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=20,
name="Tetra Pak Juice Box",
category=ProductCategory.BEVERAGE,
materials=["paper", "plastic", "aluminum"],
packaging=["tetra pak"],
is_recyclable=False,
water_usage="medium",
carbon_footprint="medium"
),
# Clothing
Product(
id=21,
name="Organic Cotton T-shirt",
category=ProductCategory.CLOTHING,
materials=["organic cotton"],
packaging=["paper bag"],
is_organic=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=22,
name="Conventional Cotton T-shirt",
category=ProductCategory.CLOTHING,
materials=["cotton"],
packaging=["plastic bag"],
contains_chemicals=True,
water_usage="high",
carbon_footprint="medium"
),
Product(
id=23,
name="Polyester Jacket",
category=ProductCategory.CLOTHING,
materials=["polyester", "nylon"],
packaging=["plastic bag"],
contains_chemicals=True,
water_usage="medium",
carbon_footprint="high"
),
Product(
id=24,
name="Recycled Polyester Jacket",
category=ProductCategory.CLOTHING,
materials=["recycled polyester"],
packaging=["recycled paper bag"],
is_recyclable=True,
water_usage="medium",
carbon_footprint="low"
),
Product(
id=25,
name="Leather Boots",
category=ProductCategory.CLOTHING,
materials=["leather", "rubber"],
packaging=["cardboard box"],
contains_chemicals=True,
water_usage="high",
carbon_footprint="high"
),
Product(
id=26,
name="Vegan Leather Boots",
category=ProductCategory.CLOTHING,
materials=["polyurethane", "rubber"],
packaging=["cardboard box"],
contains_chemicals=True,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=27,
name="Fast Fashion Dress",
category=ProductCategory.CLOTHING,
materials=["polyester", "spandex"],
packaging=["plastic bag"],
contains_chemicals=True,
water_usage="high",
carbon_footprint="high"
),
Product(
id=28,
name="Vintage/Second-hand Dress",
category=ProductCategory.CLOTHING,
materials=["various"],
packaging=["none"],
is_organic=False,
water_usage="low",
carbon_footprint="very low"
),
# Household Products
Product(
id=29,
name="Plastic Dish Scourer",
category=ProductCategory.HOUSEHOLD,
materials=["plastic", "nylon"],
packaging=["plastic packaging"],
contains_chemicals=True,
is_recyclable=False,
water_usage="low",
carbon_footprint="medium"
),
Product(
id=30,
name="Natural Loofah Scourer",
category=ProductCategory.HOUSEHOLD,
materials=["loofah", "cotton"],
packaging=["cardboard"],
is_organic=True,
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=31,
name="Chemical Cleaner (Bleach)",
category=ProductCategory.HOUSEHOLD,
ingredients=["sodium hypochlorite", "surfactants", "fragrance"],
packaging=["plastic bottle"],
contains_chemicals=True,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=32,
name="Vinegar-Based Cleaner",
category=ProductCategory.HOUSEHOLD,
ingredients=["vinegar", "essential oils", "water"],
packaging=["glass bottle"],
contains_chemicals=False,
is_organic=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=33,
name="Plastic Trash Bags",
category=ProductCategory.HOUSEHOLD,
materials=["plastic"],
packaging=["cardboard box"],
contains_chemicals=True,
is_recyclable=False,
water_usage="low",
carbon_footprint="medium"
),
Product(
id=34,
name="Compostable Trash Bags",
category=ProductCategory.HOUSEHOLD,
materials=["corn starch", "plant materials"],
packaging=["cardboard box"],
is_organic=True,
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
# Electronics
Product(
id=35,
name="Smartphone",
category=ProductCategory.ELECTRONICS,
materials=["plastic", "glass", "lithium", "rare earth metals"],
packaging=["cardboard box", "plastic"],
contains_chemicals=True,
is_recyclable=False,
water_usage="high",
carbon_footprint="high"
),
Product(
id=36,
name="Refurbished Smartphone",
category=ProductCategory.ELECTRONICS,
materials=["plastic", "glass", "lithium", "rare earth metals"],
packaging=["recycled cardboard"],
contains_chemicals=True,
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=37,
name="Single-use Batteries",
category=ProductCategory.ELECTRONICS,
materials=["zinc", "manganese", "plastic", "chemicals"],
packaging=["plastic blister pack"],
contains_chemicals=True,
is_recyclable=False,
water_usage="medium",
carbon_footprint="high"
),
Product(
id=38,
name="Rechargeable Batteries",
category=ProductCategory.ELECTRONICS,
materials=["lithium-ion", "nickel"],
packaging=["cardboard box"],
contains_chemicals=True,
is_recyclable=True,
water_usage="medium",
carbon_footprint="medium"
),
# Additional Food Products
Product(
id=39,
name="Imported Avocados",
category=ProductCategory.FOOD,
ingredients=["avocados"],
packaging=["none"],
contains_palm_oil=False,
water_usage="high", # High water usage
carbon_footprint="high" # High transport emissions
),
Product(
id=40,
name="Local Seasonal Vegetables",
category=ProductCategory.FOOD,
ingredients=["mixed vegetables"],
packaging=["paper bag"],
is_organic=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=41,
name="Farmed Salmon",
category=ProductCategory.FOOD,
ingredients=["salmon"],
packaging=["plastic tray", "plastic wrap"],
contains_chemicals=True,
water_usage="high",
carbon_footprint="medium"
),
Product(
id=42,
name="Wild Caught Salmon",
category=ProductCategory.FOOD,
ingredients=["salmon"],
packaging=["paper"],
is_organic=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=43,
name="Beef (Feedlot)",
category=ProductCategory.FOOD,
ingredients=["beef"],
packaging=["plastic wrap", "styrofoam"],
contains_chemicals=True,
water_usage="high",
carbon_footprint="very high"
),
Product(
id=44,
name="Grass-Fed Beef",
category=ProductCategory.FOOD,
ingredients=["beef"],
packaging=["paper", "compostable wrap"],
is_organic=True,
water_usage="medium",
carbon_footprint="high"
),
Product(
id=45,
name="Plant-Based Burger",
category=ProductCategory.FOOD,
ingredients=["pea protein", "coconut oil", "potato starch"],
packaging=["cardboard box"],
contains_palm_oil=False,
water_usage="low",
carbon_footprint="low"
),
# Household Paper Products
Product(
id=46,
name="Virgin Paper Towels",
category=ProductCategory.HOUSEHOLD,
materials=["wood pulp"],
packaging=["plastic wrap"],
contains_palm_oil=False,
water_usage="high",
carbon_footprint="medium"
),
Product(
id=47,
name="Recycled Paper Towels",
category=ProductCategory.HOUSEHOLD,
materials=["recycled paper"],
packaging=["paper wrap"],
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=48,
name="Bamboo Toilet Paper",
category=ProductCategory.HOUSEHOLD,
materials=["bamboo"],
packaging=["paper wrap"],
is_organic=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=49,
name="Regular Toilet Paper",
category=ProductCategory.HOUSEHOLD,
materials=["wood pulp"],
packaging=["plastic wrap"],
contains_palm_oil=False,
water_usage="high",
carbon_footprint="medium"
),
# Personal Care - Makeup
Product(
id=50,
name="Conventional Lipstick",
category=ProductCategory.PERSONAL_CARE,
ingredients=["parabens", "petrolatum", "fragrance", "synthetic dyes"],
packaging=["plastic tube"],
contains_chemicals=True,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=51,
name="Natural Lipstick",
category=ProductCategory.PERSONAL_CARE,
ingredients=["beeswax", "shea butter", "natural pigments"],
packaging=["recycled cardboard"],
is_organic=True,
contains_chemicals=False,
water_usage="low",
carbon_footprint="low"
),
Product(
id=52,
name="Plastic Makeup Wipes",
category=ProductCategory.PERSONAL_CARE,
ingredients=["polyester", "chemicals", "preservatives"],
packaging=["plastic pouch"],
contains_chemicals=True,
is_recyclable=False,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=53,
name="Reusable Cotton Rounds",
category=ProductCategory.PERSONAL_CARE,
materials=["organic cotton"],
packaging=["cardboard box"],
is_organic=True,
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
# Baby Products
Product(
id=54,
name="Disposable Diapers",
category=ProductCategory.PERSONAL_CARE,
materials=["plastic", "wood pulp", "superabsorbent polymers"],
packaging=["plastic bag"],
contains_chemicals=True,
is_recyclable=False,
water_usage="high",
carbon_footprint="high"
),
Product(
id=55,
name="Cloth Diapers",
category=ProductCategory.PERSONAL_CARE,
materials=["organic cotton", "bamboo"],
packaging=["reusable bag"],
is_organic=True,
is_recyclable=True,
water_usage="medium",
carbon_footprint="low"
),
# Pet Products
Product(
id=56,
name="Plastic Pet Food Bowl",
category=ProductCategory.HOUSEHOLD,
materials=["plastic"],
packaging=["plastic"],
contains_chemicals=True,
water_usage="low",
carbon_footprint="medium"
),
Product(
id=57,
name="Stainless Steel Pet Bowl",
category=ProductCategory.HOUSEHOLD,
materials=["stainless steel"],
packaging=["cardboard"],
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=58,
name="Conventional Cat Litter",
category=ProductCategory.HOUSEHOLD,
materials=["clay", "silica", "chemicals"],
packaging=["plastic bag"],
contains_chemicals=True,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=59,
name="Biodegradable Cat Litter",
category=ProductCategory.HOUSEHOLD,
materials=["recycled paper", "wood", "corn"],
packaging=["paper bag"],
is_organic=True,
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
# Office Supplies
Product(
id=60,
name="Plastic Pens (Pack)",
category=ProductCategory.OTHER,
materials=["plastic", "ink"],
packaging=["plastic blister pack"],
contains_chemicals=True,
is_recyclable=False,
water_usage="medium",
carbon_footprint="medium"
),
Product(
id=61,
name="Refillable Bamboo Pen",
category=ProductCategory.OTHER,
materials=["bamboo", "metal", "ink"],
packaging=["cardboard"],
is_organic=True,
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
Product(
id=62,
name="Plastic Stapler",
category=ProductCategory.OTHER,
materials=["plastic", "metal"],
packaging=["plastic"],
contains_chemicals=True,
is_recyclable=False,
water_usage="low",
carbon_footprint="medium"
),
Product(
id=63,
name="Metal Stapler",
category=ProductCategory.OTHER,
materials=["metal"],
packaging=["cardboard"],
is_recyclable=True,
water_usage="low",
carbon_footprint="low"
),
]
# Save to JSON file
with open(self.json_file, 'w') as f:
json.dump([p.dict() for p in products], f, indent=2)
return products
def search_product(self, query: str) -> Optional[Product]:
"""Search for a product by name (partial match)"""
query = query.lower()
# First try exact match
for product in self.products:
if query == product.name.lower():
return product
# Then try partial match
for product in self.products:
if query in product.name.lower():
return product
# Try matching individual words
query_words = query.split()
best_match = None
best_match_count = 0
for product in self.products:
product_name_lower = product.name.lower()
match_count = sum(1 for word in query_words if word in product_name_lower)
if match_count > best_match_count:
best_match_count = match_count
best_match = product
return best_match if best_match_count > 0 else None
def get_products_by_category(self, category: ProductCategory) -> List[Product]:
"""Get all products in a specific category"""
return [p for p in self.products if p.category == category]
def get_all_products(self) -> List[Product]:
"""Get all products"""
return self.products
def get_alternatives(self, category: ProductCategory, exclude_id: int, limit: int = 3) -> List[Product]:
"""Get alternative products in the same category"""
# Get products in same category with better eco characteristics
alternatives = []
for product in self.products:
if product.id != exclude_id and product.category == category:
# Prioritize organic, recyclable, low-impact products
score = 0
if product.is_organic:
score += 3
if product.is_recyclable:
score += 2
if not product.contains_chemicals:
score += 2
if not product.contains_palm_oil:
score += 1
if product.water_usage == 'low':
score += 1
if product.carbon_footprint == 'low':
score += 1
alternatives.append((score, product))
# Sort by score (higher is better) and return top alternatives
alternatives.sort(key=lambda x: x[0], reverse=True)
return [p for _, p in alternatives[:limit]]
def add_product(self, product: Product) -> int:
"""Add a new product to the database"""
# Generate new ID
new_id = max([p.id for p in self.products if p.id is not None] + [0]) + 1
product.id = new_id
self.products.append(product)
# Save to JSON
with open(self.json_file, 'w') as f:
json.dump([p.dict() for p in self.products], f, indent=2)
return new_id
def get_product_summary(self) -> dict:
"""Get summary statistics about products"""
summary = {
'total_products': len(self.products),
'by_category': {},
'organic_count': 0,
'recyclable_count': 0,
'contains_palm_oil': 0,
'contains_chemicals': 0
}
for product in self.products:
# Count by category
category = product.category.value
summary['by_category'][category] = summary['by_category'].get(category, 0) + 1
# Count attributes
if product.is_organic:
summary['organic_count'] += 1
if product.is_recyclable:
summary['recyclable_count'] += 1
if product.contains_palm_oil:
summary['contains_palm_oil'] += 1
if product.contains_chemicals:
summary['contains_chemicals'] += 1
return summary