data.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import functools
  2. from typing import Any, Generator, Callable
  3. class Data:
  4. def __init__(self, super) -> None:
  5. self.data: list[dict[Any, Any]] = list()
  6. self.success: bool = False
  7. self.super = super
  8. @staticmethod
  9. def store_output(func: Callable) -> Callable:
  10. def wrapper(self, *args, **kwargs) -> Any:
  11. output = func(self, *args, **kwargs)
  12. self.success = output
  13. return output
  14. return wrapper
  15. @store_output
  16. def store_data(self, data_gen: Generator, n: int) -> bool:
  17. self.data.clear()
  18. if n == 0:
  19. return True
  20. i: int = 0
  21. while (match := next(data_gen, None)) is not None:
  22. self.data.append(match)
  23. i+=1
  24. if i >= n:
  25. break
  26. else:
  27. if n == float("inf"):
  28. return bool(len(self.data))
  29. self.data.clear()
  30. return False
  31. return True
  32. def get_super(self) -> int:
  33. return self.super
  34. def replace(self, data: "Data") -> None:
  35. self.data.clear()
  36. self.data.extend(data.data)
  37. def append(self, data: Any) -> None:
  38. self.data.append(data)
  39. def clear(self) -> None:
  40. self.data.clear()
  41. def pop(self, index = -1) -> Any:
  42. return self.data.pop(index)
  43. def empty(self) -> bool:
  44. return len(self.data) == 0
  45. def __getitem__(self, index):
  46. return self.data[index]
  47. def __iter__(self):
  48. return self.data.__iter__()
  49. def __len__(self):
  50. return self.data.__len__()