A Python package to generate and analyze code using the Groq API. This package lets you quickly create code files from descriptive prompts and analyze existing Python code for metrics and best practices, making it ideal for rapid prototyping and AI-assisted development.
- Features
- Installation
- Usage
- Code Analysis
- Project Structure
- Configuration
- Development & Testing
- Contributing
- License
- Easy Code Generation: Generate code based on natural language prompts
- Code Analysis: Analyze Python code for metrics, complexity, and best practices
- Detailed Reports: Generate markdown reports with code analysis insights
- File Management: Write generated code and analysis reports to specified files
- CLI and API Support: Use via command-line interface or import as a Python module
- Configurable: Customize model parameters like temperature, token limits, and more
-
Clone the Repository:
git clone https://github.com/yourusername/code_generator.git cd code_generator
-
(Optional) Create and Activate a Virtual Environment:
python3 -m venv venv source venv/bin/activate
-
Install the Package:
pip install .
-
Add to PATH:
Add the following line to your ~/.bashrc file:
export PATH="$HOME/.local/bin:$PATH"
Then either start a new terminal or run:
source ~/.bashrc
After installation, you can use the code-generator
command in two ways:
-
Generate Code:
code-generator generate "add two numbers" add.py
This will:
- Generate code based on the prompt "add two numbers"
- Write the generated code into add.py
-
Analyze Code:
code-generator analyze existing_code.py
This will:
- Analyze the Python file for various metrics
- Generate a detailed report in existingcodereadme.md
You can use the package directly within your Python code:
# For code generation
from code_generator import CodeGenerator
generator = CodeGenerator()
generated_code = generator.generate_code("add two numbers")
generator.write_code_to_file("add two numbers", "add.py")
# For code analysis
from code_generator import CodeAnalyzer
analyzer = CodeAnalyzer()
metrics = analyzer.analyze_file("existing_code.py")
analyzer.generate_readme("existing_code.py", metrics)
The analyzer provides comprehensive code metrics including:
- Lines of code
- Function and class count
- Import analysis
- Cyclomatic complexity
- Guidelines compliance
- Recommendations for improvement
Analysis reports include:
- Code structure overview
- Detailed function and class listings
- Import dependencies
- Complexity metrics
- Best practices compliance
- Improvement suggestions
code_generator/
├── code_generator
│ ├── __init__.py # Package initialization
│ ├── cli.py # Command-line interface
│ ├── generator.py # Code generation functionality
│ └── analyzer.py # Code analysis functionality
├── banner.png # Project banner image
├── setup.py # Package installation script
└── README.md # Documentation
You can configure the code generation process by modifying the parameters in CodeGenerator
:
model
: Model name (default: "llama-3.3-70b-versatile")temperature
: Controls randomness (default: 0.5)max_tokens
: Maximum token limit (default: 1024)top_p
: Controls diversity (default: 1.0)
The analyzer provides default thresholds for:
- Function count (warning at > 10)
- Cyclomatic complexity (warning at > 20)
- Import count (warning at > 15)
-
Set Up Development Environment:
python3 -m venv venv source venv/bin/activate pip install -e .
-
Test Code Generation:
python3 -m code_generator.cli generate "add two numbers" add.py
-
Test Code Analysis:
python3 -m code_generator.cli analyze existing_code.py
Contributions are welcome! To contribute:
- Fork the repository
- Create a feature branch
- Commit your changes
- Open a pull request
Please ensure your code adheres to the project's style guidelines.
This project is licensed under the MIT License.
Happy coding! 🚀