For this week we were tasked have a static Analysis tool for our tool that we have developed, in my case DocBot also we had a requirement for adding a CONTRIBUTING.md
file to our Repo.
Formatter
What is a formatter, this is an additional tool that we can use in our code to have our code look cleaner and pretterier, making it organized and better to read. To accomplish this task i have used The Ruff Formatter, the reason why i choose this is that i found its documentation and their website easy to understand and the installation was also pretty easy.
Some other reasons include, availability of extensions and more support or documentations of this tool is available online for a beginner to use it.
How to use Ruff Formatter
Well this is pretty simple first it's the installation of the tool itself:
pip install ruff
Then there is actually 3 commands that you can use to format the code which include:
ruff format # Format all files in the current directory.
ruff format path/to/code/ # Format all files in `path/to/code` (and any subdirectories).
ruff format path/to/file.py # Format a single file.
For Example :
Unformatted Code:
def my_function(x,y):return x+y
class MyClass:
def method_one(self):print("Hello, World!")
def method_two(self,x):if x>10:return True
return False
Formatted Code (after applying Ruff):
def my_function(x, y):
return x + y
class MyClass:
def method_one(self):
print("Hello, World!")
def method_two(self, x):
if x > 10:
return True
return False
In this small example we can see that how Ruff takes care of the Spacing, Line Breaks and Indentation.
Linter
So linter is a tool that helps analyze the code and gives or fixes the issue, which will improve code quality and helps users to find bugs. For my requirement i have used The Ruff Linter as Ruff offers both Formatter and Linter.
How to use Ruff Linter
First of is the installation it is same, it will get installed along with Formater.
pip install ruff
Well Ruff Linter offers 4 option to the user which are:
ruff check # Lint all files in the current directory.
ruff check --fix # Lint all files in the current directory, and fix any fixable errors.
ruff check --watch # Lint all files in the current directory, and re-lint on change.
ruff check path/to/code/ # Lint all files in `path/to/code` (and any subdirectories).
When i ran this in my code i had one issue which was picked up the tool:
This was an very easy fix !
VSCODE Automation
Final task for this week was to have it automated, it was pretty simple as theirs were clear instruction from Ruff on how to install the VS code extension and how to use the setting.json
to make it automated.
All the details for this was found in Ruff VSCODE
The final setting.json
file will look like:
{
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
-
editor.formatOnSave
: Automatically formats the code when you save the file. -
editor.codeActionsOnSave
: Specifies actions to take on save -
source.fixAll
: Applies all possible fixes explicitly (like linting errors). -
source.organizeImports
: Organizes import statements explicitly. -
editor.defaultFormatter
: Sets charliermarsh.ruff as the default formatter for Python code.
Conclusion
Well, it was fun adding an CONTRIBUTING.md
file to our Repo, which i felt like a basic requirement for every open source programme, i was happy to learn about that and Secondly automation, which was also a new knowledge for me and i understood its importance too. Finally all of the commits were squashed together and merged to main.
AJO GEORGE 01-11-2024
Top comments (0)