You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
7.5 KiB
Plaintext

2 years ago
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# A bot in the shell (with memory)\n",
"\n",
"This bot has one extra feature: it can save information to a database. The database is a JSON file."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
">>> count\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<<< 6\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
">>> hello\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<<< What did you say?\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
">>> count\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<<< 7\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "Interrupted by user",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-17-72e501df7c33>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 52\u001b[0;31m \u001b[0mmessage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\">>>\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmessage\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"exit\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.7/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 861\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 862\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 863\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 864\u001b[0m )\n\u001b[1;32m 865\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.7/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 902\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 903\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 904\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Interrupted by user\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 905\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 906\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid Message:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
]
}
],
"source": [
"import os\n",
"import json\n",
"\n",
"# --- database functions ---\n",
"def write_db(this_db):\n",
" with open(db_filename, 'w+') as f:\n",
" f.write(json.dumps(this_db, indent=4, sort_keys=True))\n",
" \n",
"def load_db():\n",
" # Create the database when it does not exist yet\n",
" if not os.path.exists(db_filename):\n",
" new_db = {}\n",
" write_db(new_db)\n",
" with open(db_filename, 'r') as f:\n",
" db = json.loads(f.read())\n",
" \n",
" return db\n",
"\n",
"def read_db(key):\n",
" if key in db:\n",
" return db[key]\n",
" else:\n",
" return None\n",
"\n",
"def update_db(key, value):\n",
" db[key] = value\n",
" write_db(db)\n",
"# -------------------------\n",
"\n",
"def get_reply(message):\n",
" if \"hello\" in message:\n",
" reply = \"oh hello!\"\n",
" # --- database example --- \n",
" if \"count\" in message:\n",
" # First check if there is a counter already,\n",
" # and if not, make one\n",
" if read_db(\"count\") == None:\n",
" current_count = update_db(\"count\", 0)\n",
" current_count = read_db(\"count\") \n",
" new_count = current_count + 1\n",
" update_db(\"count\", new_count)\n",
" reply = new_count\n",
" # ------------------------\n",
" else:\n",
" reply = None\n",
" \n",
" return reply\n",
"\n",
"# --- database settings ---\n",
"db_filename = 'shell-bot.json'\n",
"db = load_db()\n",
"# -------------------------\n",
"\n",
"while True:\n",
" message = input(\">>>\")\n",
" if message == \"exit\":\n",
" break\n",
" \n",
" reply = get_reply(message)\n",
" \n",
" if reply: \n",
" print(\"<<<\", reply)\n",
" else:\n",
" print(\"<<<\", \"What did you say?\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run the bot from a terminal\n",
"\n",
"You can also save this code as a `.py` script, and run it from the terminal.\n",
"\n",
"1. Make a new file and save it as a `.py` script\n",
"2. Open a new terminal \n",
"3. Navigate to the folder where you saved your `.py` script\n",
"4. Run your bot: `$ python3 YOURFILENAME.py`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}