This is a code translator tool that uses OpenAI's GPT-5 model to translate code from Python to R or from R to Python. It also generates an explanation of the translated code, focusing on how to use it.
- Requirements
- Setting up OpenAI Developer Account and API Key
- Installation
- Usage
- Command Line Options
- Examples
- Functions
- Changelog
- Disclaimer
- Python 3.6 or later
- OpenAI Python package:
pip install openai - OpenAI Developer Account and API key
- Visit the OpenAI website at https://www.openai.com/.
- Click on the "Sign Up" button and create an account.
- After verifying your email address and logging in, navigate to the API section.
- In the API section, you will find your API key. Copy this key and keep it safe.
- Set your API key as an environment variable:
export OPENAI_API_KEY=your_api_key_here
- Clone or download this repository
- Install the required dependencies:
pip install openai
- Set your OpenAI API key as an environment variable (see above)
The script accepts a Python (.py) or R (.r/.R) file and translates it to the opposite language.
Basic usage:
python py2r.py <input_file>filename- Required. Input file to translate (.py or .r/.R)-o, --output- Optional. Specify output filename (default: input_translated.ext)--no-explain- Optional. Skip the code explanation step-v, --verbose- Optional. Enable verbose output with debug information-q, --quiet- Optional. Suppress all output except errors-h, --help- Show help message and exit
# Translate Python to R
python py2r.py my_script.py
# Translate R to Python
python py2r.py my_script.rpython py2r.py script.py -o converted_script.Rpython py2r.py script.py --no-explainpython py2r.py script.py -vpython py2r.py script.py -qpython py2r.py script.py -o new_script.R --no-explain -vValidates that the OPENAI_API_KEY environment variable is set.
Translates code from source language to target language using OpenAI's GPT-5 model.
Arguments:
client: OpenAI client instancecode: The code to be translated as a stringsource_language: Source programming language ("Python" or "R")target_language: Target programming language ("Python" or "R")
Returns:
- String containing the translated code
Generates an explanation of the translated code using OpenAI's GPT-5 model.
Arguments:
client: OpenAI client instancecode: The code to be explained as a string
Returns:
- None. The explanation is printed to the console.
Determines source/target languages and output filename based on input file extension.
Reads and returns contents of the input file with proper error handling.
Writes translated code to the output file with proper error handling.
Configures logging based on verbosity settings.
- Security: Now uses environment variables for API key instead of hardcoding
- Modernization: Updated to newer OpenAI client library with proper error handling
- Efficiency: Removed redundant file operations for better performance
- Robustness: Added case-insensitive file extension handling (.r, .R, .py)
- Improved: Better markdown removal using robust regex patterns
- CLI: Complete rewrite with argparse for professional command-line interface
- Features: Added --no-explain option to skip code explanation
- Logging: Implemented proper logging system with verbose (-v) and quiet (-q) modes
- Customization: Added custom output filename support with -o/--output option
- Error Handling: Comprehensive error messages with helpful guidance
- File Support: Added UTF-8 encoding support for better international character handling
- Code Quality: Modular function structure for better maintainability
- Basic translation between Python and R
- Simple command-line interface
- Code explanation feature
- Hardcoded API key (security issue)
The OpenAI GPT-5 model is a powerful tool but it is not infallible. It may not always generate perfect translations or explanations, particularly for complex or poorly written code. Always review the generated code and explanations, and use your own judgment. Test translated code thoroughly before using in production environments.