def main(): myTimer = timer.begin_timer() stringer.show_welcome(NAME) print() # tuple of product objects demonstrating polymorphism products = (Product("Heavy hammer", 12.99, 62), Product("Light nails", 5.06, 0), Movie("Blade Runner", 29.99, 0, 1984), Book("Moby Dick", 19.99, 0, "Herman Melville"), Product("Medium tape", 7.24, 0)) show_products(products) while True: print() try: number = val.get_int("Enter Product number: ", len(products), 0) product = products[number - 1] show_product(product) except Exception as e: print("There was an Error processing your product.", e) break choice = input("View another? (y/n): ").lower() if choice != "y": break # end while loop timer.stop_timer(myTimer) print("Bye!")
def main(): print("The Product Viewer program") print() # a tuple of Product objects products = (Product("Stanley 13 Ounce Wood Hammer", 12.99, 62), Product('National Hardware 3/4" Wire Nails', 5.06, 0), Product("Economy Duct Tape, 60 yds, Silver", 7.24, 0)) show_products(products) while True: number = int(input("Enter product number: ")) print() product = products[number - 1] show_product(product) choice = input("View another product? (y/n): ") print() if choice != "y": print("Bye!") break
def main(): # create two product objects product1 = Product("Stanley 13 Ounce wood Hammer", 12.99, 62) product2 = Product('National Hardware 3/4" Wire Nails', 5.06, 5) # print data for product1 to console print_data(product1) # print data for product2 to console print_data(product2)
def main(): print("The Product Viewer Program") print() products = (Product("Stanley 13 Ounce Wood Hammer", 12.99, 62), Product("National HardWare 3/4 wire Nails", 5.06, 0), Product("Economy Duct Tape 60 yds silver ", 7.24, 0)) show_products(products) while True: number = int(input("Enter The Product Number ? ")) product = products[number - 1] show_product(product) choice = input("View Another Product? (y/n)? ") if choice.lower() != "y": print("bye") break
def main(): print("The Product Viewer program") print() # a tuple of Product objects # products = (Product('Stanley 13 Ounce Wood Hammer', 12.99, 62), # Book("The Big Short", 15.95, 34, "Michael Lewis","Hardcover"), # Movie("The Holy Grail", 14.99, 68, 1975,"DVD"), # Album("Rubber Soul", 10, 0,"The Beatles","CD")) products = (Product('Stanley 13 Ounce Wood Hammer', 12.99, 62), Book("The Big Short", 15.95, 34, "Michael Lewis","Hardcover"), Movie("The Holy Grail", 14.99, 68, 1975,"DVD"), Album("Rubber Soul", 10, 0,"The Beatles","CD")) show_products(products) while True: number = int(input("Enter product number: ")) print() product = products[number-1] show_product(product) choice = input("Continue? (y/n): ") print() if choice != "y": print("Bye!") break
def main(): print("The Product Viewer program") print() # The main function creates a tuple of Product Objects. To do that, it calls the constructer of the Product Class # to create each object, and it stores each object in the tuple without going through the intermediate step of # creating a variable name that refers to the object type. products = (Product('Stanley 13 Ounce Wood Hammer', 12.99, 62), Book("The Big Short", 15.95, 34, "Michael Lewis"), Movie("The Holy Grail - DVD", 14.99, 68, 1975)) show_products(products) # This create the a loop to keep asking for prompts. while True: # Prompts the user to enter a number that corresponds with a product number = int(input("Enter product number: ")) print() # it gets the number assigned to int, substracts 1 to adjust for the index product = products[number-1] # product is assigned the object at the inputted index. # the product is passed to show_product(). show_product uses if statements to handle different object attributes. show_product(product) # prompt to continue. choice = input("Continue?\t (y/n):\t") print() if choice != "y": break
def getShoppingCartProducts(name = None, sortBy = None, minPrice = None, maxPrice = None, offset = None, limit = None, productIds = None, code=None, userId=None): """here caller validation""" if userId == None: raise Exception("Error in shopping cart get; userId not given") products = db.getProducts(name, sortBy, minPrice, maxPrice, offset, limit, productIds, code, userId) cart = db.getShoppingCart(userId) results = [] #Assemble products with cart information for p in products: print(p) """Swap 'count' entry in product with count in shopping cart of customer""" obj = Product.serialize(p) """Delete 'inStock'-entry from obj since we dont need it here""" del obj['inStock'] """Add 'inCart'-entry, which tells us how many products of each type our cart has""" obj['inCart'] = cart.products[p.productId] results.append(obj) print(results) return results
def on_put(self, req, resp, productId): try: productId = int(productId) product = bl.getProduct(productId) if product is None: return falcon.HTTP_404 doc = req.context['doc'] newProduct = Product.from_json(doc) if newProduct.productId is None: newProduct.productId = product.productId #If updated object has id which doesn't match old one, return http forbidden-status elif newProduct.productId != productId: return falcon.HTTP_403 #update product bl.setProduct(newProduct) resp.status = falcon.HTTP_200 #Status Ok except: #Return 500 - internal error resp.status = falcon.HTTP_500
def on_post(self, req, resp): try: doc = req.context['doc'] product = Product.from_json(doc) bl.setProduct(product) resp.status = falcon.HTTP_200 except: resp.status = falcon.HTTP_500
def main(): print("The Product Viewer program") print() products = (Product("Stanley 13 Ounce wood Hammer", 12.99, 62), Product('National Hardware 3/4" Wire Nails', 5.06, 5), Book("The Big Short", 15.95, 34, "Michael Lewis"), Movie("The Holy Grail - DVD", 14.99, 68, 1975)) show_products(products) while True: number = int(input("Enter product number: ")) print() product = products[number - 1] show_product(product) choice = input("Continue? (y/n): ").lower() print() if choice != "y": break
def main(): myTimer = timer.begin_timer() stringer.show_welcome(NAME) # create tuple of Product objects products = (Book("Moby Dick", 19.99, 0, "Herman Melville"), Product("Heavy hammer", 9.95, 13), Movie("Blade Runner", 29.99, 0, 1984)) print() print("This program creates 3 objects of different types and, uses polymorphism to display data on each object.") print() show_products(products) timer.stop_timer(myTimer) print("Bye!")
def main(): print("The Product Viewer Program ") print() products = (Product("Stanley 13 ounce wood hummer", 12.99, 62), Book("The World Is Flat", 15.95, 34, "Tomas Friedman"), Music("The Time Machine", 14.99, 95, 1992)) show_products(products) while True: number = int(input("Enter The Product Number ? ")) print() product = products[number - 1] show_product(product) choice = input("Continue (y/n): ") if choice.lower() != "y": print("bye") break
def main(): print("The Product Viewer program") print() # a tuple of Product objects products = (Product('Stanley 13 Ounce Wood Hammer', 12.99, 62), Book("The Big Short", 15.95, 34, "Michael Lewis"), Movie("The Holy Grail - DVD", 14.99, 68, 1975)) show_products(products) while True: number = int(input("Enter product number: ")) print() product = products[number-1] show_product(product) choice = input("Continue? (y/n): ") print() if choice != "y": print("Bye!") break
def db_init(): db.create_all() customer1 = Customer( name="Sandro Ephrem", address="Mansourieh, Bedran Street Yazbeck Hammouche 10", countryCode=961, email="*****@*****.**", ) customer2 = Customer( name="Maroun Ayli", address="Bsaba, Mtoll Street Anibal Abi Antoun Bdlg", countryCode=961, email="*****@*****.**", ) customer3 = Customer( name="Joe Biden", address= "The White House 1600 Pennsylvania Avenue NW Washington, DC 20500", countryCode=1, email="*****@*****.**", ) product1 = Product(productDescription="Nvidia Geforce RTX 3090", pricePerUnit=700, currency="USD", quantity=1000, unitWeight=0.4) db.session.add(customer1) db.session.add(customer2) db.session.add(customer3) db.session.add(product1) db.session.commit()
def add_product(): if not request.files: xml = request.data else: file = request.files['file'] xml = file.read() data = parseString(xml) prod_details = dict() for event, node in data: if event == START_ELEMENT and node.tagName == 'product': data.expandNode(node) for details in node.childNodes: if not getattr(details, 'tagName', None): continue prod_details[details.tagName] =\ "".join(detail.nodeValue for detail in details.childNodes) product = Product( name=prod_details['name'], owner=prod_details['owner'], price=prod_details['price'] ) with psycopg2.connect(CONNECTION_DATA) as conn: with closing(conn.cursor()) as cur: cur.execute( "INSERT INTO product (name, price, owner) VALUES ('{NAME}', '{PRICE}', '{OWNER}')" .format( NAME=product.name[:254], PRICE=product.price[:254], OWNER=product.owner[:254]) ) conn.commit() return "{NAME};{OWNER};{PRICE}".format( NAME=product.name, OWNER=product.owner, PRICE=product.price )
def main(): print("The Product Viewer program") print() # a tuple of Product objects products = (Product('Stanley 13 Ounce Wood Hammer', 12.99, 62), Book("The Big Short", 15.95, 34, "Hardcover", "Michael Lewis"), Movie("The Holy Grail", 14.99, 68, "DVD", 1975), Album("A Matter of Life And Death", 19.99, 15, "CD", "Iron Maiden")) show_products(products) while True: number = int(input("Enter product number: ")) print() product = products[number - 1] show_product(product) choice = input("Continue? (y/n): ") print() if choice != "y": print("Bye!") break
def create_product(): body = request.get_json() desc = body.get("productDescription") price = body.get("pricePerUnit") currency = body.get("currency") quantity = body.get("quantity") unitWeight = body.get("unitWeight") if desc and price and currency and quantity: product = Product(productDescription=desc, pricePerUnit=price, currency=currency, quantity=quantity, unitWeight=unitWeight) db.session.add(product) db.session.commit() product = db.session.query(Product).filter_by(id=product.id).first() return ( jsonify({ "message": "Product created succesfully", "productId": product.id, "productDescription": product.productDescription, "pricePerUnit": product.pricePerUnit, "currency": product.currency, "quantity": product.quantity, "unitWeight": product.unitWeight }), 200, ) else: return ( jsonify({ "error_code": 400, "message": "Product not created - missing field" }), 400, )
from objects import Product prod2 = Product("laptop", 400, 5) prod1 = Product("phone price", 250) prod3 = Product() print("name: {:s}".format(prod1.name)) print("Price: {:.2f}".format(prod1.price)) print("Discount Percent: {:d}%".format(prod1.discountPercent)) print("Discount amount: {:.2f}".format(prod1.getDiscAmount())) print("Discount Price: {:.2f}".format(prod1.getDiscPrice())) print("name: {:s}".format(prod2.name)) print("Price: {:.2f}".format(prod2.price)) print("Discount Percent: {:d}%".format(prod2.discountPercent)) print("Discount amount: {:.2f}".format(prod2.getDiscAmount())) print("Discount Price: {:.2f}".format(prod2.getDiscPrice())) print("name: {:s}".format(prod3.name)) print("Price: {:.2f}".format(prod3.price)) print("Discount Percent: {:d}%".format(prod3.discountPercent)) print("Discount amount: {:.2f}".format(prod3.getDiscAmount())) print("Discount Price: {:.2f}".format(prod3.getDiscPrice()))
def test_product_serialize(): p = bl.getProduct(1) print(json.dumps(Product.serialize(p), sort_keys = True))
from objects import Product apple = Product("Green Apple", .099) banana = Product("Lettuce", .89, "produce") fruit = [apple, banana] def print_prod_info(fruit): print(fruit.name) for f in fruit: print_prod_info(f)