DEV Community

madhur Saluja
madhur Saluja

Posted on

Introducing My Release-0.1 Project: A Code Complexity Pro

Contributing to open-source projects is one of the most rewarding experiences in software development, and building my own project for release gave me valuable insights into both software development and collaboration. In this post, I will introduce the Release-0.1 project I created as part of my participation in the Topics in Open Source course. I'll walk through how to use the tool, the features it includes, and show examples of how it works.

Project Overview: Code Complexity Analyzer

My project, Release-0.1, is a Code Complexity Analyzer. It’s a command-line tool that analyzes the complexity of source code files using the Grok AI API. The tool helps developers assess how complex their code is and provides insight into areas that could benefit from refactoring or optimization.

Key Features
Here’s a list of the features that the project includes:

  • Version Display: You can view the tool’s version using a simple command-line flag.
  • Help/Usage Message: The tool provides a detailed help message to guide users on how to run the analyzer.
  • File Input: Users can specify one or more files to analyze.
  • Customizable Output: The output can be directed either to the terminal (stdout) or to a specific file.
  • Model Selection: Users can choose which AI model to use for analyzing code complexity (the default model is llama-v2).
  • API Key and Base URL Configuration: API credentials can be passed through the command line or set in a .env file for convenience.
  • Error Handling: All errors and debug information are written to stderr, keeping logs and output separate.

Getting Started

To start using the tool, you’ll first need to set up your environment by cloning the project, installing dependencies, and configuring your API credentials.

1. Clone the Repository
First, clone the repository to your local machine:

git clone https://github.com/MadhurSaluja/Release-0.1
cd Release-0.1
Enter fullscreen mode Exit fullscreen mode

2. Install Dependencies
Ensure that you have Python 3.x installed. Next, install the necessary dependencies using pip:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

3. Set Up API Credentials
The tool relies on the Grok AI API to analyze code complexity. To get started, you’ll need to create a .env file in the project directory containing your API key and base URL. Your .env file should look like this:

API_KEY=your-grok-api-key
BASE_URL=https://api.groq.com/openai/v1/chat/completions
Enter fullscreen mode Exit fullscreen mode

You can obtain your API key by signing up for the Grok AI service.

##How to Use the Tool

Here’s a breakdown of how to use the tool, with examples of key functionality.

1. Display Version
You can view the version of the tool with the following command:

python main.py --version
Enter fullscreen mode Exit fullscreen mode

2. Display Help
To view the available options and usage instructions, run the --help or -h flag:

python main.py --help
Enter fullscreen mode Exit fullscreen mode

This will print out all the available command-line options, including input files, model selection, output options, and API configuration.

3. Run the Analyzer on a Single File
You can analyze a single source code file by passing the file path as an argument:

python main.py ./test_code.py
Enter fullscreen mode Exit fullscreen mode

This will analyze the complexity of test_code.py and print the results to the terminal.

4. Run the Analyzer on Multiple Files
The tool can handle multiple files. Simply list the files you want to analyze:

python main.py file1.py file2.py
Enter fullscreen mode Exit fullscreen mode

the results for each file will be printed in sequence.

5. Specify an Output File
By default, the results are printed to the terminal (stdout). If you want to save the output to a file, use the --output or -o flag:

python main.py test_code.py -o analysis_results.txt
Enter fullscreen mode Exit fullscreen mode

This will write the analysis results to analysis_results.txt.

6. Select a Specific AI Model
The tool allows you to specify the AI model used for the analysis with the --model or -m flag. By default, it uses the llama-v2 model, but you can select a different one if desired:

python main.py test_code.py --model model
Enter fullscreen mode Exit fullscreen mode

7. Use API Key and Base URL
If you don’t want to use the .env file for API credentials, you can pass the API key and base URL directly via command-line arguments:

python main.py test_code.py --api-key your_api_key --base-url https://api.custom-url.com
Enter fullscreen mode Exit fullscreen mode

This flexibility allows you to switch between environments or APIs without changing the configuration file.

8. Error Handling
If you try to run the tool with an invalid file or without providing necessary arguments, it will log the errors to stderr. For example, if the file doesn’t exist:

python main.py nonexistent_file.py
Enter fullscreen mode Exit fullscreen mode

The tool will output:

Error: File nonexistent_file.py not found

Enter fullscreen mode Exit fullscreen mode

Example: Full Command

Here’s an example that combines several options: running the analyzer on two files, specifying a different AI model, and saving the output to a file:

python main.py test_code.py another_code.py --model model -o complexity_analysis.txt
Enter fullscreen mode Exit fullscreen mode

Conclusion

The Code Complexity Analyzer is a simple but powerful tool that integrates with the Grok AI API to provide valuable insights into your code's complexity. With flexible options for input, output, and configuration, it can easily fit into your workflow.

I’m excited to see how this tool can evolve, and I welcome contributions and feedback. If you’re interested, you can check out the Release-0.1 repository and try it out for yourself!

Feel free to reach out if you have any questions or suggestions for improvements!

Top comments (0)