def get_product(): product = pb.Product() product.name = "gana" product.id = 1234 product.company = "lotte" product.kcal = 300 return MessageToJson(product)
def get(product_name): with MongoClient() as client: db = client.main_db products = db.products temp = products.find_one({"name" : product_name}) res = pb.Product() print("---mongo api---") pprint(temp) print("---mongo api---") if temp is None: return None for key in temp.keys(): value = temp[key] if(key == '_id'): continue if(key == 'col'): key = 'chol' if value == '-': value = 0 if key in ['chol', 'kcal', 'protein', 'fat', 'carbs', 'sugar', 'na']: value = float(value) setattr(res, key, value) #FIXME # change col to chol #res.name = temp['chol'] return res
def products(product_name): print("-----") print(product_name) product = mongo_api.get(product_name) if product is None: product = pb.Product() print(product.name) return MessageToJson(product)
def manualtranslate(): if request.method == 'POST': body = request.get_json() age = body['age'] sex = body['sex'] product_info = pb.Product() product_info.kcal = int(body['kcal']) product_info.protein = int(body['protein']) product_info.fat = int(body['fat']) product_info.carbs = int(body['carbs']) product_info.sugar = int(body['sugar']) product_info.na = int(body['na']) product_info.chol = int(body['chol']) res = pb.CompareDatas() res.basic.product.CopyFrom(product_info) res.translated.CopyFrom(calculate_data.multi(age, sex, product_info)) return MessageToJson(res)
def GetProdsDetail(self, request, context): # Retrieve vendor and prodType from client vendor = request.vendor.lower() product_type = request.productType.lower() logging.info( "have received a request for -> {} <- product type from -> {} <- vendor" .format(product_type, vendor)) try: prod_type_list = get_prods(vendor, product_type) except AssertionError as error: print(error) context.set_details(error) context.set_code(grpc.StatusCode.INVALID_ARGUMENT) return api_pb2.StorageResponse() # time.sleep(5) products = [] if context.is_active(): for prod in prod_type_list: product = api_pb2.Product() product.title = prod["title"] product.url = prod["url"] products.append(product) else: context.set_code(grpc.StatusCode.DEADLINE_EXCEEDED) logging.info("the connection to {} has dropped".format( context.peer())) return api_pb2.StorageResponse() logging.info("a number of {} products were sent to client".format( len(products))) return api_pb2.StorageResponse(prodDetail=products)
ans.product.carbs = round(target['carbs'] / standard['carbs'] * product_info.carbs) ans.product.sugar = round(target['sugar'] / standard['sugar'] * product_info.sugar) ans.product.na = round(target['na'] / standard['na'] * product_info.na) if standard['chol'] == 0: ans.product.chol = 0 else: ans.proudct.chol = round(target['chol'] / standard['chol'] * product_info.chol) return ans if __name__ == "__main__": from mongo import mongo_api product_info = pb.Product() product_info.kcal = 1000 product_info.protein = 1000 product_info.fat = 1000 product_info.carbs = 1000 product_info.sugar = 1000 product_info.na = 1000 product_info.chol = 1000 ans = multi("2", "male", product_info) print(ans.kcal) print(ans.protein) print(ans.fat) print(ans.carbs) print(ans.sugar) print(ans.na) print(ans.chol)