Command-line tool designed to interact with an inventory implemented as a SQL database using sqlite3. This tool enables users to add and remove specific quantities of items from the database. Additionally, the tool allows for searching for specific items and printing the entire database as a table to the console. Different flags are available to access the various functionalities.
.gitignore
contains files to exclude from version controldb.py
contains functions to connect to the database and load its content as a dict objecthelpers.py
defines helper functions enabling to get user inputproject.py
containsmain()
as well asadd_item()
,remove_item()
andfind_item()
README.md
contains the tool description that you're currently readingrequirements.txt
lists the required librariesschema.sql
define the structure and organization of the databasetest_project.py
contains unit tests
python3 project.py [-h] [-i] [-a] [-r] [-f] [-v]
Caution
You need to initialize the database before using the tool. Initialization generates an inv.db
file in your working directory.
To exit the program, use CTRL+D.
options:
-h, --help show this help message and exit
-i, -init Initialize the database
-a, -add Add item(s) to the inventory
-r, -remove Remove item(s) from the inventory
-f, -find Find item(s) in the inventory
-v, -view View item(s) in the inventory
Deletes existing tables and recreates the database using the SQL commands in schema.sql
:
DROP TABLE IF EXISTS inv;
CREATE TABLE inv (
id INTEGER PRIMARY KEY AUTOINCREMENT,
item TEXT NOT NULL UNIQUE,
quantity INTEGER NOT NULL
);
Note
Before initialization, you will have to confirm your choice:
Are you sure you want to initialize the database? (y/n)
Adds an item to the database.
- Enter item to insert:
> python3 project.py -a
Item to add: coffee 150g
- Enter quantity to insert:
> python3 project.py -a
Item to add: coffee 150g
Quantity: 5
Warning
The quantity must be an integer. Thus, you cannot remove a fraction of an item.
- A confirmation message appears and you're prompted for the next item to add:
> python3 project.py -a
Item to add: coffee 150g
Quantity: 5
Added 5 COFFEE 150G to the inventory!
Item to add:
Removes item from the database.
- Enter the item to remove from the database:
> python3 project.py -r
Item to remove: coffee 150g
- Enter the quantity to remove from the database:
> python3 project.py -r
Item to remove: coffee 150g
Quantity: 2
- A confirmation message appears and you're prompted for the next item to remove:
> python3 project.py -r
Item to remove: coffee 150g
Quantity: 5
Removed 2 COFFEE 150G to the inventory!
Item to remove:
Warning
- You cannot remove more items than you own:
python3 project.py -r
Item to remove: coffee 150g
Quantity: 1000
Trying to remove more items than you own!
Item to remove:
- You must spell the item to remove exactly as it is saved inside the database. This can definitely be improved in the future.
- If you remove the entire quantity of a specific item (i.e the final quantity owned is 0), then the item is removed completely from the database.
Finds items in the database based on regular expression pattern matching.
- Enter the item you want to find:
> python3 project.py -f
Search item: coffee
Note
Note that you don't have to name items exactly as they're stored inside the database. Here, I replaced "coffee 150g" by "coffee", but the searching will still work. This is because I use the following regexp to look for items:
pattern = re.compile(f"^.*{item}.*$")
In case of a match, a table is printed to the console and you're prompted for the next item to search for.
> python3 project.py -f
Search item: coffee
+--------+------------+
| Item | Quantity |
+========+============+
| COFFEE | 3 |
+--------+------------+
Search item:
If there's no match, a message is printed.
> python3 project.py -r
Search item: banana
BANANA not in inventory
Displays the entire database as a table in the console.
> python3 project.py -v
+----------------------+------------+
| Item | Quantity |
+======================+============+
| CHEDDAR | 1 |
+----------------------+------------+
| COFFEE | 3 |
+----------------------+------------+
| CRANBERRY JUICE 1.5L | 1 |
+----------------------+------------+
| MANGO | 5 |
+----------------------+------------+
| MILK 1L | 2 |
+----------------------+------------+