Pyleet is a Python tool that allows you to run and test your LeetCode Python solutions locally with minimal modification. It bridges the gap between LeetCode's online environment and local development, making it easier to debug and verify your solutions offline.
- Local Testing: Run LeetCode Python solutions locally without modifying code.
- Test Case Flexibility: Support for
.txtand.jsontest case files. - Intuitive CLI: Run tests easily with commands like
pyleet solution.py --testcases cases.txt. - Smart Method Detection: Automatically selects methods based on input types or allows explicit selection with
--method. - Built-in Data Structures: Includes
ListNodeandTreeNodewith automatic serialization, eliminating boilerplate code. - Customizable Execution: Choose automatic fallback, explicit imports, or custom class overrides for flexible usage.
- Broad Data Support: Seamlessly handles lists, integers, strings, and custom classes with bidirectional serialization.
- Clear Error Reporting: Provides detailed feedback and comparison for accurate test results.
- Debugging Support: Displays
print()output from solutions in test results for easier debugging. - Programmatic Interface: Run tests directly from Python code with
pyleet.run()for better integration. - Auto Test Case Retrieval: Automatically fetch test cases from LeetCode. (Learn more)
pip install pyleet
git clone https://github.com/ergs0204/pyleet.git
cd pyleet
pip install -e .Run your LeetCode solution against a test case file using the CLI:
pyleet your_solution_file.py --testcases test_cases.txtFor example, if your solution is in solution.py and test cases in cases.txt:
pyleet solution.py -t cases.txtPyleet also supports programmatic usage. For example
import pyleet
class Solution:
def twoSum(self, nums, target):
pass
testcases=pyleet.get_testcase(problem_id=1)
results = pyleet.run(testcases, method="twoSum")
pyleet.print_results(results)For more in-depth information on specific features and usage, please refer to the following documents:
- Programmatic Usage: Learn how to integrate Pyleet into your Python scripts and debugging workflows.
- CLI Usage: Explore advanced command-line options and test case formats.
- Auto Test Case Retrieval: Discover how to fetch test cases directly from LeetCode.
- Method Selection: Understand how Pyleet handles solutions with multiple methods.
- Custom Class Support: Guide for working with custom data structures.
- Dictionary Handling Guide: Learn how Pyleet handles dictionaries as inputs and outputs.
| Feature | CLI Approach | Programmatic Approach |
|---|---|---|
| Test Case Storage | External files (.txt, .json) |
Python code |
| IDE Integration | Limited | Full autocomplete/debugging |
| Debugging | Terminal only | IDE debugger integration |
| Method Selection | --method flag |
method parameter |
| Output | Printed in terminal | Captured in variables |
| Automation | CI / Shell scripts | Python / Notebooks |
| Best For | Quick tests, CI/CD | Development, notebooks, IDE use |
Use Programmatic Interface when:
- Developing and debugging in an IDE
- Working in Jupyter notebooks
- Need tight integration with Python workflows
- Want to process test results programmatically
- Prefer keeping tests close to solution code
Use CLI when:
- Quick testing of solutions
- CI/CD pipelines
- Sharing test cases with others
- Working with large test suites in files
- Loads your solution file dynamically
- Loads and parses the test cases from the external file
- Converts inputs into Python data structures
- Calls your solution method with the inputs
- Compares the output to the expected result
- Reports pass/fail status for each test case
- Any
print()output from your solution will be shown in test result, helping with step-by-step debugging
Pyleet supports both built-in and custom data structures commonly used in LeetCode problems, such as ListNode, TreeNode, or your own custom classes like Point or Matrix.
Pyleet includes built-in ListNode and TreeNode classes that match LeetCode specifications.
- ✅ Zero boilerplate – No need to copy-paste class definitions into your solution
- ✅ Flexible – Use built-in classes or override with your own if needed
- ✅ Standard compliant – Compatible with LeetCode's input/output formats
- ✅ Automatic serialization – Input/output conversion is handled seamlessly
This section provides a brief overview. For full details, see the Custom Classes Guide.
Pyleet allows full support for custom data types and complex class structures. You can:
- Define and register your own classes
- Create custom serializers and deserializers
- Use automatic or explicit method selection with type-based heuristics
- Build reusable and maintainable test cases using JSON-based formats
- ✅ Testcases fetching - Fetch testcases with
pyleet.get_testcase(). - ✅ Programmatic Interface - Run tests directly from Python code with
pyleet.run()for better IDE integration - ✅ Built-in ListNode and TreeNode classes - Zero configuration needed for common LeetCode problems
- ✅ Three usage patterns - Automatic fallback, explicit import, or custom override
- ✅ Enhanced custom class support - Any class structure now supported
- ✅ Fixed serialization errors - No more
valattribute requirements - ✅ Flexible method selection - Both automatic selection and explicit method specification via
--methodparameter - ✅ Bidirectional serialization - Both input and output serialization support
- ✅ Robust comparison - Multiple fallback strategies for output comparison
- ✅ Improved error handling - Clear feedback when specified methods are not found
- Integration with testing frameworks (pytest, unittest)
- Support for more data structures.
- Run tests in parallel.
- Record running time.
- Performance optimizations for large test suites
- Better documentation and examples.
For detailed information on contributing, see Contributing.