From 5b9cdcec4870a1d651d6f18e56fd2fc34c26ac27 Mon Sep 17 00:00:00 2001 From: furlat Date: Thu, 5 Jan 2023 20:19:31 +0100 Subject: [PATCH 1/4] Create openbugger_example.ipynb added an example for open_bugger --- .../code-bugger/openbugger_example.ipynb | 273 ++++++++++++++++++ 1 file changed, 273 insertions(+) create mode 100644 notebooks/code-bugger/openbugger_example.ipynb diff --git a/notebooks/code-bugger/openbugger_example.ipynb b/notebooks/code-bugger/openbugger_example.ipynb new file mode 100644 index 00000000..2416e42a --- /dev/null +++ b/notebooks/code-bugger/openbugger_example.ipynb @@ -0,0 +1,273 @@ +{ + "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", + "# Now, we'll call the function to install OpenBugger.\n", + "install_openbugger()\n" + ] + }, + { + "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\n" + ] + }, + { + "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)\n" + ] + }, + { + "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", + "# Simple example script\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", + "# 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", + "# 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)\n" + ] + }, + { + "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)\n" + ] + } + ], + "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.8.2 (default, Apr 8 2021, 23:19:18) \n[Clang 12.0.5 (clang-1205.0.22.9)]" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 51739fa5b72d08e4cbc21ca81a17a0bb5e54542f Mon Sep 17 00:00:00 2001 From: furlat Date: Thu, 5 Jan 2023 20:33:04 +0100 Subject: [PATCH 2/4] Update openbugger_example.ipynb --- .../code-bugger/openbugger_example.ipynb | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/notebooks/code-bugger/openbugger_example.ipynb b/notebooks/code-bugger/openbugger_example.ipynb index 2416e42a..ec8583d1 100644 --- a/notebooks/code-bugger/openbugger_example.ipynb +++ b/notebooks/code-bugger/openbugger_example.ipynb @@ -22,15 +22,16 @@ "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", + "\n", " # Next, we'll use Git to clone the repository.\n", " subprocess.run([\"git\", \"clone\", \"https://github.com/furlat/OpenBugger\", cwd + \"/OpenBugger\"])\n", - " \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", + "\n", "# Now, we'll call the function to install OpenBugger.\n", - "install_openbugger()\n" + "install_openbugger()" ] }, { @@ -41,7 +42,7 @@ "source": [ "# Finally, we'll import SyntaxBug and LogicBug.\n", "from syntax.syntax_injector import SyntaxBug\n", - "from logic.logic_injector import LogicBug\n" + "from logic.logic_injector import LogicBug" ] }, { @@ -50,7 +51,7 @@ "metadata": {}, "outputs": [], "source": [ - "#setup syntax bug\n", + "# setup syntax bug\n", "syntax_bug = SyntaxBug()\n", "\n", "# Simple script\n", @@ -66,9 +67,9 @@ "# 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 version Easy\", errors, counter)\n", "print(modified_simple_script)\n", - "print(\"are they the same?\",simple_script == modified_simple_script)" + "print(\"are they the same?\", simple_script == modified_simple_script)" ] }, { @@ -88,13 +89,13 @@ " \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", + "# 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 version Medium\", errors, counter)\n", "print(modified_medium_script)\n", - "print(\"are they the same?\",medium_script == modified_medium_script)\n", + "print(\"are they the same?\", medium_script == modified_medium_script)\n", "# Hard script\n", "hard_script = \"\"\"\n", "class Greeting:\n", @@ -132,9 +133,9 @@ "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 version Hard\", errors, counter)\n", "print(modified_hard_script)\n", - "print(\"are they the same?\",hard_script == modified_hard_script)\n" + "print(\"are they the same?\", hard_script == modified_hard_script)" ] }, { @@ -153,13 +154,15 @@ "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", + "\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", @@ -168,20 +171,22 @@ " 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", + "\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", + "\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)" @@ -198,13 +203,13 @@ "\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", + "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", + "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)\n" + "print(\"are they the same?\", simple_script_str == modified_simple_script)" ] }, { @@ -215,14 +220,14 @@ "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", + "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", + "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 version Medium\", error, counter)\n", "print(modified_medium_script)\n", - "print(\"are they the same?\",medium_script_str == modified_medium_script)" + "print(\"are they the same?\", medium_script_str == modified_medium_script)" ] }, { @@ -233,13 +238,13 @@ "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", + "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", + "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)\n" + "print(\"are they the same?\", hard_script_str == modified_hard_script)" ] } ], From bb66f5d750622aa10d6431acfa425a92addaf3b3 Mon Sep 17 00:00:00 2001 From: furlat Date: Thu, 5 Jan 2023 23:09:01 +0100 Subject: [PATCH 3/4] Added md for openbugger_example --- .../code-bugger/openbugger_example.ipynb | 4 +- notebooks/code-bugger/openbugger_example.md | 70 +++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 notebooks/code-bugger/openbugger_example.md diff --git a/notebooks/code-bugger/openbugger_example.ipynb b/notebooks/code-bugger/openbugger_example.ipynb index ec8583d1..22e9b0c8 100644 --- a/notebooks/code-bugger/openbugger_example.ipynb +++ b/notebooks/code-bugger/openbugger_example.ipynb @@ -264,12 +264,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.2 (default, Apr 8 2021, 23:19:18) \n[Clang 12.0.5 (clang-1205.0.22.9)]" + "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": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + "hash": "ceba285e8b4e6478fe8ad229bc63940a90ad5cf3d143521e7c38823a2e915b21" } } }, diff --git a/notebooks/code-bugger/openbugger_example.md b/notebooks/code-bugger/openbugger_example.md new file mode 100644 index 00000000..0ce05de7 --- /dev/null +++ b/notebooks/code-bugger/openbugger_example.md @@ -0,0 +1,70 @@ +# 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. +5) Imports the SyntaxBug and LogicBug classes from the syntax_injector and logic_injector modules, respectively. +6) Creates an instance of the SyntaxBug class and assigns it to the syntax_bug variable. +7) Defines three scripts: a simple script, a medium script, and a hard script. +8) 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. +9) 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) +``` \ No newline at end of file From f7ee8fd74bd4da2198e286b769c3466101845f1c Mon Sep 17 00:00:00 2001 From: furlat Date: Thu, 5 Jan 2023 23:10:34 +0100 Subject: [PATCH 4/4] pre_commit openbugger md --- notebooks/code-bugger/openbugger_example.md | 62 ++++++++++++++------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/notebooks/code-bugger/openbugger_example.md b/notebooks/code-bugger/openbugger_example.md index 0ce05de7..d8611dd7 100644 --- a/notebooks/code-bugger/openbugger_example.md +++ b/notebooks/code-bugger/openbugger_example.md @@ -1,21 +1,35 @@ # 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. + +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. -5) Imports the SyntaxBug and LogicBug classes from the syntax_injector and logic_injector modules, respectively. -6) Creates an instance of the SyntaxBug class and assigns it to the syntax_bug variable. -7) Defines three scripts: a simple script, a medium script, and a hard script. -8) 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. -9) 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. +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. +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 @@ -31,12 +45,17 @@ def greet(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. + +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 @@ -44,7 +63,10 @@ modified_simple_script, errors, counter = syntax_bug.inject(simple_script, "easy 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 + +Or for higher severity and logic error by directly transforming a Python class +into text + ``` import inspect import random @@ -55,7 +77,7 @@ from logic.logic_injector import LogicBug 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) @@ -67,4 +89,4 @@ 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) -``` \ No newline at end of file +```