From eb75ae5fde29b859691bd3136befd870b0259109 Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Thu, 23 May 2019 12:10:21 +0530 Subject: [PATCH 01/12] [PS] Update code to Python 3 The current code only works for Python 2. Updated code should be compatible with Python 3. --- Lesson_1/lotsofmenus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lesson_1/lotsofmenus.py b/Lesson_1/lotsofmenus.py index b86d401..e4c78f0 100644 --- a/Lesson_1/lotsofmenus.py +++ b/Lesson_1/lotsofmenus.py @@ -377,4 +377,4 @@ session.commit() -print "added menu items!" +print("added menu items!") From 48a845f28c4a1c634c6df79a4a9bef9b4e4a9e22 Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Thu, 23 May 2019 12:12:39 +0530 Subject: [PATCH 02/12] [PS] Update code to Python3 --- Lesson-2/first-web-server/webserver.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Lesson-2/first-web-server/webserver.py b/Lesson-2/first-web-server/webserver.py index f870055..ba999cd 100644 --- a/Lesson-2/first-web-server/webserver.py +++ b/Lesson-2/first-web-server/webserver.py @@ -1,4 +1,9 @@ -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +## Run in terminal once +## python -m http.server --cgi 8000 ## The --cgi turns the python handler on. + + + +from http.server import BaseHTTPRequestHandler, HTTPServer class WebServerHandler(BaseHTTPRequestHandler): @@ -10,8 +15,8 @@ def do_GET(self): self.end_headers() message = "" message += "Hello!" - self.wfile.write(message) - print message + self.wfile.write(bytes(message, "utf-8")) + print(message) return else: self.send_error(404, 'File Not Found: %s' % self.path) @@ -21,11 +26,11 @@ def main(): try: port = 8080 server = HTTPServer(('', port), WebServerHandler) - print "Web Server running on port %s" % port + print("Web Server running on port %s" % port) server.serve_forever() except KeyboardInterrupt: - print " ^C entered, stopping web server...." + print(" ^C entered, stopping web server....") server.socket.close() if __name__ == '__main__': - main() + main() \ No newline at end of file From f6dc360738b7fb8a398f1f86e62506bda6a4b68d Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Thu, 23 May 2019 12:13:27 +0530 Subject: [PATCH 03/12] [PS] Update code to Python 3 --- Lesson-2/hola-server/webserver.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lesson-2/hola-server/webserver.py b/Lesson-2/hola-server/webserver.py index cff2303..7166ae7 100644 --- a/Lesson-2/hola-server/webserver.py +++ b/Lesson-2/hola-server/webserver.py @@ -1,4 +1,4 @@ -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from http.server import BaseHTTPRequestHandler, HTTPServer class webServerHandler(BaseHTTPRequestHandler): @@ -10,8 +10,8 @@ def do_GET(self): self.end_headers() message = "" message += "Hello!" - self.wfile.write(message) - print message + self.wfile.write(bytes(message, "utf-8")) + print(message) return if self.path.endswith("/hola"): @@ -19,9 +19,9 @@ def do_GET(self): self.send_header('Content-type', 'text/html') self.end_headers() message = "" - message += " ¡ Hola ! " - self.wfile.write(message) - print message + message += " ¡ Hola ! Back to Hello " + self.wfile.write(bytes(message, "utf-8")) + print(message) return else: @@ -32,10 +32,10 @@ def main(): try: port = 8080 server = HTTPServer(('', port), webServerHandler) - print "Web Server running on port %s" % port + print("Web Server running on port %s" % port) server.serve_forever() except KeyboardInterrupt: - print " ^C entered, stopping web server...." + print(" ^C entered, stopping web server....") server.socket.close() if __name__ == '__main__': From 5fc19d1c10fd2a310a5a47562261e167db646f8a Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Thu, 23 May 2019 18:48:16 +0530 Subject: [PATCH 04/12] [PS] Make code Python 3 compatible --- .../Objective-1-Solution/database_setup.py | 33 ++ Lesson-2/Objective-1-Solution/lotsofmenus.py | 380 ++++++++++++++++++ Lesson-2/Objective-1-Solution/webserver.py | 8 +- 3 files changed, 417 insertions(+), 4 deletions(-) create mode 100644 Lesson-2/Objective-1-Solution/database_setup.py create mode 100644 Lesson-2/Objective-1-Solution/lotsofmenus.py diff --git a/Lesson-2/Objective-1-Solution/database_setup.py b/Lesson-2/Objective-1-Solution/database_setup.py new file mode 100644 index 0000000..5016567 --- /dev/null +++ b/Lesson-2/Objective-1-Solution/database_setup.py @@ -0,0 +1,33 @@ +import os +import sys +from sqlalchemy import Column, ForeignKey, Integer, String +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import relationship +from sqlalchemy import create_engine + +Base = declarative_base() + + +class Restaurant(Base): + __tablename__ = 'restaurant' + + id = Column(Integer, primary_key=True) + name = Column(String(250), nullable=False) + + +class MenuItem(Base): + __tablename__ = 'menu_item' + + name = Column(String(80), nullable=False) + id = Column(Integer, primary_key=True) + description = Column(String(250)) + price = Column(String(8)) + course = Column(String(250)) + restaurant_id = Column(Integer, ForeignKey('restaurant.id')) + restaurant = relationship(Restaurant) + + +engine = create_engine('sqlite:///restaurantmenu.db') + + +Base.metadata.create_all(engine) diff --git a/Lesson-2/Objective-1-Solution/lotsofmenus.py b/Lesson-2/Objective-1-Solution/lotsofmenus.py new file mode 100644 index 0000000..e4c78f0 --- /dev/null +++ b/Lesson-2/Objective-1-Solution/lotsofmenus.py @@ -0,0 +1,380 @@ +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +from database_setup import Restaurant, Base, MenuItem + +engine = create_engine('sqlite:///restaurantmenu.db') +# Bind the engine to the metadata of the Base class so that the +# declaratives can be accessed through a DBSession instance +Base.metadata.bind = engine + +DBSession = sessionmaker(bind=engine) +# A DBSession() instance establishes all conversations with the database +# and represents a "staging zone" for all the objects loaded into the +# database session object. Any change made against the objects in the +# session won't be persisted into the database until you call +# session.commit(). If you're not happy about the changes, you can +# revert all of them back to the last commit by calling +# session.rollback() +session = DBSession() + + +# Menu for UrbanBurger +restaurant1 = Restaurant(name="Urban Burger") + +session.add(restaurant1) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$7.50", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +menuItem1 = MenuItem(name="French Fries", description="with garlic and parmesan", + price="$2.99", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Chicken Burger", description="Juicy grilled chicken patty with tomato mayo and lettuce", + price="$5.50", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Chocolate Cake", description="fresh baked and served with ice cream", + price="$3.99", course="Dessert", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Sirloin Burger", description="Made with grade A beef", + price="$7.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem5 = MenuItem(name="Root Beer", description="16oz of refreshing goodness", + price="$1.99", course="Beverage", restaurant=restaurant1) + +session.add(menuItem5) +session.commit() + +menuItem6 = MenuItem(name="Iced Tea", description="with Lemon", + price="$.99", course="Beverage", restaurant=restaurant1) + +session.add(menuItem6) +session.commit() + +menuItem7 = MenuItem(name="Grilled Cheese Sandwich", description="On texas toast with American Cheese", + price="$3.49", course="Entree", restaurant=restaurant1) + +session.add(menuItem7) +session.commit() + +menuItem8 = MenuItem(name="Veggie Burger", description="Made with freshest of ingredients and home grown spices", + price="$5.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem8) +session.commit() + + +# Menu for Super Stir Fry +restaurant2 = Restaurant(name="Super Stir Fry") + +session.add(restaurant2) +session.commit() + + +menuItem1 = MenuItem(name="Chicken Stir Fry", description="With your choice of noodles vegetables and sauces", + price="$7.99", course="Entree", restaurant=restaurant2) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem( + name="Peking Duck", description=" A famous duck dish from Beijing[1] that has been prepared since the imperial era. The meat is prized for its thin, crisp skin, with authentic versions of the dish serving mostly the skin and little meat, sliced in front of the diners by the cook", price="$25", course="Entree", restaurant=restaurant2) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Spicy Tuna Roll", description="Seared rare ahi, avocado, edamame, cucumber with wasabi soy sauce ", + price="15", course="Entree", restaurant=restaurant2) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Nepali Momo ", description="Steamed dumplings made with vegetables, spices and meat. ", + price="12", course="Entree", restaurant=restaurant2) + +session.add(menuItem4) +session.commit() + +menuItem5 = MenuItem(name="Beef Noodle Soup", description="A Chinese noodle soup made of stewed or red braised beef, beef broth, vegetables and Chinese noodles.", + price="14", course="Entree", restaurant=restaurant2) + +session.add(menuItem5) +session.commit() + +menuItem6 = MenuItem(name="Ramen", description="a Japanese noodle soup dish. It consists of Chinese-style wheat noodles served in a meat- or (occasionally) fish-based broth, often flavored with soy sauce or miso, and uses toppings such as sliced pork, dried seaweed, kamaboko, and green onions.", + price="12", course="Entree", restaurant=restaurant2) + +session.add(menuItem6) +session.commit() + + +# Menu for Panda Garden +restaurant1 = Restaurant(name="Panda Garden") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Pho", description="a Vietnamese noodle soup consisting of broth, linguine-shaped rice noodles called banh pho, a few herbs, and meat.", + price="$8.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Chinese Dumplings", description="a common Chinese dumpling which generally consists of minced meat and finely chopped vegetables wrapped into a piece of dough skin. The skin can be either thin and elastic or thicker.", + price="$6.99", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Gyoza", description="The most prominent differences between Japanese-style gyoza and Chinese-style jiaozi are the rich garlic flavor, which is less noticeable in the Chinese version, the light seasoning of Japanese gyoza with salt and soy sauce, and the fact that gyoza wrappers are much thinner", + price="$9.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Stinky Tofu", description="Taiwanese dish, deep fried fermented tofu served with pickled cabbage.", + price="$6.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$9.50", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +# Menu for Thyme for that +restaurant1 = Restaurant(name="Thyme for That Vegetarian Cuisine ") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Tres Leches Cake", description="Rich, luscious sponge cake soaked in sweet milk and topped with vanilla bean whipped cream and strawberries.", + price="$2.99", course="Dessert", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Mushroom risotto", description="Portabello mushrooms in a creamy risotto", + price="$5.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Honey Boba Shaved Snow", description="Milk snow layered with honey boba, jasmine tea jelly, grass jelly, caramel, cream, and freshly made mochi", + price="$4.50", course="Dessert", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Cauliflower Manchurian", description="Golden fried cauliflower florets in a midly spiced soya,garlic sauce cooked with fresh cilantro, celery, chilies,ginger & green onions", + price="$6.95", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem5 = MenuItem(name="Aloo Gobi Burrito", description="Vegan goodness. Burrito filled with rice, garbanzo beans, curry sauce, potatoes (aloo), fried cauliflower (gobi) and chutney. Nom Nom", + price="$7.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem5) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$6.80", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +# Menu for Tony's Bistro +restaurant1 = Restaurant(name="Tony\'s Bistro ") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Shellfish Tower", description="Lobster, shrimp, sea snails, crawfish, stacked into a delicious tower", + price="$13.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Chicken and Rice", description="Chicken... and rice", + price="$4.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Mom's Spaghetti", description="Spaghetti with some incredible tomato sauce made by mom", + price="$6.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Choc Full O\' Mint (Smitten\'s Fresh Mint Chip ice cream)", + description="Milk, cream, salt, ..., Liquid nitrogen magic", price="$3.95", course="Dessert", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem5 = MenuItem(name="Tonkatsu Ramen", description="Noodles in a delicious pork-based broth with a soft-boiled egg", + price="$7.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem5) +session.commit() + + +# Menu for Andala's +restaurant1 = Restaurant(name="Andala\'s") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Lamb Curry", description="Slow cook that thang in a pool of tomatoes, onions and alllll those tasty Indian spices. Mmmm.", + price="$9.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Chicken Marsala", description="Chicken cooked in Marsala wine sauce with mushrooms", + price="$7.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Potstickers", description="Delicious chicken and veggies encapsulated in fried dough.", + price="$6.50", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Nigiri Sampler", description="Maguro, Sake, Hamachi, Unagi, Uni, TORO!", + price="$6.75", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$7.00", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +# Menu for Auntie Ann's +restaurant1 = Restaurant(name="Auntie Ann\'s Diner ") + +session.add(restaurant1) +session.commit() + +menuItem9 = MenuItem(name="Chicken Fried Steak", description="Fresh battered sirloin steak fried and smothered with cream gravy", + price="$8.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem9) +session.commit() + + +menuItem1 = MenuItem(name="Boysenberry Sorbet", description="An unsettlingly huge amount of ripe berries turned into frozen (and seedless) awesomeness", + price="$2.99", course="Dessert", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Broiled salmon", description="Salmon fillet marinated with fresh herbs and broiled hot & fast", + price="$10.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Morels on toast (seasonal)", description="Wild morel mushrooms fried in butter, served on herbed toast slices", + price="$7.50", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Tandoori Chicken", description="Chicken marinated in yoghurt and seasoned with a spicy mix(chilli, tamarind among others) and slow cooked in a cylindrical clay or metal oven which gets its heat from burning charcoal.", + price="$8.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$9.50", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem10 = MenuItem(name="Spinach Ice Cream", description="vanilla ice cream made with organic spinach leaves", + price="$1.99", course="Dessert", restaurant=restaurant1) + +session.add(menuItem10) +session.commit() + + +# Menu for Cocina Y Amor +restaurant1 = Restaurant(name="Cocina Y Amor ") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Super Burrito Al Pastor", description="Marinated Pork, Rice, Beans, Avocado, Cilantro, Salsa, Tortilla", + price="$5.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Cachapa", description="Golden brown, corn-based Venezuelan pancake; usually stuffed with queso telita or queso de mano, and possibly lechon. ", + price="$7.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +restaurant1 = Restaurant(name="State Bird Provisions") +session.add(restaurant1) +session.commit() + +menuItem1 = MenuItem(name="Chantrelle Toast", description="Crispy Toast with Sesame Seeds slathered with buttery chantrelle mushrooms", + price="$5.95", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem1 = MenuItem(name="Guanciale Chawanmushi", description="Japanese egg custard served hot with spicey Italian Pork Jowl (guanciale)", + price="$6.95", course="Dessert", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + + +menuItem1 = MenuItem(name="Lemon Curd Ice Cream Sandwich", description="Lemon Curd Ice Cream Sandwich on a chocolate macaron with cardamom meringue and cashews", + price="$4.25", course="Dessert", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + + +print("added menu items!") diff --git a/Lesson-2/Objective-1-Solution/webserver.py b/Lesson-2/Objective-1-Solution/webserver.py index ab04726..653d777 100644 --- a/Lesson-2/Objective-1-Solution/webserver.py +++ b/Lesson-2/Objective-1-Solution/webserver.py @@ -1,4 +1,4 @@ -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from http.server import BaseHTTPRequestHandler, HTTPServer import cgi # import CRUD Operations from Lesson 1 @@ -29,7 +29,7 @@ def do_GET(self): output += "


" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) return except IOError: self.send_error(404, 'File Not Found: %s' % self.path) @@ -38,10 +38,10 @@ def do_GET(self): def main(): try: server = HTTPServer(('', 8080), webServerHandler) - print 'Web server running...open localhost:8080/restaurants in your browser' + print('Web server running...open localhost:8080/restaurants in your browser') server.serve_forever() except KeyboardInterrupt: - print '^C received, shutting down server' + print('^C received, shutting down server') server.socket.close() if __name__ == '__main__': From c9bd74aae6b20608b88e14f85f6aebf2b62c1e40 Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Thu, 23 May 2019 18:49:12 +0530 Subject: [PATCH 05/12] [PS] Make code Python 3 compatible --- .../Objective-2-Solution/database_setup.py | 33 ++ Lesson-2/Objective-2-Solution/lotsofmenus.py | 380 ++++++++++++++++++ Lesson-2/Objective-2-Solution/webserver.py | 8 +- 3 files changed, 417 insertions(+), 4 deletions(-) create mode 100644 Lesson-2/Objective-2-Solution/database_setup.py create mode 100644 Lesson-2/Objective-2-Solution/lotsofmenus.py diff --git a/Lesson-2/Objective-2-Solution/database_setup.py b/Lesson-2/Objective-2-Solution/database_setup.py new file mode 100644 index 0000000..5016567 --- /dev/null +++ b/Lesson-2/Objective-2-Solution/database_setup.py @@ -0,0 +1,33 @@ +import os +import sys +from sqlalchemy import Column, ForeignKey, Integer, String +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import relationship +from sqlalchemy import create_engine + +Base = declarative_base() + + +class Restaurant(Base): + __tablename__ = 'restaurant' + + id = Column(Integer, primary_key=True) + name = Column(String(250), nullable=False) + + +class MenuItem(Base): + __tablename__ = 'menu_item' + + name = Column(String(80), nullable=False) + id = Column(Integer, primary_key=True) + description = Column(String(250)) + price = Column(String(8)) + course = Column(String(250)) + restaurant_id = Column(Integer, ForeignKey('restaurant.id')) + restaurant = relationship(Restaurant) + + +engine = create_engine('sqlite:///restaurantmenu.db') + + +Base.metadata.create_all(engine) diff --git a/Lesson-2/Objective-2-Solution/lotsofmenus.py b/Lesson-2/Objective-2-Solution/lotsofmenus.py new file mode 100644 index 0000000..e4c78f0 --- /dev/null +++ b/Lesson-2/Objective-2-Solution/lotsofmenus.py @@ -0,0 +1,380 @@ +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +from database_setup import Restaurant, Base, MenuItem + +engine = create_engine('sqlite:///restaurantmenu.db') +# Bind the engine to the metadata of the Base class so that the +# declaratives can be accessed through a DBSession instance +Base.metadata.bind = engine + +DBSession = sessionmaker(bind=engine) +# A DBSession() instance establishes all conversations with the database +# and represents a "staging zone" for all the objects loaded into the +# database session object. Any change made against the objects in the +# session won't be persisted into the database until you call +# session.commit(). If you're not happy about the changes, you can +# revert all of them back to the last commit by calling +# session.rollback() +session = DBSession() + + +# Menu for UrbanBurger +restaurant1 = Restaurant(name="Urban Burger") + +session.add(restaurant1) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$7.50", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +menuItem1 = MenuItem(name="French Fries", description="with garlic and parmesan", + price="$2.99", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Chicken Burger", description="Juicy grilled chicken patty with tomato mayo and lettuce", + price="$5.50", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Chocolate Cake", description="fresh baked and served with ice cream", + price="$3.99", course="Dessert", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Sirloin Burger", description="Made with grade A beef", + price="$7.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem5 = MenuItem(name="Root Beer", description="16oz of refreshing goodness", + price="$1.99", course="Beverage", restaurant=restaurant1) + +session.add(menuItem5) +session.commit() + +menuItem6 = MenuItem(name="Iced Tea", description="with Lemon", + price="$.99", course="Beverage", restaurant=restaurant1) + +session.add(menuItem6) +session.commit() + +menuItem7 = MenuItem(name="Grilled Cheese Sandwich", description="On texas toast with American Cheese", + price="$3.49", course="Entree", restaurant=restaurant1) + +session.add(menuItem7) +session.commit() + +menuItem8 = MenuItem(name="Veggie Burger", description="Made with freshest of ingredients and home grown spices", + price="$5.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem8) +session.commit() + + +# Menu for Super Stir Fry +restaurant2 = Restaurant(name="Super Stir Fry") + +session.add(restaurant2) +session.commit() + + +menuItem1 = MenuItem(name="Chicken Stir Fry", description="With your choice of noodles vegetables and sauces", + price="$7.99", course="Entree", restaurant=restaurant2) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem( + name="Peking Duck", description=" A famous duck dish from Beijing[1] that has been prepared since the imperial era. The meat is prized for its thin, crisp skin, with authentic versions of the dish serving mostly the skin and little meat, sliced in front of the diners by the cook", price="$25", course="Entree", restaurant=restaurant2) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Spicy Tuna Roll", description="Seared rare ahi, avocado, edamame, cucumber with wasabi soy sauce ", + price="15", course="Entree", restaurant=restaurant2) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Nepali Momo ", description="Steamed dumplings made with vegetables, spices and meat. ", + price="12", course="Entree", restaurant=restaurant2) + +session.add(menuItem4) +session.commit() + +menuItem5 = MenuItem(name="Beef Noodle Soup", description="A Chinese noodle soup made of stewed or red braised beef, beef broth, vegetables and Chinese noodles.", + price="14", course="Entree", restaurant=restaurant2) + +session.add(menuItem5) +session.commit() + +menuItem6 = MenuItem(name="Ramen", description="a Japanese noodle soup dish. It consists of Chinese-style wheat noodles served in a meat- or (occasionally) fish-based broth, often flavored with soy sauce or miso, and uses toppings such as sliced pork, dried seaweed, kamaboko, and green onions.", + price="12", course="Entree", restaurant=restaurant2) + +session.add(menuItem6) +session.commit() + + +# Menu for Panda Garden +restaurant1 = Restaurant(name="Panda Garden") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Pho", description="a Vietnamese noodle soup consisting of broth, linguine-shaped rice noodles called banh pho, a few herbs, and meat.", + price="$8.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Chinese Dumplings", description="a common Chinese dumpling which generally consists of minced meat and finely chopped vegetables wrapped into a piece of dough skin. The skin can be either thin and elastic or thicker.", + price="$6.99", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Gyoza", description="The most prominent differences between Japanese-style gyoza and Chinese-style jiaozi are the rich garlic flavor, which is less noticeable in the Chinese version, the light seasoning of Japanese gyoza with salt and soy sauce, and the fact that gyoza wrappers are much thinner", + price="$9.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Stinky Tofu", description="Taiwanese dish, deep fried fermented tofu served with pickled cabbage.", + price="$6.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$9.50", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +# Menu for Thyme for that +restaurant1 = Restaurant(name="Thyme for That Vegetarian Cuisine ") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Tres Leches Cake", description="Rich, luscious sponge cake soaked in sweet milk and topped with vanilla bean whipped cream and strawberries.", + price="$2.99", course="Dessert", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Mushroom risotto", description="Portabello mushrooms in a creamy risotto", + price="$5.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Honey Boba Shaved Snow", description="Milk snow layered with honey boba, jasmine tea jelly, grass jelly, caramel, cream, and freshly made mochi", + price="$4.50", course="Dessert", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Cauliflower Manchurian", description="Golden fried cauliflower florets in a midly spiced soya,garlic sauce cooked with fresh cilantro, celery, chilies,ginger & green onions", + price="$6.95", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem5 = MenuItem(name="Aloo Gobi Burrito", description="Vegan goodness. Burrito filled with rice, garbanzo beans, curry sauce, potatoes (aloo), fried cauliflower (gobi) and chutney. Nom Nom", + price="$7.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem5) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$6.80", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +# Menu for Tony's Bistro +restaurant1 = Restaurant(name="Tony\'s Bistro ") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Shellfish Tower", description="Lobster, shrimp, sea snails, crawfish, stacked into a delicious tower", + price="$13.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Chicken and Rice", description="Chicken... and rice", + price="$4.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Mom's Spaghetti", description="Spaghetti with some incredible tomato sauce made by mom", + price="$6.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Choc Full O\' Mint (Smitten\'s Fresh Mint Chip ice cream)", + description="Milk, cream, salt, ..., Liquid nitrogen magic", price="$3.95", course="Dessert", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem5 = MenuItem(name="Tonkatsu Ramen", description="Noodles in a delicious pork-based broth with a soft-boiled egg", + price="$7.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem5) +session.commit() + + +# Menu for Andala's +restaurant1 = Restaurant(name="Andala\'s") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Lamb Curry", description="Slow cook that thang in a pool of tomatoes, onions and alllll those tasty Indian spices. Mmmm.", + price="$9.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Chicken Marsala", description="Chicken cooked in Marsala wine sauce with mushrooms", + price="$7.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Potstickers", description="Delicious chicken and veggies encapsulated in fried dough.", + price="$6.50", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Nigiri Sampler", description="Maguro, Sake, Hamachi, Unagi, Uni, TORO!", + price="$6.75", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$7.00", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +# Menu for Auntie Ann's +restaurant1 = Restaurant(name="Auntie Ann\'s Diner ") + +session.add(restaurant1) +session.commit() + +menuItem9 = MenuItem(name="Chicken Fried Steak", description="Fresh battered sirloin steak fried and smothered with cream gravy", + price="$8.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem9) +session.commit() + + +menuItem1 = MenuItem(name="Boysenberry Sorbet", description="An unsettlingly huge amount of ripe berries turned into frozen (and seedless) awesomeness", + price="$2.99", course="Dessert", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Broiled salmon", description="Salmon fillet marinated with fresh herbs and broiled hot & fast", + price="$10.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem3 = MenuItem(name="Morels on toast (seasonal)", description="Wild morel mushrooms fried in butter, served on herbed toast slices", + price="$7.50", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem3) +session.commit() + +menuItem4 = MenuItem(name="Tandoori Chicken", description="Chicken marinated in yoghurt and seasoned with a spicy mix(chilli, tamarind among others) and slow cooked in a cylindrical clay or metal oven which gets its heat from burning charcoal.", + price="$8.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem4) +session.commit() + +menuItem2 = MenuItem(name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", + price="$9.50", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + +menuItem10 = MenuItem(name="Spinach Ice Cream", description="vanilla ice cream made with organic spinach leaves", + price="$1.99", course="Dessert", restaurant=restaurant1) + +session.add(menuItem10) +session.commit() + + +# Menu for Cocina Y Amor +restaurant1 = Restaurant(name="Cocina Y Amor ") + +session.add(restaurant1) +session.commit() + + +menuItem1 = MenuItem(name="Super Burrito Al Pastor", description="Marinated Pork, Rice, Beans, Avocado, Cilantro, Salsa, Tortilla", + price="$5.95", course="Entree", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem2 = MenuItem(name="Cachapa", description="Golden brown, corn-based Venezuelan pancake; usually stuffed with queso telita or queso de mano, and possibly lechon. ", + price="$7.99", course="Entree", restaurant=restaurant1) + +session.add(menuItem2) +session.commit() + + +restaurant1 = Restaurant(name="State Bird Provisions") +session.add(restaurant1) +session.commit() + +menuItem1 = MenuItem(name="Chantrelle Toast", description="Crispy Toast with Sesame Seeds slathered with buttery chantrelle mushrooms", + price="$5.95", course="Appetizer", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + +menuItem1 = MenuItem(name="Guanciale Chawanmushi", description="Japanese egg custard served hot with spicey Italian Pork Jowl (guanciale)", + price="$6.95", course="Dessert", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + + +menuItem1 = MenuItem(name="Lemon Curd Ice Cream Sandwich", description="Lemon Curd Ice Cream Sandwich on a chocolate macaron with cardamom meringue and cashews", + price="$4.25", course="Dessert", restaurant=restaurant1) + +session.add(menuItem1) +session.commit() + + +print("added menu items!") diff --git a/Lesson-2/Objective-2-Solution/webserver.py b/Lesson-2/Objective-2-Solution/webserver.py index 29a794c..cca817e 100644 --- a/Lesson-2/Objective-2-Solution/webserver.py +++ b/Lesson-2/Objective-2-Solution/webserver.py @@ -1,4 +1,4 @@ -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from http.server import BaseHTTPRequestHandler, HTTPServer import cgi # import CRUD Operations from Lesson 1 @@ -34,7 +34,7 @@ def do_GET(self): output += "


" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) return except IOError: self.send_error(404, 'File Not Found: %s' % self.path) @@ -43,10 +43,10 @@ def do_GET(self): def main(): try: server = HTTPServer(('', 8080), webServerHandler) - print 'Web server running...open localhost:8080/restaurants in your browser' + print('Web server running...open localhost:8080/restaurants in your browser') server.serve_forever() except KeyboardInterrupt: - print '^C received, shutting down server' + print('^C received, shutting down server') server.socket.close() if __name__ == '__main__': From 93b990e3c3a1b9346ddc625ea4c49ade0610d53c Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Thu, 23 May 2019 19:19:32 +0530 Subject: [PATCH 06/12] [PS] Updated code to Python 3 --- Lesson-2/post-web-server/webserver.py | 30 +++++++++++++++------------ 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Lesson-2/post-web-server/webserver.py b/Lesson-2/post-web-server/webserver.py index 3a62f33..32c425c 100644 --- a/Lesson-2/post-web-server/webserver.py +++ b/Lesson-2/post-web-server/webserver.py @@ -1,6 +1,7 @@ -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from http.server import BaseHTTPRequestHandler, HTTPServer import cgi - +import sys +## Source : https://stackoverflow.com/questions/36909047/do-post-not-working-on-http-server-with-python class webServerHandler(BaseHTTPRequestHandler): @@ -15,8 +16,8 @@ def do_GET(self): output += "

Hello!

" output += '''

What would you like me to say?

''' output += "" - self.wfile.write(output) - print output + self.wfile.write(output.encode()) + print(output) return if self.path.endswith("/hola"): @@ -28,8 +29,8 @@ def do_GET(self): output += "

¡ Hola !

" output += '''

What would you like me to say?

''' output += "" - self.wfile.write(output) - print output + self.wfile.write(output.encode()) + print(output) return except IOError: @@ -41,30 +42,33 @@ def do_POST(self): self.send_header('Content-type', 'text/html') self.end_headers() ctype, pdict = cgi.parse_header( - self.headers.getheader('content-type')) + self.headers.get('content-type')) ## Changed getheader to get + pdict['boundary'] = bytes(pdict['boundary'], "utf-8") ## For Python 3 if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('message') output = "" output += "" output += "

Okay, how about this:

" - output += "

%s

" % messagecontent[0] + output += "

%s

" % messagecontent[0].decode("utf-8") output += '''

What would you like me to say?

''' output += "" - self.wfile.write(output) - print output + #self.wfile.write(bytes(output, "utf-8")) + self.wfile.write(output.encode()) + print(output) except: - pass + self.send_error(404, "{}".format(sys.exc_info()[0])) + print(sys.exc_info()) def main(): try: port = 8080 server = HTTPServer(('', port), webServerHandler) - print "Web Server running on port %s" % port + print("Web Server running on port %s" % port) server.serve_forever() except KeyboardInterrupt: - print " ^C entered, stopping web server...." + print(" ^C entered, stopping web server....") server.socket.close() if __name__ == '__main__': From 8f94ef5e3bb91579689f1967afc4acb7e966bd48 Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Fri, 24 May 2019 19:41:17 +0530 Subject: [PATCH 07/12] [PS] Update code to Python 3 --- Lesson-2/Objective-4-Solution/webserver.py | 27 +++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Lesson-2/Objective-4-Solution/webserver.py b/Lesson-2/Objective-4-Solution/webserver.py index 505af75..fa579ea 100644 --- a/Lesson-2/Objective-4-Solution/webserver.py +++ b/Lesson-2/Objective-4-Solution/webserver.py @@ -1,5 +1,7 @@ -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from http.server import BaseHTTPRequestHandler, HTTPServer import cgi +import sys + # import CRUD Operations from Lesson 1 from database_setup import Base, Restaurant, MenuItem @@ -29,7 +31,7 @@ def do_GET(self): output += " " output += "" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) return if self.path.endswith("/edit"): restaurantIDPath = self.path.split("/")[2] @@ -49,7 +51,7 @@ def do_GET(self): output += "" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) if self.path.endswith("/restaurants"): restaurants = session.query(Restaurant).all() @@ -72,7 +74,7 @@ def do_GET(self): output += "


" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) return except IOError: self.send_error(404, 'File Not Found: %s' % self.path) @@ -82,7 +84,8 @@ def do_POST(self): try: if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header( - self.headers.getheader('content-type')) + self.headers.get('content-type')) + pdict['boundary'] = bytes(pdict['boundary'], "utf-8") ## For Python 3 if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') @@ -91,7 +94,7 @@ def do_POST(self): myRestaurantQuery = session.query(Restaurant).filter_by( id=restaurantIDPath).one() if myRestaurantQuery != []: - myRestaurantQuery.name = messagecontent[0] + myRestaurantQuery.name = messagecontent[0].decode("utf-8") session.add(myRestaurantQuery) session.commit() self.send_response(301) @@ -101,13 +104,14 @@ def do_POST(self): if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( - self.headers.getheader('content-type')) + self.headers.get('content-type')) + pdict['boundary'] = bytes(pdict['boundary'], "utf-8") ## For Python 3 if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') # Create new Restaurant Object - newRestaurant = Restaurant(name=messagecontent[0]) + newRestaurant = Restaurant(name=messagecontent[0].decode("utf-8")) session.add(newRestaurant) session.commit() @@ -117,16 +121,17 @@ def do_POST(self): self.end_headers() except: - pass + self.send_error(404, "{}".format(sys.exc_info()[0])) + print(sys.exc_info()) def main(): try: server = HTTPServer(('', 8080), webServerHandler) - print 'Web server running...open localhost:8080/restaurants in your browser' + print('Web server running...open localhost:8080/restaurants in your browser') server.serve_forever() except KeyboardInterrupt: - print '^C received, shutting down server' + print('^C received, shutting down server') server.socket.close() From aabea00508ce2303740ba652b979597c40e4afe8 Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Fri, 24 May 2019 19:47:32 +0530 Subject: [PATCH 08/12] [PS] Update code to Python 3 --- Lesson-2/Objective-3-Solution/webserver.py | 28 +++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Lesson-2/Objective-3-Solution/webserver.py b/Lesson-2/Objective-3-Solution/webserver.py index 68875bd..ea3e79e 100644 --- a/Lesson-2/Objective-3-Solution/webserver.py +++ b/Lesson-2/Objective-3-Solution/webserver.py @@ -1,5 +1,7 @@ -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from http.server import BaseHTTPRequestHandler, HTTPServer import cgi +import sys + # import CRUD Operations from Lesson 1 from database_setup import Base, Restaurant, MenuItem @@ -28,10 +30,10 @@ def do_GET(self): output += "
" output += " " output += "" - output += "
" - self.wfile.write(output) + output += "" + self.wfile.write(bytes(output, "utf-8")) return - + if self.path.endswith("/restaurants"): restaurants = session.query(Restaurant).all() output = "" @@ -46,30 +48,32 @@ def do_GET(self): output += restaurant.name output += "
" # Objective 2 -- Add Edit and Delete Links + # Objective 4 -- Replace Edit href output += "Edit " output += "
" output += " Delete " output += "


" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) return - except IOError: self.send_error(404, 'File Not Found: %s' % self.path) # Objective 3 Step 3- Make POST method def do_POST(self): try: + if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( - self.headers.getheader('content-type')) + self.headers.get('content-type')) + pdict['boundary'] = bytes(pdict['boundary'], "utf-8") ## For Python 3 if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') # Create new Restaurant Object - newRestaurant = Restaurant(name=messagecontent[0]) + newRestaurant = Restaurant(name=messagecontent[0].decode("utf-8")) session.add(newRestaurant) session.commit() @@ -79,17 +83,19 @@ def do_POST(self): self.end_headers() except: - pass + self.send_error(404, "{}".format(sys.exc_info()[0])) + print(sys.exc_info()) def main(): try: server = HTTPServer(('', 8080), webServerHandler) - print 'Web server running... Open localhost:8080/restaurants in your browser' + print('Web server running...open localhost:8080/restaurants in your browser') server.serve_forever() except KeyboardInterrupt: - print '^C received, shutting down server' + print('^C received, shutting down server') server.socket.close() + if __name__ == '__main__': main() From ba8a3cd403020dc1e4093eb92b6ee254f1de787e Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Fri, 24 May 2019 19:55:44 +0530 Subject: [PATCH 09/12] [PS] Add code for Python 2 --- Lesson-2/Objective-5-Solution/webserver.py | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Lesson-2/Objective-5-Solution/webserver.py b/Lesson-2/Objective-5-Solution/webserver.py index 0762589..24329bb 100644 --- a/Lesson-2/Objective-5-Solution/webserver.py +++ b/Lesson-2/Objective-5-Solution/webserver.py @@ -1,5 +1,6 @@ -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from http.server import BaseHTTPRequestHandler, HTTPServer import cgi +import sys # import CRUD Operations from Lesson 1 ## from database_setup import Base, Restaurant, MenuItem @@ -29,7 +30,7 @@ def do_GET(self): output += " " output += "" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) return if self.path.endswith("/edit"): restaurantIDPath = self.path.split("/")[2] @@ -49,7 +50,8 @@ def do_GET(self): output += "" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) + if self.path.endswith("/delete"): restaurantIDPath = self.path.split("/")[2] @@ -66,7 +68,8 @@ def do_GET(self): output += "" output += "" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) + if self.path.endswith("/restaurants"): restaurants = session.query(Restaurant).all() @@ -91,7 +94,7 @@ def do_GET(self): output += "


" output += "" - self.wfile.write(output) + self.wfile.write(bytes(output, "utf-8")) return except IOError: self.send_error(404, 'File Not Found: %s' % self.path) @@ -113,7 +116,8 @@ def do_POST(self): if self.path.endswith("/edit"): ctype, pdict = cgi.parse_header( - self.headers.getheader('content-type')) + self.headers.get('content-type')) + pdict['boundary'] = bytes(pdict['boundary'], "utf-8") ## For Python 3 if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') @@ -122,7 +126,7 @@ def do_POST(self): myRestaurantQuery = session.query(Restaurant).filter_by( id=restaurantIDPath).one() if myRestaurantQuery != []: - myRestaurantQuery.name = messagecontent[0] + myRestaurantQuery.name = messagecontent[0].decode("utf-8") session.add(myRestaurantQuery) session.commit() self.send_response(301) @@ -132,13 +136,14 @@ def do_POST(self): if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( - self.headers.getheader('content-type')) + self.headers.get('content-type')) + pdict['boundary'] = bytes(pdict['boundary'], "utf-8") ## For Python 3 if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') # Create new Restaurant Object - newRestaurant = Restaurant(name=messagecontent[0]) + newRestaurant = Restaurant(name=messagecontent[0].decode("utf-8")) session.add(newRestaurant) session.commit() @@ -148,16 +153,17 @@ def do_POST(self): self.end_headers() except: - pass + self.send_error(404, "{}".format(sys.exc_info()[0])) + print(sys.exc_info()) def main(): try: server = HTTPServer(('', 8080), webServerHandler) - print 'Web server running...open localhost:8080/restaurants in your browser' + print('Web server running...open localhost:8080/restaurants in your browser') server.serve_forever() except KeyboardInterrupt: - print '^C received, shutting down server' + print('^C received, shutting down server') server.socket.close() From f122c4dfb762607c188eb8604104a360cec557f4 Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Fri, 24 May 2019 19:59:24 +0530 Subject: [PATCH 10/12] [PS] Update code to Python 3 From c809d47cdd7f042516d09cb1c7dd6d650cf2b73b Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Fri, 24 May 2019 20:00:13 +0530 Subject: [PATCH 11/12] Update code to Python 3 From b11e151b944ae389ed23686f4ec4ebeae29c2a66 Mon Sep 17 00:00:00 2001 From: Pritesh Shrivastava Date: Fri, 24 May 2019 20:01:35 +0530 Subject: [PATCH 12/12] [PS] Update code to Python 3 --- Lesson-2/Objective-5-Solution/webserver.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lesson-2/Objective-5-Solution/webserver.py b/Lesson-2/Objective-5-Solution/webserver.py index 24329bb..dc16bc6 100644 --- a/Lesson-2/Objective-5-Solution/webserver.py +++ b/Lesson-2/Objective-5-Solution/webserver.py @@ -1,3 +1,4 @@ +## Updated to Python 3 from http.server import BaseHTTPRequestHandler, HTTPServer import cgi import sys