From 51739fa5b72d08e4cbc21ca81a17a0bb5e54542f Mon Sep 17 00:00:00 2001 From: furlat Date: Thu, 5 Jan 2023 20:33:04 +0100 Subject: [PATCH] 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)" ] } ],