From 12b58e7bf1e331f8dfa4198c8db4f4277bb6dd22 Mon Sep 17 00:00:00 2001 From: Askill Date: Thu, 15 Aug 2024 18:02:13 +0200 Subject: [PATCH] formatting --- app/ShyBadger/src/endpoints/BasketEndpoint.py | 7 ++-- app/ShyBadger/src/main.py | 6 ++-- app/ShyBadger/src/models/Item.py | 3 +- app/ShyBadger/src/run.py | 2 +- app/ShyBadger/src/services/BasketService.py | 5 ++- app/ShyBadger/src/services/DealsService.py | 36 +++++++++---------- app/ShyBadger/src/services/ProductService.py | 3 +- 7 files changed, 32 insertions(+), 30 deletions(-) diff --git a/app/ShyBadger/src/endpoints/BasketEndpoint.py b/app/ShyBadger/src/endpoints/BasketEndpoint.py index fba12c6..e6a78ca 100644 --- a/app/ShyBadger/src/endpoints/BasketEndpoint.py +++ b/app/ShyBadger/src/endpoints/BasketEndpoint.py @@ -4,6 +4,7 @@ import uuid from src.services import BasketService + class Basket(Resource): basket_service = BasketService() @@ -15,11 +16,13 @@ class Basket(Resource): def get(self): if "session_id" not in session: session["session_id"] = self.get_session_id() - return Response({"total": self.basket_service.total()}, mimetype="application/json") + return Response( + {"total": self.basket_service.total()}, mimetype="application/json" + ) def post(self): # expects something similar: {"items": [{"item_id": abc002}]} - + if "session_id" not in session: session["session_id"] = self.get_session_id() jsonData = request.get_json(force=True) diff --git a/app/ShyBadger/src/main.py b/app/ShyBadger/src/main.py index 0c41ee4..a84bce3 100644 --- a/app/ShyBadger/src/main.py +++ b/app/ShyBadger/src/main.py @@ -7,14 +7,14 @@ import logging import endpoints.BasketEndpoint as BasketEndpoint app = Flask(__name__) -app.secret_key = 'SUPER_DUPER_BAD_SECRET_KEY' +app.secret_key = "SUPER_DUPER_BAD_SECRET_KEY" api = Api(app) logging.basicConfig(level=logging.DEBUG) -api.add_resource(BasketEndpoint.Basket,'/api/v1/basket/') +api.add_resource(BasketEndpoint.Basket, "/api/v1/basket/") + @app.route("/") def index(): """in lieu of a UI redirect to the API doc""" app.logger.info(f"request from {request.remote_addr}") return redirect("/api/doc/swagger.json", code=302) - diff --git a/app/ShyBadger/src/models/Item.py b/app/ShyBadger/src/models/Item.py index 7e16611..c1e0213 100644 --- a/app/ShyBadger/src/models/Item.py +++ b/app/ShyBadger/src/models/Item.py @@ -1,6 +1,7 @@ from dataclasses import dataclass + @dataclass class Item: id: str - price: str \ No newline at end of file + price: str diff --git a/app/ShyBadger/src/run.py b/app/ShyBadger/src/run.py index 2b2a255..d54fb3f 100644 --- a/app/ShyBadger/src/run.py +++ b/app/ShyBadger/src/run.py @@ -2,4 +2,4 @@ from main import app # disabled threading for easier session handleing in this demo project # if threaded was True, as session store would be needed -app.run(host="0.0.0.0", port='80', debug=False, threaded=False) \ No newline at end of file +app.run(host="0.0.0.0", port="80", debug=False, threaded=False) diff --git a/app/ShyBadger/src/services/BasketService.py b/app/ShyBadger/src/services/BasketService.py index 566f1bf..3bb6b71 100644 --- a/app/ShyBadger/src/services/BasketService.py +++ b/app/ShyBadger/src/services/BasketService.py @@ -1,4 +1,3 @@ - from collections import defaultdict from DealsService import DealsService from ProductService import ProductService @@ -18,7 +17,7 @@ class BasketService: items = self.baskets[sessionID] prices = self.dealsService.get_items_with_final_prices(items) return sum(prices) - + def scan(self, session_id: str, itemId) -> None: item = self.productService.get_item_from_id(itemId) - self.baskets[session_id].append(item) \ No newline at end of file + self.baskets[session_id].append(item) diff --git a/app/ShyBadger/src/services/DealsService.py b/app/ShyBadger/src/services/DealsService.py index 275cce0..ee8e19e 100644 --- a/app/ShyBadger/src/services/DealsService.py +++ b/app/ShyBadger/src/services/DealsService.py @@ -1,4 +1,3 @@ - from collections import defaultdict import math @@ -14,10 +13,9 @@ class DealsService: self.promotions["A0001"].append("two_for_one") self.promotions["A0002"].append("ten_percent_off") - # deal behavior needs to be programmed an can only be - # changed with the rollout of a new version, + # deal behavior needs to be programmed an can only be + # changed with the rollout of a new version, self.deals["two_for_one"] = DealsService.two_for_one_deal - def get_items_with_final_prices(self, items) -> list: groups = defaultdict(list) @@ -26,7 +24,7 @@ class DealsService: groups[obj.id].append(obj) for group in groups.values(): - # the deals do not stack, the first deal in the list of deals is taken, + # the deals do not stack, the first deal in the list of deals is taken, # ideally for the customer all deals would be calculated and the maximum value selected # not implemented for time reasons @@ -40,36 +38,38 @@ class DealsService: break return [item for group in groups.values() for item in group] - + @staticmethod def two_for_one_deal(group): - '''' applies deal onto item group in place ''' - # ideally would be an object implementing a "Deal"-interface, - # which has 2 functions, deal_is_applicable() and apply_deal() + """' applies deal onto item group in place""" + + # ideally would be an object implementing a "Deal"-interface, + # which has 2 functions, deal_is_applicable() and apply_deal() def _round_up(self, val: float): - return math.ceil(val*100)/100 - + return math.ceil(val * 100) / 100 + # could be compressed # not done since this would be the 2 different functions in the above mentioned interface deal_is_applicable = False if len(group) >= 2: deal_is_applicable = True - for i in range(len(group)//2): + for i in range(len(group) // 2): group[i].price = _round_up(group[i].price / 2) return deal_is_applicable - + @staticmethod def ten_percent_off(group): # the percentage discount could be added to function signature - '''' applies deal onto item group in place ''' - # ideally would be an object implementing a "Deal"-interface, - # which has 2 functions, deal_is_applicable() and apply_deal() + """' applies deal onto item group in place""" + + # ideally would be an object implementing a "Deal"-interface, + # which has 2 functions, deal_is_applicable() and apply_deal() def _round_up(self, val: float): - return math.ceil(val*100)/100 + return math.ceil(val * 100) / 100 deal_is_applicable = True for i in range(len(group)): group[i].price = _round_up(group[i].price * 0.9) - return deal_is_applicable \ No newline at end of file + return deal_is_applicable diff --git a/app/ShyBadger/src/services/ProductService.py b/app/ShyBadger/src/services/ProductService.py index 5f79515..6cac84b 100644 --- a/app/ShyBadger/src/services/ProductService.py +++ b/app/ShyBadger/src/services/ProductService.py @@ -2,7 +2,7 @@ from src.models import Item class ProductService: - # prices are global, sheduled function to refresh prices periodically + # prices are global, sheduled function to refresh prices periodically # could save ressources under high load prices = dict() @@ -11,6 +11,5 @@ class ProductService: self.prices["A0001"] = 12.99 self.prices["A0002"] = 3.99 - def get_item_from_id(self, item_id: str) -> Item: return Item(id=item_id, price=self.prices.get(item_id))