?print-pdf
' Created for
if x is None
instead of if x == None
.try-except
with specific exceptions.
# Wrong:
# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# Correct:
# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# Add 4 spaces (an extra level of indentation) to distinguish arguments from the rest.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
# Wrong:
# operators sit far away from their operands
income = (gross_wages +
taxable_interest +
(dividends - qualified_dividends) -
ira_deduction -
student_loan_interest)
# Correct:
# easy to match operators with operands
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)
# Install Ruff
pip install ruff
# Run linting
ruff check .
# Run linting with automatic fixes
ruff check --fix .
# Format code
ruff format .
pyproject.toml
file, which must be in your project root folder.
[tool.ruff]
# Set the maximum allowed line length to 80 characters
line-length = 80
# Select which rules to enable
# "ALL" enables every single rule that Ruff supports
select = [
"ALL",
]
# Specify which rules to ignore
ignore = [
"D100", # Ignore missing module docstrings (top-level docstrings in files)
"T201", # Ignore warnings about print statements (useful for debugging/learning)
]
# Exclude files and directories from being checked
# These are typically system directories or generated code
exclude = [
".git",
".mypy_cache",
".ruff_cache",
"venv",
".venv",
"__pycache__",
]
# Configuration for the linting component of Ruff
[tool.ruff.lint]
# Allow Ruff to automatically fix all fixable violations
# This makes it easy to clean up code with a single command
fixable = ["ALL"]
# Configuration for the formatting component of Ruff
[tool.ruff.format]
# Use double quotes for strings (similar to Black's default style)
quote-style = "double"
{
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
{
"[python]": {
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
}
}
}
help
function.
def foo(x,y):
# Just a Block comment - do not use it for function description
"""foo() Summary
Args:
x (TYPE): int
y (TYPE): int
Returns:
TYPE: int
"""
return x+Y
help(foo)
# Help on function foo in module __main__:
# foo(x, y)
# foo() Summary
# Args:
# x (TYPE): int
# y (TYPE): int
# Returns:
# TYPE: int
These slides are based on
customised version of
framework