You're a python programmer. You are developing a marketplace application using orm…
- User
You're a python programmer. You are developing a marketplace application using orm peewee and a sqlite database. The following code is already written:
class WBStatus(StatusEnum): """Статус сборочного задания в системе WB."""
WAITING = 'waiting', 'В работе' SORTED = 'sorted', 'Отсортирован' SOLD = 'sold', 'Продан' CANCELED = 'canceled', 'Отмена заказа' CANCELED_BY_CLIENT = 'canceled_by_client', 'Отказ покупателем' DECLINED_BY_CLIENT = 'declined_by_client', 'Покупатель отменил заказ' DEFECT = 'defect', 'Отмена по причине брака' READY_FOR_PICKUP = 'ready_for_pickup', 'Прибыл в ПВЗ' CANCELED_BY_MISSED_CALL = 'canceled_by_missed_call', 'Отмена заказа' def is_new(self) -> bool: return self in (WBStatus.WAITING) def is_delivered(self) -> bool: return self in (WBStatus.SOLD) def is_cancelled(self) -> bool: return self in ( WBStatus.CANCELED, WBStatus.CANCELED_BY_CLIENT, WBStatus.DECLINED_BY_CLIENT, WBStatus.DEFECT, WBStatus.CANCELED_BY_MISSED_CALL, ) @classmethod def get_completed_statuses(cls) -> list['WBStatus']: return [ cls.SOLD, cls.CANCELED, cls.CANCELED_BY_CLIENT, cls.DECLINED_BY_CLIENT, cls.DEFECT, cls.CANCELED_BY_MISSED_CALL, ]In the function response comes a dictionary {'sold': 2, 'waiting': 2} - the number of purchases by status. According to WBStatus enum, count the number of orders in is_new and is_delivered statuses
- Assistant
To count the number of orders in the
is_newandis_deliveredstatuses using theWBStatusenum, you can create a function that processes the dictionary and uses the methods defined in theWBStatusclass to determine which statuses are new and delivered. Here is an example of how you can do this:from typing import Dict def count_orders_by_status(order_counts: Dict[str, int]) -> Dict[str, int]: result = { 'new': 0, 'delivered': 0, } for status_name, count in order_counts.items(): # Convert the status name to a WBStatus enum member status = WBStatus[status_name.upper()] # Check if the status is new if status.is_new(): result['new'] += count # Check if the status is delivered if status.is_delivered(): result['delivered'] += count return result # Example usage: order_counts = {'sold': 2, 'waiting': 2} result = count_orders_by_status(order_counts) print(result) # Output: {'new': 2, 'delivered': 2}