Preparing git repo for final project
33
problems/pset6/lines/lines.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
file_name = sys.argv[1]
|
||||
except IndexError:
|
||||
print("Provide a file name from command line!")
|
||||
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"The file `{file_name}` does not exist!")
|
||||
sys.exit(1)
|
||||
if not file_name.endswith('.py'):
|
||||
print(f"The file `{file_name}` is not a python script (does not end with the proper suffix)!")
|
||||
sys.exit(1)
|
||||
line_count = count_lines(file_name)
|
||||
print(line_count)
|
||||
|
||||
|
||||
def count_lines(file_name: str) -> int:
|
||||
line_count = 0
|
||||
with open(file_name, 'r') as file_object:
|
||||
for line in file_object:
|
||||
if len(line.strip()) > 0 and not line.strip().startswith('#'):
|
||||
line_count += 1
|
||||
return line_count
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
13
problems/pset6/lines/test_lines.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from lines import count_lines
|
||||
|
||||
|
||||
def main():
|
||||
test_count_lines()
|
||||
|
||||
|
||||
def test_count_lines():
|
||||
assert count_lines('lines.py') == 28
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
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
@@ -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
|
|
54
problems/pset6/scourgify/after.csv
Normal file
@@ -0,0 +1,54 @@
|
||||
first,last,house
|
||||
Hannah,Abbott,Hufflepuff
|
||||
Katie,Bell,Gryffindor
|
||||
Susan,Bones,Hufflepuff
|
||||
Terry,Boot,Ravenclaw
|
||||
Lavender,Brown,Gryffindor
|
||||
Millicent,Bulstrode,Slytherin
|
||||
Cho,Chang,Ravenclaw
|
||||
Penelope,Clearwater,Ravenclaw
|
||||
Vincent,Crabbe,Slytherin
|
||||
Colin,Creevey,Gryffindor
|
||||
Dennis,Creevey,Gryffindor
|
||||
Cedric,Diggory,Hufflepuff
|
||||
Marietta,Edgecombe,Ravenclaw
|
||||
Justin,Finch-Fletchley,Hufflepuff
|
||||
Seamus,Finnigan,Gryffindor
|
||||
Anthony,Goldstein,Ravenclaw
|
||||
Gregory,Goyle,Slytherin
|
||||
Hermione,Granger,Gryffindor
|
||||
Angelina,Johnson,Gryffindor
|
||||
Lee,Jordan,Gryffindor
|
||||
Neville,Longbottom,Gryffindor
|
||||
Luna,Lovegood,Ravenclaw
|
||||
Remus,Lupin,Gryffindor
|
||||
Draco,Malfoy,Slytherin
|
||||
Scorpius,Malfoy,Slytherin
|
||||
Ernie,Macmillan,Hufflepuff
|
||||
Minerva,McGonagall,Gryffindor
|
||||
Eloise,Midgen,Gryffindor
|
||||
Cormac,McLaggen,Gryffindor
|
||||
Graham,Montague,Slytherin
|
||||
Theodore,Nott,Slytherin
|
||||
Pansy,Parkinson,Slytherin
|
||||
Padma,Patil,Gryffindor
|
||||
Parvati,Patil,Gryffindor
|
||||
Harry,Potter,Gryffindor
|
||||
Tom,Riddle,Slytherin
|
||||
Demelza,Robins,Gryffindor
|
||||
Newt,Scamander,Hufflepuff
|
||||
Horace,Slughorn,Slytherin
|
||||
Zacharias,Smith,Hufflepuff
|
||||
Severus,Snape,Slytherin
|
||||
Alicia,Spinnet,Gryffindor
|
||||
Pomona,Sprout,Hufflepuff
|
||||
Dean,Thomas,Gryffindor
|
||||
Romilda,Vane,Gryffindor
|
||||
Myrtle,Warren,Ravenclaw
|
||||
Fred,Weasley,Gryffindor
|
||||
George,Weasley,Gryffindor
|
||||
Ginny,Weasley,Gryffindor
|
||||
Percy,Weasley,Gryffindor
|
||||
Ron,Weasley,Gryffindor
|
||||
Oliver,Wood,Gryffindor
|
||||
Blaise,Zabini,Slytherin
|
|
54
problems/pset6/scourgify/before.csv
Normal file
@@ -0,0 +1,54 @@
|
||||
name,house
|
||||
"Abbott, Hannah",Hufflepuff
|
||||
"Bell, Katie",Gryffindor
|
||||
"Bones, Susan",Hufflepuff
|
||||
"Boot, Terry",Ravenclaw
|
||||
"Brown, Lavender",Gryffindor
|
||||
"Bulstrode, Millicent",Slytherin
|
||||
"Chang, Cho",Ravenclaw
|
||||
"Clearwater, Penelope",Ravenclaw
|
||||
"Crabbe, Vincent",Slytherin
|
||||
"Creevey, Colin",Gryffindor
|
||||
"Creevey, Dennis",Gryffindor
|
||||
"Diggory, Cedric",Hufflepuff
|
||||
"Edgecombe, Marietta",Ravenclaw
|
||||
"Finch-Fletchley, Justin",Hufflepuff
|
||||
"Finnigan, Seamus",Gryffindor
|
||||
"Goldstein, Anthony",Ravenclaw
|
||||
"Goyle, Gregory",Slytherin
|
||||
"Granger, Hermione",Gryffindor
|
||||
"Johnson, Angelina",Gryffindor
|
||||
"Jordan, Lee",Gryffindor
|
||||
"Longbottom, Neville",Gryffindor
|
||||
"Lovegood, Luna",Ravenclaw
|
||||
"Lupin, Remus",Gryffindor
|
||||
"Malfoy, Draco",Slytherin
|
||||
"Malfoy, Scorpius",Slytherin
|
||||
"Macmillan, Ernie",Hufflepuff
|
||||
"McGonagall, Minerva",Gryffindor
|
||||
"Midgen, Eloise",Gryffindor
|
||||
"McLaggen, Cormac",Gryffindor
|
||||
"Montague, Graham",Slytherin
|
||||
"Nott, Theodore",Slytherin
|
||||
"Parkinson, Pansy",Slytherin
|
||||
"Patil, Padma",Gryffindor
|
||||
"Patil, Parvati",Gryffindor
|
||||
"Potter, Harry",Gryffindor
|
||||
"Riddle, Tom",Slytherin
|
||||
"Robins, Demelza",Gryffindor
|
||||
"Scamander, Newt",Hufflepuff
|
||||
"Slughorn, Horace",Slytherin
|
||||
"Smith, Zacharias",Hufflepuff
|
||||
"Snape, Severus",Slytherin
|
||||
"Spinnet, Alicia",Gryffindor
|
||||
"Sprout, Pomona",Hufflepuff
|
||||
"Thomas, Dean",Gryffindor
|
||||
"Vane, Romilda",Gryffindor
|
||||
"Warren, Myrtle",Ravenclaw
|
||||
"Weasley, Fred",Gryffindor
|
||||
"Weasley, George",Gryffindor
|
||||
"Weasley, Ginny",Gryffindor
|
||||
"Weasley, Percy",Gryffindor
|
||||
"Weasley, Ron",Gryffindor
|
||||
"Wood, Oliver",Gryffindor
|
||||
"Zabini, Blaise",Slytherin
|
|
7
problems/pset6/scourgify/in.csv
Normal file
@@ -0,0 +1,7 @@
|
||||
name,house
|
||||
"Abbott, Hannah",Hufflepuff
|
||||
"Bell, Katie",Gryffindor
|
||||
"Bones, Susan",Hufflepuff
|
||||
"Boot, Terry",Ravenclaw
|
||||
"Brown, Lavender",Gryffindor
|
||||
"Bulstrode, Millicent",Slytherin
|
|
5
problems/pset6/scourgify/out.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
first,last,house
|
||||
Susan,Bones,Hufflepuff
|
||||
Terry,Boot,Ravenclaw
|
||||
Lavender,Brown,Gryffindor
|
||||
Millicent,Bulstrode,Slytherin
|
|
42
problems/pset6/scourgify/scourgify.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import csv
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
input_file_name, output_file_name = sys.argv[1:3]
|
||||
except IndexError:
|
||||
print("Too few command-line arguments")
|
||||
sys.exit(1)
|
||||
if len(sys.argv) > 3:
|
||||
print("Too many command-line arguments")
|
||||
sys.exit(1)
|
||||
if not os.path.isfile(input_file_name): # Or try opening and catch FileNotFoundError
|
||||
print(f"Could not read {input_file_name}")
|
||||
sys.exit(1)
|
||||
if not input_file_name.endswith('.csv'):
|
||||
print(f"Not a CSV file")
|
||||
sys.exit(1)
|
||||
write_names_to_csv_file(read_names_from_csv_file(input_file_name), output_file_name)
|
||||
|
||||
|
||||
def read_names_from_csv_file(input_file_name):
|
||||
with open(input_file_name, 'r') as input_file:
|
||||
reader = csv.DictReader(input_file)
|
||||
for row in reader:
|
||||
yield row
|
||||
|
||||
|
||||
def write_names_to_csv_file(input_data, output_file_name):
|
||||
with open(output_file_name, 'w') as output_file:
|
||||
writer = csv.DictWriter(output_file, fieldnames=['first', 'last', 'house'])
|
||||
writer.writeheader()
|
||||
for row in input_data:
|
||||
row['last'], row['first'] = row['name'].split(', ')
|
||||
del row['name']
|
||||
writer.writerow(row)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
BIN
problems/pset6/shirt/after1.jpg
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
problems/pset6/shirt/after2.jpg
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
problems/pset6/shirt/after3.jpg
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
problems/pset6/shirt/before1.jpg
Normal file
After Width: | Height: | Size: 980 KiB |
BIN
problems/pset6/shirt/before2.jpg
Normal file
After Width: | Height: | Size: 1.2 MiB |
BIN
problems/pset6/shirt/before3.jpg
Normal file
After Width: | Height: | Size: 1.2 MiB |
BIN
problems/pset6/shirt/shirt.png
Normal file
After Width: | Height: | Size: 64 KiB |
43
problems/pset6/shirt/shirt.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
import PIL
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
input_file_name, output_file_name = sys.argv[1:3]
|
||||
except (IndexError, ValueError):
|
||||
print("Too few command-line arguments")
|
||||
sys.exit(1)
|
||||
if len(sys.argv) > 3:
|
||||
print("Too many command-line arguments")
|
||||
sys.exit(1)
|
||||
if not os.path.isfile(input_file_name): # Or try opening and catch FileNotFoundError
|
||||
print(f"Input does not exist")
|
||||
sys.exit(1)
|
||||
if not (any(input_file_name.lower().endswith(format) for format in (".jpg", ".jpeg", ".png")) and any(output_file_name.lower().endswith(format) for format in (".jpg", ".jpeg", ".png"))):
|
||||
print(f"Invalid input")
|
||||
sys.exit(1)
|
||||
if (input_file_name[-4] == '.' and input_file_name[-4:] != output_file_name[-4:]) or (input_file_name[-3] == '.' and input_file_name[-3:] != output_file_name[-3:]):
|
||||
print(f"Input and output have different extensions")
|
||||
sys.exit(1)
|
||||
overlap_t_shirt(input_file_name, output_file_name)
|
||||
"""Open the input with Image.open, per pillow.
|
||||
resize and crop the input with ImageOps.fit, per pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.fit,
|
||||
using default values for method, bleed, and centering, overlay the shirt with Image.paste,
|
||||
per pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.paste,
|
||||
and save the result with Image.save, per pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.save."""
|
||||
|
||||
|
||||
def overlap_t_shirt(input_file_name, output_file_name):
|
||||
image = Image.open(input_file_name)
|
||||
shirt = Image.open('shirt.png')
|
||||
image = PIL.ImageOps.fit(image, size=shirt.size)
|
||||
image.paste(shirt, mask=shirt)
|
||||
image.save(output_file_name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|