pointers.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from typing import Any, Iterable, Optional, SupportsIndex, Union
  2. class PointerList(list):
  3. """Pointer to list saved in database"""
  4. def __init__(
  5. self,
  6. db: "Database", # type: ignore
  7. module: str,
  8. key: str,
  9. default: Optional[Any] = None,
  10. ):
  11. self._db = db
  12. self._module = module
  13. self._key = key
  14. self._default = default
  15. super().__init__(db.get(module, key, default))
  16. def __repr__(self):
  17. return f"PointerList({list(self)})"
  18. def __str__(self):
  19. return f"PointerList({list(self)})"
  20. def __delitem__(self, __i: Union[SupportsIndex, slice]) -> None:
  21. a = super().__delitem__(__i)
  22. self._save()
  23. return a
  24. def __setitem__(self, __i: Union[SupportsIndex, slice], __v: Any) -> None:
  25. a = super().__setitem__(__i, __v)
  26. self._save()
  27. return a
  28. def __iadd__(self, __x: Iterable) -> "Self": # type: ignore
  29. a = super().__iadd__(__x)
  30. self._save()
  31. return a
  32. def __imul__(self, __x: int) -> "Self": # type: ignore
  33. a = super().__imul__(__x)
  34. self._save()
  35. return a
  36. def append(self, value: Any):
  37. super().append(value)
  38. self._save()
  39. def extend(self, value: Iterable):
  40. super().extend(value)
  41. self._save()
  42. def insert(self, index: int, value: Any):
  43. super().insert(index, value)
  44. self._save()
  45. def remove(self, value: Any):
  46. super().remove(value)
  47. self._save()
  48. def pop(self, index: int = -1) -> Any:
  49. a = super().pop(index)
  50. self._save()
  51. return a
  52. def clear(self) -> None:
  53. super().clear()
  54. self._save()
  55. def _save(self):
  56. self._db.set(self._module, self._key, list(self))
  57. class PointerDict(dict):
  58. """Pointer to dict saved in database"""
  59. def __init__(
  60. self,
  61. db: "Database", # type: ignore
  62. module: str,
  63. key: str,
  64. default: Optional[Any] = None,
  65. ):
  66. self._db = db
  67. self._module = module
  68. self._key = key
  69. self._default = default
  70. super().__init__(db.get(module, key, default))
  71. def __repr__(self):
  72. return f"PointerDict({dict(self)})"
  73. def __bool__(self) -> bool:
  74. return bool(self._db.get(self._module, self._key, self._default))
  75. def __setitem__(self, key: str, value: Any):
  76. super().__setitem__(key, value)
  77. self._save()
  78. def __delitem__(self, key: str):
  79. super().__delitem__(key)
  80. self._save()
  81. def __str__(self):
  82. return f"PointerDict({dict(self)})"
  83. def update(self, __m: dict) -> None:
  84. super().update(__m)
  85. self._save()
  86. def setdefault(self, key: str, default: Any = None) -> Any:
  87. a = super().setdefault(key, default)
  88. self._save()
  89. return a
  90. def pop(self, key: str, default: Any = None) -> Any:
  91. a = super().pop(key, default)
  92. self._save()
  93. return a
  94. def popitem(self) -> tuple:
  95. a = super().popitem()
  96. self._save()
  97. return a
  98. def clear(self) -> None:
  99. super().clear()
  100. self._save()
  101. def _save(self):
  102. self._db.set(self._module, self._key, dict(self))