Files
Ultroid-fork/pyUltroid/dB/base.py
Devesh Pal 09971ca76d Update Ultroid v0.8
---------

Co-authored-by: Amit Sharma <48654350+buddhhu@users.noreply.github.com>
Co-authored-by: Aditya <me@xditya.me>
Co-authored-by: Dark <darkbeamer.official@gmail.com>
Co-authored-by: buddhhu <amitsharma123234@gmail.com>
Co-authored-by: Kaif <88398455+kaif-00z@users.noreply.github.com>
Co-authored-by: 1Danish-00 <danish@ultroid.tech>
Co-authored-by: TechiError <techierror@gmail.com>
Co-authored-by: Dark <59723913+DarkBeamerYT@users.noreply.github.com>
2023-02-20 10:51:29 +05:30

45 lines
1.3 KiB
Python

from .. import udB
class KeyManager:
def __init__(self, key, cast=None) -> None:
self._key = key
self._cast = cast
def get(self):
_data = udB.get_key(self._key)
if self._cast and not isinstance(_data, self._cast):
return [_data] if self._cast == list else self._cast(_data)
return _data or (self._cast() if callable(self._cast) else self._cast)
def get_child(self, key):
return self.get()[key]
def count(self):
return len(self.get())
def add(self, item):
content = self.get()
if content == None and callable(type(item)):
content = type(item)()
if isinstance(content, dict) and isinstance(item, dict):
content.update(item)
elif isinstance(content, list) and item not in content:
content.append(item)
else:
return
udB.set_key(self._key, content)
def remove(self, item):
content = self.get()
if isinstance(content, list) and item in content:
content.remove(item)
elif isinstance(content, dict) and content.get(item):
del content[item]
else:
return
udB.set_key(self._key, content)
def contains(self, item):
return item in self.get()