Recreate for python:

import { Database } from "bun:sqlite";
import {
  existsSync,
  mkdirSync,
  readdirSync,
  rmdirSync,
  unlinkSync,
} from "node:fs";
import { dirname, join } from "node:path";

interface FlashcoreOptions {
  namespace?: string | Array<string>;
}

export const Flashcore = {
  delete: (key: string, options?: FlashcoreOptions): boolean => {
    if (options?.namespace) {
      key = Array.isArray(options.namespace)
        ? `${options.namespace.join("/")}__${key}`
        : `${options.namespace}__${key}`;
    }

    const dbPath = join(process.cwd(), ".byte", "data", `${key}.sqlite`);
    if (!existsSync(dbPath)) {
      return false;
    }
    try {
      const db = new Database(dbPath);
      db.query("DROP TABLE IF EXISTS data").run();
      db.close();
      unlinkSync(dbPath);
      return true;
    } catch {
      return false;
    }
  },

  has: (key: string, options?: FlashcoreOptions): boolean => {
    if (options?.namespace) {
      key = Array.isArray(options.namespace)
        ? `${options.namespace.join("/")}__${key}`
        : `${options.namespace}__${key}`;
    }

    const dbPath = join(process.cwd(), ".byte", "data", `${key}.sqlite`);
    if (!existsSync(dbPath)) {
      return false;
    }
    try {
      const db = new Database(dbPath);
      db.query("CREATE TABLE IF NOT EXISTS data (value TEXT)").run();
      const result = db.query("SELECT value FROM data").get();
      db.close();
      return result !== null;
    } catch {
      return false;
    }
  },

  get: <V>(
    key: string,
    options?: FlashcoreOptions & { default?: unknown }
  ): V | null => {
    if (options?.namespace) {
      key = Array.isArray(options.namespace)
        ? `${options.namespace.join("/")}__${key}`
        : `${options.namespace}__${key}`;
    }

    const dbPath = join(process.cwd(), ".byte", "data", `${key}.sqlite`);

    if (!existsSync(dbPath)) {
      return (options?.default as V) ?? null;
    }

    try {
      const db = new Database(dbPath);

      db.query("CREATE TABLE IF NOT EXISTS data (value TEXT)").run();

      const result = db.query("SELECT value FROM data").get();
      db.close();

      if (result && typeof result === "object" && "value" in result) {
        return JSON.parse(result.value as string) as V;
      } else if (options?.default !== undefined) {
        return options.default as V;
      } else {
        return null as V;
      }
    } catch {
      if (options?.default !== undefined) {
        return options.default as V;
      } else {
        return null as V;
      }
    }
  },

  set: <V>(key: string, value: V, options?: FlashcoreOptions): boolean => {
    if (options?.namespace) {
      key = Array.isArray(options.namespace)
        ? `${options.namespace.join("/")}__${key}`
        : `${options.namespace}__${key}`;
    }

    const dbPath = join(process.cwd(), ".byte", "data", `${key}.sqlite`);

    try {
      mkdirSync(dirname(dbPath), { recursive: true });
      const db = new Database(dbPath);

      db.query("CREATE TABLE IF NOT EXISTS data (value TEXT)").run();
      db.query("DELETE FROM data").run();
      db.query("INSERT INTO data (value) VALUES (?)").run(
        JSON.stringify(value)
      );

      db.close();
      return true;
    } catch {
      return false;
    }
  },

  clear: (options?: FlashcoreOptions): boolean => {
    const dataPath = join(process.cwd(), ".byte", "data");
    try {
      if (options?.namespace) {
        const namespace = Array.isArray(options.namespace)
          ? options.namespace.join("/")
          : options.namespace;
        const namespacePath = join(dataPath, namespace);
        if (existsSync(namespacePath)) {
          for (const file of readdirSync(namespacePath)) {
            unlinkSync(join(namespacePath, file));
          }
          rmdirSync(namespacePath);
        }
      } else {
        if (existsSync(dataPath)) {
          for (const file of readdirSync(dataPath)) {
            const filePath = join(dataPath, file);
            if (existsSync(filePath)) {
              if (file.endsWith(".sqlite")) {
                unlinkSync(filePath);
              } else {
                rmdirSync(filePath, { recursive: true });
              }
            }
          }
        }
      }
      return true;
    } catch (error) {
      console.error("Error clearing data:", error);
      return false;
    }
  },
};