diff --git a/notebooks/code-bugger/openbugger_example.ipynb b/notebooks/code-bugger/openbugger_example.ipynb new file mode 100644 index 00000000..22e9b0c8 --- /dev/null +++ b/notebooks/code-bugger/openbugger_example.ipynb @@ -0,0 +1,278 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# First, we'll import the necessary libraries\n", + "import os\n", + "import subprocess" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## only run from OpenAssistant not if you alread cloned the github\n", + "# Next, we'll define the function that will clone the OpenBugger repository and install it.\n", + "def install_openbugger():\n", + " # First, we'll get the current working directory. This is where the repository will be cloned to.\n", + " cwd = os.getcwd()\n", + "\n", + " # Next, we'll use Git to clone the repository.\n", + " subprocess.run([\"git\", \"clone\", \"https://github.com/furlat/OpenBugger\", cwd + \"/OpenBugger\"])\n", + "\n", + " # Now, we'll use pip to install the package from the local repository.\n", + " subprocess.run([\"python3\", \"-m\", \"pip\", \"install\", \"--editable\", cwd + \"/OpenBugger\"])\n", + "\n", + "\n", + "# Now, we'll call the function to install OpenBugger.\n", + "install_openbugger()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Finally, we'll import SyntaxBug and LogicBug.\n", + "from syntax.syntax_injector import SyntaxBug\n", + "from logic.logic_injector import LogicBug" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# setup syntax bug\n", + "syntax_bug = SyntaxBug()\n", + "\n", + "# Simple script\n", + "simple_script = \"\"\"\n", + "def greet(name):\n", + " print(\"Hello, \" + name)\n", + "\n", + "greet(\"Bob\")\n", + "\"\"\"\n", + "\n", + "# The simple script can be modified using the \"easy\" injection method because it only contains simple syntax and does not have any nested code blocks. This means that there are fewer characters (e.g. quotes, brackets, braces, parenthesis) that could be the target of syntax errors, and the \"easy\" injection method, which only injects errors that involve replacing or removing a single character, is sufficient to modify the script.\n", + "print(simple_script)\n", + "# Inject easy syntax errors into the simple script\n", + "\n", + "modified_simple_script, errors, counter = syntax_bug.inject(simple_script, \"easy\", 1)\n", + "print(\"Modified version Easy\", errors, counter)\n", + "print(modified_simple_script)\n", + "print(\"are they the same?\", simple_script == modified_simple_script)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Medium script\n", + "medium_script = \"\"\"\n", + "def greet(name):\n", + " print(\"Hello, \" + name)\n", + " \n", + "def greet_all(names):\n", + " for name in names:\n", + " greet(name)\n", + " \n", + "greet_all([\"Bob\", \"Alice\", \"Eve\"])\n", + "\"\"\"\n", + "# The medium script can be modified using the \"medium\" injection method because it contains a nested code block (the for loop in the `greet_all` function). This means that there are more characters (e.g. quotes, brackets, braces, parenthesis) that could be the target of syntax errors, and the \"medium\" injection method, which injects errors that involve replacing, removing, or adding a single character, is sufficient to modify the script.\n", + "print(medium_script)\n", + "# Inject medium syntax errors into the medium script\n", + "modified_medium_script, errors, counter = syntax_bug.inject(medium_script, \"medium\", 3)\n", + "print(\"Modified version Medium\", errors, counter)\n", + "print(modified_medium_script)\n", + "print(\"are they the same?\", medium_script == modified_medium_script)\n", + "# Hard script\n", + "hard_script = \"\"\"\n", + "class Greeting:\n", + " def __init__(self, greeting):\n", + " self.greeting = greeting\n", + " \n", + " def greet(self, name):\n", + " print(self.greeting + \", \" + name)\n", + " \n", + "greeting = Greeting(\"Hello\")\n", + "greeting.greet(\"Bob\")\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Hard script\n", + "hard_script = \"\"\"\n", + "class Greeting:\n", + " def __init__(self, greeting):\n", + " self.greeting = greeting\n", + " \n", + " def greet(self, name):\n", + " print(self.greeting + \", \" + name)\n", + " \n", + "greeting = Greeting(\"Hello\")\n", + "greeting.greet(\"Bob\")\n", + "\"\"\"\n", + "\n", + "# The hard script can be modified using the \"hard\" injection method because it contains multiple nested code blocks (the `__init__` and `greet` methods in the `Greeting` class). This means that there are even more characters (e.g. quotes, brackets, braces, parenthesis) that could be the target of syntax errors, and the \"hard\" injection method, which injects errors that involve replacing, removing, adding, or swapping characters, is sufficient to modify the script.\n", + "print(hard_script)\n", + "# Inject hard syntax errors into the hard script\n", + "modified_hard_script, errors, counter = syntax_bug.inject(hard_script, \"hard\", 3)\n", + "print(\"Modified version Hard\", errors, counter)\n", + "print(modified_hard_script)\n", + "print(\"are they the same?\", hard_script == modified_hard_script)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we switch to testing the LogicBugs and show how to bug a function that is defined in the script without already having the string." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import inspect\n", + "import random\n", + "\n", + "# Simple example script\n", + "\n", + "\n", + "def simple_script():\n", + " # Choose two random integers\n", + " num1 = random.randint(0, 10)\n", + " num2 = random.randint(0, 10)\n", + "\n", + " # Compare the two numbers and print a message based on their relation\n", + " if num1 > num2:\n", + " print(\"num1 is greater than num2\")\n", + " elif num1 < num2:\n", + " print(\"num1 is less than num2\")\n", + " else:\n", + " print(\"num1 is equal to num2\")\n", + "\n", + "\n", + "# Medium example script\n", + "def medium_script():\n", + " # Choose a random integer and assign it to a variable\n", + " num = random.randint(0, 10)\n", + "\n", + " # Use a loop to print all numbers from 0 to the chosen integer\n", + " for i in range(num):\n", + " print(i)\n", + "\n", + "\n", + "# Hard example script\n", + "def hard_script():\n", + " # Choose a random integer and assign it to a variable\n", + " num = random.randint(0, 10)\n", + "\n", + " # Use a loop to print the square of all numbers from 0 to the chosen integer\n", + " for i in range(num):\n", + " print(i**2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# create an instance of the LogicBug class\n", + "logic_bug = LogicBug()\n", + "\n", + "# get the source code of the simple_script function as a string\n", + "simple_script_str = inspect.getsource(simple_script)\n", + "print(\"Simple\", simple_script_str)\n", + "# inject a logic error into the simple_script function\n", + "modified_simple_script, error, counter = logic_bug.inject(simple_script_str, \"easy\", num_errors=3)\n", + "print(\"Modified version Simple\", error, counter)\n", + "# print the modified simple_script function\n", + "print(modified_simple_script)\n", + "print(\"are they the same?\", simple_script_str == modified_simple_script)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# get the source code of the medium_script function as a string\n", + "medium_script_str = inspect.getsource(medium_script)\n", + "print(\"Medium\", medium_script_str)\n", + "# inject a logic error into the medium_script function\n", + "modified_medium_script, error, counter = logic_bug.inject(medium_script_str, \"medium\", num_errors=3)\n", + "\n", + "# print the modified medium_script function\n", + "print(\"Modified version Medium\", error, counter)\n", + "print(modified_medium_script)\n", + "print(\"are they the same?\", medium_script_str == modified_medium_script)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# get the source code of the hard_script function as a string\n", + "hard_script_str = inspect.getsource(hard_script)\n", + "print(\"Hard\", hard_script_str)\n", + "# inject a logic error into the hard_script function\n", + "modified_hard_script, error, counter = logic_bug.inject(hard_script_str, \"hard\", num_errors=1)\n", + "print(\"Modified version Hard\", error, counter)\n", + "# print the modified hard_script function\n", + "print(modified_hard_script)\n", + "print(\"are they the same?\", hard_script_str == modified_hard_script)" + ] + } + ], + "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.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "ceba285e8b4e6478fe8ad229bc63940a90ad5cf3d143521e7c38823a2e915b21" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/code-bugger/openbugger_example.md b/notebooks/code-bugger/openbugger_example.md new file mode 100644 index 00000000..d8611dd7 --- /dev/null +++ b/notebooks/code-bugger/openbugger_example.md @@ -0,0 +1,92 @@ +# OpenBuggerNotebook + +https://github.com/furlat/OpenBugger/blob/main/README.md is a Python package +that allows you to inject syntax and logic errors into your code. This can be +useful for testing the robustness of your code or for creating test cases for +debugging exercises or for training an assistant to debug code. + +The Python notebook openbugger_example.ipynb does the following: + +1. Imports the necessary libraries to install OpenBugger in the notebookdirecory + (os and subprocess). +2. Defines a function, install_openbugger, which clones the OpenBugger + repository from GitHub and installs it using pip. +3. Calls the install_openbugger function to install OpenBugger. +4. Imports the SyntaxBug and LogicBug classes from the syntax_injector and + logic_injector modules, respectively. +5. Creates an instance of the SyntaxBug class and assigns it to the syntax_bug + variable. +6. Defines three scripts: a simple script, a medium script, and a hard script. +7. Calls the inject method on the simple script, passing in the string "easy" as + the second argument and the integer 1 as the third argument. This will inject + easy syntax errors into the script. The *item *item modified script, a list + of the injected errors, and the number of errors injected are returned and + assigned to variables. +8. Prints the original and modified versions of the simple script, as well as + the list of injected errors and the number of errors injected. 10 Repeats + steps 7 and 8 for the medium and hard scripts, but with the "medium" and + "hard" injection methods and different numbers of errors to inject. + +General Usage To use OpenBugger, import the SintaxBug or LogicBug classes from +the openbugger module and use them to inject a bug with a call to the inject(). +The injector will return the modified script with the injected bug. + +``` +from syntax.syntax_injector import SyntaxInjector, SyntaxBug + +syntax_bug = SyntaxBug() + + + +# Simple script +simple_script = """ +def greet(name): + print("Hello, " + name) + +greet("Bob") +""" + +print(simple_script) +``` + +The simple script can be modified using the "easy" injection method because it +only contains simple syntax and does not have any nested code blocks. This means +that there are fewer characters (e.g. quotes, brackets, braces, parenthesis) +that could be the target of syntax errors, and the "easy" injection method, +which only injects errors that involve replacing or removing a single character, +is sufficient to modify the script. + +``` +# Inject easy syntax errors into the simple script + +modified_simple_script, errors, counter = syntax_bug.inject(simple_script, "easy", 1) +print("Modified version Easy",errors,counter) +print(modified_simple_script) +``` + +Or for higher severity and logic error by directly transforming a Python class +into text + +``` +import inspect +import random +from logic.logic_injector import LogicBug + + +# Medium example script +def medium_script(): + # Choose a random integer and assign it to a variable + num = random.randint(0, 10) + + # Use a loop to print all numbers from 0 to the chosen integer + for i in range(num): + print(i) + +# create an instance of the LogicBug class +logic_bug = LogicBug() +# get the source code of the medium_script function as a string +medium_script_str = inspect.getsource(medium_script) +print("Medium",medium_script_str) +# inject a logic error into the medium_script function +modified_medium_script, error, counter = logic_bug.inject(medium_script_str,"medium",num_errors=3) +```