vasil_k_k

OOP Reminder in colours

Jul 5th, 2023 (edited)
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. import sys
  2. from typing import List
  3.  
  4. class SandwichError(Exception):
  5.     pass
  6.  
  7.  
  8. class Colors:
  9.     HEADER = '\033[95m'
  10.     BLUE = '\033[94m'
  11.     GREEN = '\033[92m'
  12.     YELLOW = '\033[93m'
  13.     RED = '\033[91m'
  14.     BOLD = '\033[1m'
  15.     UNDERLINE = '\033[4m'
  16.     RESET = '\033[0m'
  17.  
  18.  
  19. class Products:
  20.     def __init__(self, product_list: List[str]) -> None:
  21.         self.product_list = product_list
  22.  
  23.     @property
  24.     def product_list(self):
  25.         return self.__product_list
  26.  
  27.     @product_list.setter
  28.     def product_list(self, value):
  29.         if "sandwich" not in value:
  30.             raise SandwichError("You forgot the sandwich!!!")
  31.         self.__product_list = value
  32.  
  33.     @classmethod
  34.     def custom(cls):
  35.         my_products = ["banana", "sandwich", "chocolate"]
  36.         return cls(my_products)
  37.  
  38.     def __str__(self) -> str:
  39.         return "This is very cool and works as expected"
  40.  
  41.  
  42.  
  43. products = Products.custom()
  44.  
  45. try:
  46.     sorted_product = next(filter(lambda x: x == "sandwich", products.product_list))
  47.     print(Colors.BLUE + f"I found a {sorted_product} from the list of products: {', '.join(products.product_list)}" + Colors.RESET)
  48. except StopIteration:
  49.     print(Colors.RED + "I can not find it" + Colors.RESET)
  50. else:
  51.     print(Colors.GREEN + "I will try next time" + Colors.RESET)
  52. finally:
  53.     print(Colors.HEADER + Colors.BOLD + "I will do it anyway" + Colors.RESET)
  54.  
  55. print(products, file=sys.stderr)
  56.  
  57. print(Colors.UNDERLINE + Colors.RED + ", ".join(next(iter(products.__dict__.values()))) + Colors.RESET)
Advertisement
Add Comment
Please, Sign In to add comment