yapf
Format Python code with Yapf, a powerful Python code formatter. Improve readability and maintain consistent coding styles. Free online tool for developers.
Yapf - Python Code Formatter
Yapf (Yet Another Python Formatter) is a powerful tool designed to reformat Python code, making it more readable and consistent. It helps developers adhere to specific style guides, such as PEP 8 or Google's Python Style Guide, ensuring a uniform codebase across projects. This tool is invaluable for maintaining code quality and simplifying collaboration among team members.
Basic Yapf Usage
The primary function of Yapf is to format Python files. You can either modify files in place or preview the changes before applying them. Here are some fundamental commands:
# Format a single Python file in place
yapf -i <file_name.py>
# Preview the formatting changes without altering the file
yapf <file_name.py>
Formatting Directories and Styles
Yapf can also process entire directories recursively and apply specific formatting styles. This is crucial for large projects where consistency is paramount.
# Format all Python files in a directory in place
yapf -i -r <directory_name>
# Preview formatting changes for all Python files in a directory
yapf -r <directory_name>
# Format a Python file using a specific style (e.g., pep8, google)
yapf -i --style=<style_name> <file_name.py>
# Preview changes using a specific style
yapf --style=<style_name> <file_name.py>
Advanced Formatting Options
For more complex scenarios, Yapf offers options to redirect output to a different file or use custom configuration files. You can also view a diff of the proposed changes.
# Format a file and save the changes to another file
yapf <file_name.py> > <output_file_name.py>
# Run yapf with a custom configuration file
yapf --style=<config_file_path> <file_name.py>
# Show diff of what would be changed
yapf --diff <file_name.py>
Getting Help
If you need to explore all available options and commands for Yapf, you can access its help message.
# Display help message with all available options
yapf --help