Quick Start
The examples directory includes a simple example of using PyLLMut. This page provides step-by-step instructions for running this example.
Example: Triangle Area
As shown in Figure 1,
Triangle Area, in the
examples/triangle_area
directory, is a simple project with two packages:
code and tests.
The code package contains the project’s source code,
which consists of two Python modules:
equilateral.py and isosceles.py,
which compute the area of equilateral and
isosceles triangles, respectively.
The tests package contains the project’s test suite,
including two test modules:
test_equilateral.py and test_isosceles.py,
which test the equilateral.py and isosceles.py modules,
respectively.
In this example, we use PyLLMut to generate
mutants for equilateral.py, shown in Figure 2.
triangle_area/
├── code/
│ ├── __init__.py
│ ├── equilateral.py
│ └── isosceles.py
└── tests/
├── __init__.py
├── test_equilateral.py
└── test_isosceles.py
| Figure 2: Implementation of 'equilateral.py', a function that computes the area of an equilateral triangle. | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 | |
Setup Instructions
To start using PyLLMut with this example, we first need to set up the required environment by following these steps:
-
Clone the PyLLMut repository:
git clone git@github.com:mohrez86/pyllmut.git -
Copy the
examplesdirectory to a location of your choice (e.g., your home directory):cp -r pyllmut/examples ~/pyllmut_examples -
Navigate to the
examplesdirectory:cd ~/pyllmut_examples -
Create a Python virtual environment using the following command. On some machines (e.g., MacBooks), you may need to use
python3instead ofpython. Note that creating a Python virtual environment is optional in this example, but it is recommended to set up a dedicated Python environment for each project.python -m venv env -
Activate the virtual environment created in the previous step (if created):
source env/bin/activate -
Install PyLLMut by following the instructions in the Installation Guide.
How to Use PyLLMut
PyLLMut is a Python library. You can use it by importing it into a Python program and calling its API. The example in Figure 3 demonstrates how to use PyLLMut to generate mutants for a given Python module.
| Figure 3: 'pyllmut_triangle_area.py', a script demonstrating how to use PyLLMut to generate mutants for 'equilateral.py'. | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
You can also find this code in the examples/pyllmut_triangle_area.py file and run it using the following steps:
Running the Example
-
Navigate to the
examplesdirectory:cd ~/pyllmut_examples -
Activate the virtual environment (if created):
source env/bin/activate -
Run
pyllmut_triangle_area.py:python pyllmut_triangle_area.py
Explanation of pyllmut_triangle_area.py
The script in Figure 3 follows these steps:
Imports
- Line 1 imports
loggingfor debugging and status updates (lines 1 and 6 are optional). - Line 2 imports
Pathfrompathlibfor reading the module file. - Line 4 imports
MutantGeneratorandModelTypefrom PyLLMut.
Setup and Configuration
- Lines 8 and 10 define
module_pathand read its content. - Lines 11-14 specify:
lines_to_mutate: A list of line numbers where mutants should be generated (7, 10, and 11).mutants_per_line: The number of mutants to generate for each line (3).timeout_per_line: The mutant generation timeout for each line (15 seconds).model_type: The LLM model used (GPT-4o).
Mutation Generation
- Lines 16-18 initialize a
MutantGeneratorobject. - Line 19 calls the
generate()method, which generates the mutants and returns aMutationReportobject. - Line 20 retrieves a list of valid mutants using
get_valid_mutant_list().
Output Processing
- Line 22 prints the total number of valid mutants PyLLMut generated.
- Lines 24-34 print details of the first valid mutant, including:
- The mutated line number.
- A diff representation of the mutation.
- The modified module content.
For more details about PyLLMut’s API, refer to the API Reference page.
Sample Output
Figure 4 shows an example of running pyllmut_triangle_area.py.
Lines 1-9 display logging messages, which can be disabled by removing
logging.basicConfig(...) in Figure 3.
| Figure 4: Sample output of running 'pyllmut_triangle_area.py', displaying logging messages and the first valid mutant generated. | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |