sibinasto

workshop

Jul 25th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.27 KB | None | 0 0
  1. from collections.abc import Iterable
  2.  
  3.  
  4. class CustomListIndexException(Exception):
  5.     pass
  6.  
  7.  
  8. class CustomListTypeException(Exception):
  9.     pass
  10.  
  11.  
  12. class CustomListSumException(Exception):
  13.     pass
  14.  
  15.  
  16. #TODO make this decorator and remove the ifs in the methods
  17. # Decorator IS accepting an argument which is the index, so do not forget the wrapper function
  18. def is_index_integer(index):
  19.     #TODO refactor to be decorator
  20.     if not isinstance(index, int):
  21.         raise CustomListTypeException(f"Index must be of type integer it was {type(index)}")
  22.     return True
  23.  
  24.  
  25. class CustomList:
  26.     def __init__(self, *args):
  27.         self.sequence = [el for el in args]
  28.  
  29.     def append(self, value):
  30.         #TODO int onj is not iterable list(value)
  31.         self.sequence = self.sequence + [value]
  32.         return self.sequence
  33.  
  34.     def remove(self, index):
  35.         try:
  36.             value = self.sequence[index]
  37.             del self.sequence[index]
  38.             return value
  39.         except IndexError as ex:
  40.             raise CustomListIndexException(f"MyCustomList does not found element on this"
  41.                                            f" index - {index}\nOriginal exception was - {str(ex)}")
  42.         except TypeError:
  43.             raise CustomListTypeException(
  44.                 f"Index argument does not mach the supported type. Should be integer it was {type(index)}")
  45.  
  46.     def get(self, index):
  47.         try:
  48.             return self.sequence[index]
  49.         except IndexError as ex:
  50.             raise CustomListIndexException(f"MyCustomList does not found element on this"
  51.                                            f" index - {index}\nOriginal exception was - {str(ex)}")
  52.         except TypeError:
  53.             raise CustomListTypeException(f"Index argument does not mach the supported type. Should be integer it was {type(index)}")
  54.  
  55.     def extend(self, iterable):
  56.         if not isinstance(iterable, Iterable):
  57.             raise CustomListTypeException("The argument should iterable")
  58.         self.sequence = self.sequence + list(iterable)
  59.         return self.sequence
  60.  
  61.     def insert(self, index, value):
  62.         if not isinstance(index, int):
  63.             raise CustomListTypeException(
  64.                 f"Index argument does not mach the supported type. Should be integer it was {type(index)}")
  65.         self.sequence = self.sequence[0:index] + [value] + self.sequence[index:]
  66.         return self.sequence
  67.  
  68.     def pop(self):
  69.         try:
  70.             value = self.sequence[-1]
  71.             del self.sequence[-1]
  72.             return value
  73.         except IndexError:
  74.             raise CustomListIndexException(f"MyCustomList does not contain elements")
  75.  
  76.     def clear(self):
  77.         self.sequence = []
  78.  
  79.     def index(self, value):
  80.         for index in range(len(self.sequence)):
  81.             if self.sequence[index] == value:
  82.                 return index
  83.         return -1
  84.  
  85.     def count(self, value):
  86.         counter = 0
  87.         for el in self.sequence:
  88.             if el == value:
  89.                 counter += 1
  90.         return counter
  91.  
  92.     def reverse(self):
  93.         return self.sequence[::-1]
  94.  
  95.     def copy(self):
  96.         return CustomList(*self.sequence)
  97.  
  98.     def __str__(self):
  99.         return f"{';'.join([repr(el) for el in self.sequence])}"
  100.  
  101.     def __repr__(self):
  102.         return str(self)
  103.  
  104.     def size(self):
  105.         return len(self.sequence)
  106.  
  107.     def add_first(self, value):
  108.         self.sequence = [value] + self.sequence
  109.  
  110.     def dictionize(self):
  111.         custom_dict = {}
  112.         for index in range(0, len(self.sequence), 2):
  113.             try:
  114.                 custom_dict[self.sequence[index]] = self.sequence[index + 1]
  115.             except IndexError:
  116.                 custom_dict[self.sequence[index]] = " "
  117.         return custom_dict
  118.  
  119.     def move(self, amount):
  120.         if len(self.sequence) == 0:
  121.             return []
  122.         self.sequence = self.sequence[amount:] + self.sequence[0:amount]
  123.         return self.sequence
  124.  
  125.     def sum(self):
  126.         result = 0
  127.         for el in self.sequence:
  128.             if isinstance(el, int) or isinstance(el, float):
  129.                 result += el
  130.                 continue
  131.             try:
  132.                 result += len(el)
  133.             except TypeError as ex:
  134.                 raise CustomListSumException(f"Please provide a len method to custom objects if you want to sum elements.\n"
  135.                                              f"Original exception was - {str(ex)}")
  136.         return result
  137.  
  138.     def overbound(self):
  139.         max_number = float('-inf')
  140.         element = None
  141.         for el in self.sequence:
  142.             if not isinstance(el, int) and not isinstance(el, float):
  143.                 num = len(el)
  144.             else:
  145.                 num = el
  146.             if max_number < num:
  147.                 max_number = num
  148.                 element = el
  149.         return self.index(element)
  150.  
  151.     def underbound(self):
  152.         min_number = float('inf')
  153.         element = None
  154.         for el in self.sequence:
  155.             if not isinstance(el, int) and not isinstance(el, float):
  156.                 num = len(el)
  157.             else:
  158.                 num = el
  159.             if min_number > num:
  160.                 min_number = num
  161.                 element = el
  162.         return self.index(element)
  163.  
Advertisement
Add Comment
Please, Sign In to add comment