Spaces:
No application file
No application file
Commit
Β·
1bae378
1
Parent(s):
63c91ee
add data analytics (top 3 vehicle makes)
Browse files- cli_demo.py +75 -16
cli_demo.py
CHANGED
|
@@ -6,13 +6,17 @@ Date: August 24, 2025
|
|
| 6 |
|
| 7 |
A simple command-line application for a car dealership that allows users
|
| 8 |
to look up vehicle information (year, make, and model) by VIN number.
|
|
|
|
| 9 |
The data is retrieved from the NHTSA (National Highway Traffic Safety
|
| 10 |
Administration) VIN decoding API via the helper module `nhtsa_api_call.py`.
|
| 11 |
|
| 12 |
Usage:
|
| 13 |
python cli_demo.py
|
| 14 |
"""
|
| 15 |
-
import nhtsa_api_call, re
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def is_valid_vin (vin):
|
| 18 |
"""
|
|
@@ -27,6 +31,44 @@ def is_valid_vin (vin):
|
|
| 27 |
return False
|
| 28 |
return True
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def main():
|
| 31 |
"""
|
| 32 |
Run the command-line VIN lookup loop.
|
|
@@ -38,25 +80,42 @@ def main():
|
|
| 38 |
print("π Welcome to the Car Dealership VIN Lookup!\n")
|
| 39 |
|
| 40 |
while True:
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# exit condition gets checked first
|
| 43 |
-
if
|
| 44 |
print("π You chose to exit VIN lookup. Goodbye!")
|
| 45 |
break
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
print(f" Make: {make}")
|
| 57 |
-
print(f" Model: {model}")
|
| 58 |
else:
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
main()
|
|
|
|
| 6 |
|
| 7 |
A simple command-line application for a car dealership that allows users
|
| 8 |
to look up vehicle information (year, make, and model) by VIN number.
|
| 9 |
+
The app also lets a user view top 3 looked up vehicle makes.
|
| 10 |
The data is retrieved from the NHTSA (National Highway Traffic Safety
|
| 11 |
Administration) VIN decoding API via the helper module `nhtsa_api_call.py`.
|
| 12 |
|
| 13 |
Usage:
|
| 14 |
python cli_demo.py
|
| 15 |
"""
|
| 16 |
+
import nhtsa_api_call, re, csv, os
|
| 17 |
+
import pandas as pd
|
| 18 |
+
|
| 19 |
+
TOP3 = 3
|
| 20 |
|
| 21 |
def is_valid_vin (vin):
|
| 22 |
"""
|
|
|
|
| 31 |
return False
|
| 32 |
return True
|
| 33 |
|
| 34 |
+
def save_vehicle(year, make, model, filename = "vin_lookup_history.csv"):
|
| 35 |
+
"""Append VIN lookup result to a CSV file."""
|
| 36 |
+
file_exists = os.path.isfile(filename)
|
| 37 |
+
with open(filename, "a", newline="") as f:
|
| 38 |
+
writer = csv.writer(f)
|
| 39 |
+
if not file_exists:
|
| 40 |
+
# add first row
|
| 41 |
+
writer.writerow(["year", "make", "model"])
|
| 42 |
+
writer.writerow([year, make, model])
|
| 43 |
+
|
| 44 |
+
def display_top3(filename = "vin_lookup_history.csv"):
|
| 45 |
+
"""Show top 3 car makes by lookup frequency"""
|
| 46 |
+
# if file does not exist then there is no history yet
|
| 47 |
+
if not os.path.isfile(filename):
|
| 48 |
+
print("\nNo VIN lookup history found. Please look up some VINs first.\n")
|
| 49 |
+
return
|
| 50 |
+
# create a dataframe using file data
|
| 51 |
+
df = pd.read_csv(filename)
|
| 52 |
+
# save number each make appears in the log as a series
|
| 53 |
+
counts = df['make'].value_counts()
|
| 54 |
+
# file exists but no data has been added yet
|
| 55 |
+
if len(counts) == 0:
|
| 56 |
+
print("\n No data in the log yet.\n")
|
| 57 |
+
return
|
| 58 |
+
# if there are fewer than 3 data entries in the log then the program will
|
| 59 |
+
# at least display 1 or 2 most looked up makes
|
| 60 |
+
if len(counts) < TOP3:
|
| 61 |
+
print(f"\nNot enough data for displaying top 3. Showing top {len(counts)} instead:\n")
|
| 62 |
+
top_makes = counts.head(len(counts))
|
| 63 |
+
else:
|
| 64 |
+
print(f"\n Top {TOP3} Car Makes from Lookups:\n")
|
| 65 |
+
top_makes = counts.head(TOP3)
|
| 66 |
+
|
| 67 |
+
for make, count in top_makes.items():
|
| 68 |
+
print(f" {make}: {count} lookups")
|
| 69 |
+
# an empty line print to space out CLI prompts
|
| 70 |
+
print()
|
| 71 |
+
|
| 72 |
def main():
|
| 73 |
"""
|
| 74 |
Run the command-line VIN lookup loop.
|
|
|
|
| 80 |
print("π Welcome to the Car Dealership VIN Lookup!\n")
|
| 81 |
|
| 82 |
while True:
|
| 83 |
+
print("Choose an option from the menu:")
|
| 84 |
+
print("1. Lookup a VIN")
|
| 85 |
+
print("2. Show Top 3 Most Looked-up Makes")
|
| 86 |
+
print("0. Exit")
|
| 87 |
+
choice = input("Enter your choice: ").strip()
|
| 88 |
+
|
| 89 |
# exit condition gets checked first
|
| 90 |
+
if choice == "0":
|
| 91 |
print("π You chose to exit VIN lookup. Goodbye!")
|
| 92 |
break
|
| 93 |
+
# user selected to add a VIN
|
| 94 |
+
elif choice == "1":
|
| 95 |
+
vin = input("Please enter a valid VIN or 0 (zero) to exit: \n").strip()
|
| 96 |
+
if vin == "0":
|
| 97 |
+
continue
|
| 98 |
+
# check if vin is valid, and if not print error message
|
| 99 |
+
if not is_valid_vin(vin):
|
| 100 |
+
print("Invalid VIN format. Must be 17 letters/numbers (no I, O, Q). Try again.\n")
|
| 101 |
+
continue
|
| 102 |
+
# vin is valid, perform VIN lookup
|
|
|
|
|
|
|
| 103 |
else:
|
| 104 |
+
year, make, model = nhtsa_api_call.get_vehicle_info(vin)
|
| 105 |
+
if year and make and model:
|
| 106 |
+
print(f"β
VIN {vin}")
|
| 107 |
+
print(f" Year: {year}")
|
| 108 |
+
print(f" Make: {make}")
|
| 109 |
+
print(f" Model: {model}")
|
| 110 |
+
save_vehicle(year, make, model) #save the data in the lookup history file
|
| 111 |
+
else:
|
| 112 |
+
print(f"β VIN {vin} not found in database. Try again.")
|
| 113 |
+
# user selected to view top3 vehicle makes
|
| 114 |
+
elif choice == "2":
|
| 115 |
+
display_top3()
|
| 116 |
+
# user entered something other than 1,2 or 0
|
| 117 |
+
else:
|
| 118 |
+
print("Invalid option. Please choose 1, 2, or 0.\n")
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
main()
|