battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

the LivinGrimoire Artificial General Intelligence software design pattern forum

descriptionsimple but dependable database python class for the livingrimoire Emptysimple but dependable database python class for the livingrimoire

more_horiz

Code:

import os


from LivinGrimoire23 import AbsDictionaryDB

'''
example usage code:
    db1: AbsDictionaryDB = LivinGrimoirePandaDB()
    db1.save("test1", "working1")
    db1.save("test2", "working2")
    db1.save("test2", "working3")
    print(db1.load("test1"))
    print(db1.load("test2"))
    print(db1.load("test3"))
'''


def save_to_txt(db_directory, key, value):
    with open(f'{db_directory}/{key}.txt', mode="w") as file:
        file.write(value)


def read_from_txt(db_directory, key):
    if not os.path.exists(f'{db_directory}/{key}.txt'):
        return "null"
    with open(f'{db_directory}/{key}.txt', 'r') as f:
        return f.read()


def check_data_exists(db_directory, key):
    return f'{db_directory}/{key}.txt'


class LivinGrimoirePandaDB(AbsDictionaryDB):
    def __init__(self, database_name: str = 'database'):
        super().__init__()
        self._db_name = database_name
        if not os.path.exists(self._db_name):
            os.makedirs(self._db_name)
            print(f"Directory '{self._db_name}' created successfully.")
        else:
            print(f"Directory '{self._db_name}' already exists.")

    def save(self, key: str, value: str):
        save_to_txt(self._db_name, key, value)

    def load(self, key: str) -> str:
        return read_from_txt(self._db_name, key)

    def dataExists(self, key: str) -> bool:
        return check_data_exists(self._db_name, key)

descriptionsimple but dependable database python class for the livingrimoire EmptyRe: simple but dependable database python class for the livingrimoire

more_horiz
here is how to add the database to the livingrimoire:
Chobit.setDatabase(LivinGrimoirePandaDB())

LivinGrimoirePandaDB() is simply a subclass of AbsDictionaryDB, meaninig it has a save and a load method.

I also updated the basic DiSayer skill to test it:

Code:

class DiSayer(DiSkillV2):
    def __init__(self):
        super().__init__()
        self.cmdBreaker = AXCmdBreaker("say")
        self.command = ""

    def input(self, ear, skin, eye):
        if len(ear) == 0:
            return
        if ear == "say something":
            self.setSimpleAlg(self.getKokoro().grimoireMemento.simpleLoad(f'disayer'))
            return

        self.command = self.cmdBreaker.extractCmdParam(ear)
        if self.command:
            self.getKokoro().grimoireMemento.simpleSave(f'disayer', self.command)
            self.setSimpleAlg(self.command)
            self.command = ""


say hello
for example will save: key disayer, value: hello

after which the command say something will result in the output hello (even if the app was reloaded because the data was saved)

if no data is found the output for say something is "null"

descriptionsimple but dependable database python class for the livingrimoire EmptyRe: simple but dependable database python class for the livingrimoire

more_horiz
https://rumble.com/v4hyha6-livingrimoire-database-tutorial.html
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply