pointers.py 3.3 KB

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