partsdb

electronic parts inventory
git clone https://git.e1e0.net/partsdb.git
Log | Files | Refs

commit 96b0ec846b9b911c01d3ecb6bb79b03d0a7e2797
parent fe79e4d648678496118516a473b985b974e22fd9
Author: Paco Esteban <paco@e1e0.net>
Date:   Sun, 14 Mar 2021 10:06:16 +0100

make part listing dynamic with

Diffstat:
Mdatabase.py | 9++++++++-
Mhelpers.py | 70++++++++++++++++++++++++++++++++++++++++++++++------------------------
2 files changed, 54 insertions(+), 25 deletions(-)

diff --git a/database.py b/database.py @@ -8,7 +8,14 @@ from sqlite3 import Error GET_CATEGORIES_QUERY = "SELECT id, name FROM categories" GET_STORAGES_QUERY = "SELECT id, name FROM storages" -LIST_PARTS_QUERY = "SELECT * FROM parts" +LIST_PARTS_QUERY = """ + SELECT p.id, p.pn, p.manufacturer, p.description, + p.footprint, p.quantity, p.part_type, c.name as cname + FROM parts as p, categories as c + INNER JOIN categories + ON p.category_id = c.id + GROUP BY p.id +""" LIST_PARTS_CATEGORY_QUERY = """ SELECT parts.id, parts.pn FROM parts diff --git a/helpers.py b/helpers.py @@ -4,30 +4,52 @@ def print_parts_list(parts, short=False): if short: - print(f"{'ID':5} | {'PN':10} | " - f"{'Manufacturer':20} | " - f"{'Footp':6} | " - f"{'Qty':4} |" - ) - print("-"*59) + print(f"{'ID':4} | " + f"{'Category':8} | " + f"{'PN':10} | " + f"{'Manufacturer':18} | " + f"{'Description':25} |" + ) + print("-"*79) for p in parts: - print(f"{p['id']:<5} | {p['pn'][0:9]:10} | " - f"{p['manufacturer'][0:19]:20} | " - f"{p['footprint'][0:5]:6} | " - f"{p['quantity']:4} |" - ) + print(f"{p['id']:<4} | " + f"{p['cname'][0:7]:8} | " + f"{p['pn'][0:9]:10} | " + f"{p['manufacturer'][0:17]:18} | " + f"{p['description'][0:24]:25} |" + ) else: - print(f"{'ID':5} | {'PN':10} | " - f"{'Manufacturer':20} | " - f"{'Description':50} | " - f"{'Footp':6} | " - f"{'Qty':4} |" - ) - print("-"*112) + # max takes an iterable and key is a function where iterables are + # passed and comparison performed, so we look at the longest desc to + # get the lenght of the field. We also check if the header is longer, + # the result is whatever is longer + l_pn = len(max(parts, key=lambda k: len(k['pn']))['pn']) + l_cat = len(max(parts, + key=lambda k: len(k['cname']))['cname']) + l_man = len(max(parts, + key=lambda k: len(k['manufacturer']))['manufacturer']) + l_desc = len(max(parts, + key=lambda k: len(k['description']))['description']) + l_cat = l_cat if l_cat > len('Category') else len('Category') + l_man = l_man if l_man > len('Manufacturer') else len('Manufacturer') + l_desc = l_desc if l_desc > len('Description') else len('Description') + + header = (f"{'ID':5} | {'PN':{l_pn}} | " + f"{'Category':{l_cat}} | " + f"{'Manufacturer':{l_man}} | " + f"{'Description':{l_desc}} | " + f"{'Type':4} | " + f"{'Footp':6} | " + f"{'Qty':4} |" + ) + print(header) + print("-"*len(header)) for p in parts: - print(f"{p['id']:<5} | {p['pn'][0:9]:10} | " - f"{p['manufacturer'][0:19]:20} | " - f"{p['description'][0:49]:50} | " - f"{p['footprint'][0:5]:6} | " - f"{p['quantity']:4} |" - ) + print(f"{p['id']:<5} | {p['pn']:{l_pn}} | " + f"{p['cname']:{l_cat}} | " + f"{p['manufacturer']:{l_man}} | " + f"{p['description']:{l_desc}} | " + f"{p['part_type'][0:3]:4} | " + f"{p['footprint'][0:5]:6} | " + f"{p['quantity']:4} |" + )