import typing import collections.abc from .product import product from .exception import not_ready_exception class product_response: def __new__( cls, target: product | typing.Iterable[product] ) -> list | dict: if isinstance(target, product): return cls.single(target) if isinstance(target, collections.abc.Iterable): return cls.collection(target) raise TypeError("Bad type for product response generator.") def single(target: product) -> dict: if not target.ready: raise not_ready_exception(target) return { "name": target.name, "description": target.description, "author": target.author, "image": target.image, "stock_count": target.stock_count, "barcode": target.barcode } def collection(targets: typing.Iterable[product]) -> list: result = list() for count in targets: if not isinstance(count, product): raise TypeError("Products iterable must contain products.") result.append(product_response.single(count)) return result