partsdb

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

commit edf4d634a1c640bc9a2bdb35b1f50ebbf89712ba
parent 98dd9b9b95509e5dc24c5a8fa980037901cc339b
Author: Paco Esteban <paco@e1e0.net>
Date:   Sat, 13 Mar 2021 12:31:37 +0100

format part listings

Diffstat:
Mdatabase.py | 5+++--
Ahelpers.py | 19+++++++++++++++++++
MmyChips.py | 9+++++----
3 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/database.py b/database.py @@ -6,8 +6,8 @@ import sqlite3 from sqlite3 import Error -GET_CATEGORIES_QUERY = "SELECT * FROM categories" -GET_STORAGES_QUERY = "SELECT * FROM storages" +GET_CATEGORIES_QUERY = "SELECT id, name FROM categories" +GET_STORAGES_QUERY = "SELECT id, name FROM storages" LIST_PARTS_QUERY = "SELECT * FROM parts" LIST_PARTS_CATEGORY_QUERY = """ SELECT parts.id, parts.pn @@ -50,6 +50,7 @@ class PartsDB(): def __init__(self): try: self.conn = sqlite3.connect('parts.db') + self.conn.row_factory = sqlite3.Row except Error as e: print(e) diff --git a/helpers.py b/helpers.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 + +def print_parts_list(parts): + print(f"{'ID':5} | {'PN':10} | " + f"{'Manufacturer':20} | " + f"{'Description':50} | " + f"{'Footp':6} | " + f"{'Qty':4} |" + ) + print("-"*112) + 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} |" + ) diff --git a/myChips.py b/myChips.py @@ -8,6 +8,7 @@ import sys import urllib.request from database import PartsDB +from helpers import * from octopart import OctopartClient octo = OctopartClient(os.getenv('OCTOPART_TOKEN', None)) @@ -98,9 +99,9 @@ def list_parts(category): if not parts: print("There are no parts in this category") + return - for p in parts: - print(f"{p[0]}) {p[1]}") + print_parts_list(parts) def search_part(search_term): @@ -108,9 +109,9 @@ def search_part(search_term): if not parts: print("No parts found") + return - for p in parts: - print(f"{p[0]}) {p[1]}") + print_parts_list(parts) def get_part(part_id):