{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Print all submissions in Redis\n", "import os\n", "import redis\n", "import json\n", "import dotenv\n", "\n", "dotenv.load_dotenv()\n", "\n", "\n", "# Connect to Redis\n", "redis_client = redis.Redis(\n", " host=os.getenv(\"REDIS_HOST\"),\n", " port=os.getenv(\"REDIS_PORT\"),\n", " username=os.getenv(\"REDIS_USERNAME\"),\n", " password=os.getenv(\"REDIS_PASSWORD\"),\n", " decode_responses=True,\n", ")\n", "\n", "# Get all submissions\n", "submissions = redis_client.lrange(\"submissions\", 0, -1)\n", "\n", "# Print all key-value pairs\n", "for idx, submission in enumerate(submissions, 1):\n", " print(f\"Submission {idx}:\")\n", " data = json.loads(submission)\n", " for key, value in data.items():\n", " print(f\" {key}: {value}\")\n", " print()\n", "\n", "print(f\"Total submissions: {len(submissions)}\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Delete records by a name\n", "def delete_records_by_name(name):\n", " # Get all keys in Redis\n", " all_keys = redis_client.keys(\"*\")\n", "\n", " for redis_key in all_keys:\n", " # Check if the key is a list type (like your submissions list)\n", " if redis_client.type(redis_key) == \"list\":\n", " submissions = redis_client.lrange(redis_key, 0, -1)\n", " for submission in submissions:\n", " data = json.loads(submission)\n", " if data.get(\"name\") == name:\n", " redis_client.lrem(redis_key, 1, submission)\n", " print(f\"Deleted submission with name: {name}\")\n", "\n", "\n", "delete_records_by_name(\"some name\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Clear the Redis database\n", "redis_client.delete(\"submissions\")\n", "print(\"Redis database cleared.\")" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" } }, "nbformat": 4, "nbformat_minor": 2 }