Welcome to your guide on using PolyglotPiranha, a lightweight yet powerful code transformation toolset designed to automate large-scale changes in codebases. This guide will take you through the installation process, give you an example of its usage, and provide troubleshooting tips to smooth your journey.
Installation
PolyglotPiranha can be installed either as a Python library for those who prefer coding in Python, or as a command-line tool for a more direct interface. Let’s dive into both installation methods.
Python API
To install the Python API, simply run the following command in your command line:
pip install polyglot-piranha
Command-line Interface
For a command-line interface, follow these steps:
- Install Rust.
- Clone the repository with the command:
git clone https://github.com/uber/piranha.git - Navigate to the cloned directory:
cd piranha - Build the project using:
cargo build --release(orcargo build --release --no-default-featuresfor macOS). - The binary will be generated under
target/release.
Example Usage
Now that you’ve installed PolyglotPiranha, let’s see it in action. The following analogy will help you understand the coding process:
Imagine you’re a gardener, responsibly trimming the overgrown branches of your plants. In this case, your plants are the lines of code with stale feature flags. Just as you need to know which branches to cut and how to prune them carefully, PolyglotPiranha requires instructions about the code modifications to be made.
Here’s how you execute a transformation:
from polyglot_piranha import execute_piranha, PiranhaArguments, Rule, RuleGraph, OutgoingEdges
# Original code snippet
code = """if (obj.isLocEnabled() x 0)
do something else
do something else!"""
# Define the rule to replace the method call
r1 = Rule(
name=replace_method,
query=cs :[x].isLocEnabled(),
replace_node=*,
replace=true,
is_seed_rule=True
)
# Define the edges for the rule graph.
edge = OutgoingEdges(replace_method, to=[boolean_literal_cleanup], scope=parent)
# Create Piranha arguments
piranha_arguments = PiranhaArguments(
code_snippet=code,
language=java,
rule_graph=RuleGraph(rules=[r1], edges=[edge])
)
# Execute Piranha and print the transformed code
piranha_summary = execute_piranha(piranha_arguments)
print(piranha_summary[0].content)
This code snippet defines a rule to refactor stale code gracefully, akin to your careful trimming of branches, ensuring only the necessary parts of code remain. After executing, you’ll find your code refreshed and precise!
Understanding Feature Flags
Feature flags are markers in your code that help you control where and when features are available. However, over time, some flags can become stale—similar to wearing outdated fashion. This can lead to:
- Increased code clutter, slowing down maintenance.
- Interference with other experimental flags, causing confusion.
- Unused code lingering in the source, leading to potential bugs.
PolyglotPiranha steps in to prune these stale flags automatically, vastly improving developer productivity and ensuring a clearer codebase.
Troubleshooting
If you encounter issues or have questions while using PolyglotPiranha, don’t hesitate to take action! You can report any bugs by opening a GitHub issue. Here are some common troubleshooting tips:
- Ensure Rust is properly installed, as it’s required for the command-line interface.
- Check that you cloned the repository correctly and are in the right directory.
- Make sure dependencies are properly set up if using Python, especially if you face import errors.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Happy coding, and let PolyglotPiranha streamline your code transformation journey!

