diff --git a/gamification/irc-bots.ipynb b/gamification/irc-bots.ipynb new file mode 100644 index 0000000..9a3cd54 --- /dev/null +++ b/gamification/irc-bots.ipynb @@ -0,0 +1,219 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# IRC Bots" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Making bots as a way to re(turn) to gamification mechanisms and find ways to subvert them.\n", + "\n", + "Could the prototyping session of today be a moment of collective digestion and testing? \\\n", + "A moment to bring back references from the last weeks and test ideas through making small prototypes?\n", + "\n", + "Links to start in the middle of:\n", + "\n", + "* https://pad.xpub.nl/p/18012022 - Pad of the session on \"Defining play and games, Blurrying the lines between labour and play: gamification and playbour\" \n", + "* https://pad.xpub.nl/p/2022_onesentencegameideas\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "## How to make an IRC bot?\n", + "\n", + "* **example script**: https://pzwiki.wdka.nl/mediadesign/Building_an_IRC_bot_in_python\n", + "* **IRC server** for this notebooks: https://libera.chat/\n", + "* **Python library**: irc" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true, + "tags": [] + }, + "outputs": [], + "source": [ + "! pip3 install irc" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## hellobot" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import irc.bot\n", + "\n", + "class HelloBot(irc.bot.SingleServerIRCBot):\n", + " def __init__(self, channel, nickname, server, port):\n", + " irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)\n", + " self.channel = channel\n", + "\n", + " def on_welcome(self, connection, event):\n", + " connection.join(self.channel)\n", + "\n", + " def on_privmsg(self, connection, event):\n", + " pass\n", + "\n", + " def on_pubmsg(self, connection, event):\n", + " # event.target, event.source, event.arguments, event.type\n", + " # print(event) # type: pubmsg, source: Guest92843793284!~Guest9284@2a10:3781:1734:1:fdd8:ab9a:d925:9106, target: #xpub, arguments: ['...'], tags: []\n", + " messages = event.arguments\n", + " for message in messages:\n", + " if \"hello\" in message:\n", + " connection.privmsg(self.channel, \"HELLO!!!\")\n", + "\n", + "server = \"irc.libera.chat\"\n", + "port = 6667\n", + "channel = \"#xpub\"\n", + "nickname = \"hellobot\"\n", + "\n", + "bot = HelloBot(channel, nickname, server, port)\n", + "bot.start()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## echobot" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import irc.bot\n", + "\n", + "class EchoBot(irc.bot.SingleServerIRCBot):\n", + " def __init__(self, channel, nickname, server, port):\n", + " irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)\n", + " self.channel = channel\n", + "\n", + " def on_welcome(self, connection, event):\n", + " connection.join(self.channel)\n", + "\n", + " def on_privmsg(self, connection, event):\n", + " pass\n", + "\n", + " def on_pubmsg(self, connection, event):\n", + " # event.target, event.source, event.arguments, event.type\n", + " # print(event) # type: pubmsg, source: Guest92843793284!~Guest9284@2a10:3781:1734:1:fdd8:ab9a:d925:9106, target: #xpub, arguments: ['...'], tags: []\n", + " messages = event.arguments\n", + " for message in messages:\n", + " connection.privmsg(self.channel, message)\n", + "\n", + "server = \"irc.libera.chat\"\n", + "port = 6667\n", + "channel = \"#xpub\"\n", + "nickname = \"echobot\"\n", + "\n", + "bot = EchoBot(channel, nickname, server, port)\n", + "bot.start()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## timerbot" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import irc.bot\n", + "import time \n", + "\n", + "class TimerBot(irc.bot.SingleServerIRCBot):\n", + " def __init__(self, channel, nickname, server, port):\n", + " irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)\n", + " self.channel = channel\n", + " \n", + " # This is where the last_timer is initiated\n", + " self.last_timer = time.time()\n", + "\n", + " def on_welcome(self, connection, event):\n", + " connection.join(self.channel)\n", + "\n", + " def on_privmsg(self, connection, event):\n", + " pass\n", + "\n", + " def on_pubmsg(self, connection, event):\n", + " # event.target, event.source, event.arguments, event.type\n", + " # print(event) # type: pubmsg, source: Guest92843793284!~Guest9284@2a10:3781:1734:1:fdd8:ab9a:d925:9106, target: #xpub, arguments: ['...'], tags: []\n", + " \n", + " # Make a new timestamp \n", + " now = time.time()\n", + " \n", + " # Compare \"now\" with the last_timer\n", + " timer = round((now - self.last_timer), 2)\n", + " \n", + " reply = f\"{ timer } seconds\"\n", + " connection.privmsg(self.channel, reply)\n", + " \n", + " # Reset the last_timer\n", + " self.last_timer = now\n", + "\n", + "server = \"irc.libera.chat\"\n", + "port = 6667\n", + "channel = \"#xpub\"\n", + "nickname = \"timerbot\"\n", + "\n", + "bot = TimerBot(channel, nickname, server, port)\n", + "bot.start()" + ] + }, + { + "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 +} diff --git a/gamification/printer-bots.ipynb b/gamification/printer-bots.ipynb new file mode 100644 index 0000000..6088444 --- /dev/null +++ b/gamification/printer-bots.ipynb @@ -0,0 +1,97 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from escpos.printer import Network\n", + "\n", + "printer = Network(\"192.168.1.140\") # Printer IP Address will be spit out when plugged\n", + "\n", + "printer.text(\"testing\") # Send a string to the printer\n", + "printer.cut() # Cut the paper" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "KeyboardInterrupt", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mescpos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprinter\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mNetwork\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprinter\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mNetwork\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"192.168.1.140\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Printer IP Address will be spit out when plugged\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_reply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessage\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[0;32m~/.local/lib/python3.7/site-packages/escpos/printer.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, host, port, timeout, *args, **kwargs)\u001b[0m\n\u001b[1;32m 191\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mport\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mport\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 192\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 193\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\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 194\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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[0;32m~/.local/lib/python3.7/site-packages/escpos/printer.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 197\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msocket\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mAF_INET\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSOCK_STREAM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 198\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msettimeout\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 199\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhost\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mport\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 200\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\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: " + ] + } + ], + "source": [ + "from escpos.printer import Network\n", + "\n", + "printer = Network(\"192.168.1.140\") # Printer IP Address will be spit out when plugged\n", + "\n", + "def get_reply(message):\n", + " if \"hello\" in message:\n", + " reply = \"oh hello!\"\n", + " else:\n", + " reply = None\n", + "\n", + " return reply\n", + " \n", + "while True:\n", + " message = input(\">>>\")\n", + " \n", + " printer.txt(message)\n", + " printer.cut()\n", + " \n", + " if message == \"exit\":\n", + " break\n", + " \n", + " reply = get_reply(message)\n", + " \n", + " if reply: \n", + " printer.txt(\"<<<\", reply)\n", + " printer.cut()\n", + " else:\n", + " printer.txt(\"<<<\", \"What did you say?\")\n", + " printer.cut()" + ] + }, + { + "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 +} diff --git a/gamification/shell-bot-with-memory.ipynb b/gamification/shell-bot-with-memory.ipynb new file mode 100644 index 0000000..3b7b574 --- /dev/null +++ b/gamification/shell-bot-with-memory.ipynb @@ -0,0 +1,183 @@ +{ + "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\u001b[0m in \u001b[0;36m\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 +} diff --git a/gamification/shell-bot-with-timer.ipynb b/gamification/shell-bot-with-timer.ipynb new file mode 100644 index 0000000..59f5900 --- /dev/null +++ b/gamification/shell-bot-with-timer.ipynb @@ -0,0 +1,193 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# A bot in the shell (with timer)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> hello\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< oh hello!\n", + "It took you 3.66 seconds to respond.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> hellooooo\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< oh hello!\n", + "It took you 3.04 seconds to respond.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> and now\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< What did you say?\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> and\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< What did you say?\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> hello\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< oh hello!\n", + "It took you 1.13 seconds to respond.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> hello\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< oh hello!\n", + "It took you 1.5 seconds to respond.\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\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;31m# -------------------\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\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 18\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 19\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 time\n", + "\n", + "def get_reply(message):\n", + " if \"hello\" in message:\n", + " reply = \"oh hello!\"\n", + " else:\n", + " reply = None\n", + "\n", + " return reply\n", + " \n", + "while True:\n", + " \n", + " # --- start timer ---\n", + " starttime = time.time()\n", + " # -------------------\n", + " \n", + " message = input(\">>>\")\n", + " if message == \"exit\":\n", + " break\n", + " \n", + " reply = get_reply(message)\n", + " \n", + " if reply: \n", + " print(\"<<<\", reply)\n", + " # --- check timer ---\n", + " timer = round((time.time() - starttime), 2)\n", + " print(f\"It took you { timer } seconds to respond.\")\n", + " # -------------------\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 +} diff --git a/gamification/shell-bot.ipynb b/gamification/shell-bot.ipynb new file mode 100644 index 0000000..78e3aa9 --- /dev/null +++ b/gamification/shell-bot.ipynb @@ -0,0 +1,158 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# A bot in the shell" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> hello\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< oh hello!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> and this\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< What did you say?\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> hello\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< oh hello!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> yes i don't know what to say\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< What did you say?\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> hello\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<<< oh hello!\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + ">>> exit\n" + ] + } + ], + "source": [ + "def get_reply(message):\n", + " if \"hello\" in message:\n", + " reply = \"oh hello!\"\n", + " else:\n", + " reply = None\n", + "\n", + " return reply\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 +}