Team Ai
Apppublic

azils3/bash

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
base.py45 linesDownload Raw Back to dB
1from .. import udB2 3 4class KeyManager:5    def __init__(self, key, cast=None) -> None:6        self._key = key7        self._cast = cast8 9    def get(self):10        _data = udB.get_key(self._key)11        if self._cast and not isinstance(_data, self._cast):12            return [_data] if self._cast == list else self._cast(_data)13        return _data or (self._cast() if callable(self._cast) else self._cast)14 15    def get_child(self, key):16        return self.get()[key]17 18    def count(self):19        return len(self.get())20 21    def add(self, item):22        content = self.get()23        if content == None and callable(type(item)):24            content = type(item)()25        if isinstance(content, dict) and isinstance(item, dict):26            content.update(item)27        elif isinstance(content, list) and item not in content:28            content.append(item)29        else:30            return31        udB.set_key(self._key, content)32 33    def remove(self, item):34        content = self.get()35        if isinstance(content, list) and item in content:36            content.remove(item)37        elif isinstance(content, dict) and content.get(item):38            del content[item]39        else:40            return41        udB.set_key(self._key, content)42 43    def contains(self, item):44        return item in self.get()45