File size: 2,693 Bytes
ad552d8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
{
"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": 3,
"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(\"mcmc\")"
]
},
{
"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
}
|