Preparing git repo for final project
This commit is contained in:
39
problems/pset6/pizza/pizza.py
Normal file
39
problems/pset6/pizza/pizza.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import csv
|
||||
import os
|
||||
import sys
|
||||
|
||||
import tabulate
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
file_name = sys.argv[1]
|
||||
except IndexError:
|
||||
print("Too few command-line arguments")
|
||||
sys.exit(1)
|
||||
if len(sys.argv) > 2:
|
||||
print("Too many command-line arguments")
|
||||
sys.exit(1)
|
||||
if not os.path.isfile(file_name): # Or try opening and catch FileNotFoundError
|
||||
print(f"File does not exist")
|
||||
sys.exit(1)
|
||||
if not file_name.endswith('.csv'):
|
||||
print(f"Not a CSV file")
|
||||
sys.exit(1)
|
||||
grid = get_grid_from_csv(file_name)
|
||||
print(grid)
|
||||
|
||||
|
||||
def get_grid_from_csv(file_name: str) -> str:
|
||||
headers = None
|
||||
table = []
|
||||
with open(file_name, 'r') as file_object:
|
||||
reader = csv.DictReader(file_object)
|
||||
for row in reader:
|
||||
if headers is None:
|
||||
headers = list(row.keys())
|
||||
table.append(tuple(row[field] for field in row))
|
||||
return tabulate.tabulate(table, headers, tablefmt="grid")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
6
problems/pset6/pizza/regular.csv
Normal file
6
problems/pset6/pizza/regular.csv
Normal file
@@ -0,0 +1,6 @@
|
||||
Regular Pizza,Small,Large
|
||||
Cheese,$13.50,$18.95
|
||||
1 topping,$14.75,$20.95
|
||||
2 toppings,$15.95,$22.95
|
||||
3 toppings,$16.95,$24.95
|
||||
Special,$18.50,$26.95
|
|
Reference in New Issue
Block a user