DEV Community

Karthi Mahadevan
Karthi Mahadevan

Posted on

What I use daily - python development

Best Practices I follow during python development in current project.

  • Use Virtual Environments to keep dependencies isolated.
  • vscode to develop
  • Pytest, mockito for tests
  • Document Code using MkDocs and publish it as site in gitlab pages. ( Sooner we are moving to Backstage)
  • Taskfile , designed to streamline development processes related to testing package building, and publishing for a Python project. task is responsible for creating the source distribution (sdist) and wheel packages of the application using poetry. task also publishes the built package to a specified repository (GitLab in our case).
  • Entire org using Renovate to automate the management of software dependencies, ensuring projects remain up-to-date with the latest versions and security patches.
  • We use pre-commit on every commit to enforce our linting and style, use pre-commit install --install-hooks to enable it.
  • Mypy a static type checker that helps ensure type correctness in Python code. so we can catch type-related errors before runtime.
  • Ruff , a linter and formatter that can check for style issues, potential bugs across Python code.
  • some 10+ Pre-commit hooks.
  • Strict Commitlint format
    • Gitleaks for detecting sensitive information (like API keys and passwords)
  • few more for Python project focusing on linting, testing, mutation testing, type checking, and ensuring code quality.

a sample function formatted according to Ruff's style conventions in our project would look like

def calculate_area(radius: float, pi: float = 3.14159) -> float:
    """Calculate the area of a circle.

    Args:
        radius (float): The radius of the circle.
        pi (float, optional): The value of pi. Defaults to 3.14159.

    Returns:
        float: The area of the circle.

    Raises:
        ValueError: If the radius is negative.
    """
    if radius < 0:
        raise ValueError("Radius must be non-negative.")

    return pi * (radius ** 2)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)