Skip to content

Instantly share code, notes, and snippets.

@leandroluk
Created September 14, 2023 19:45
Show Gist options
  • Save leandroluk/0dc85b6795648ecbd47e9bb4ffcc8475 to your computer and use it in GitHub Desktop.
Save leandroluk/0dc85b6795648ecbd47e9bb4ffcc8475 to your computer and use it in GitHub Desktop.

How to start a new backend Python project

  1. Commit chore: configuring vscode

    • create file .gitignore:

      # Extra folders
      .tmp
      
      # Distribution / packaging
      .Python
      env/
      build/
      develop-eggs/
      dist/
      downloads/
      eggs/
      .eggs/
      lib/
      lib64/
      parts/
      sdist/
      var/
      *.egg-info/
      .installed.cfg
      *.egg
      
      # IPython
      profile_default/
      ipython_config.py
      
      # pyenv
      .python-version
      
      # PEP 582; used by e.g. github.com/David-OConnor/pyflow
      __pypackages__/
      
      # Environments
      .env
      .venv
      env/
      venv/
      ENV/
      env.bak/
      venv.bak/
      
      # Unit test / coverage reports
      htmlcov/
      .tox/
      .nox/
      .coverage
      .coverage.*
      .cache
      nosetests.xml
      coverage.xml
      *.cover
      *.py,cover
      .hypothesis/
      .pytest_cache/
      **/__pycache__/**'
      
    • create .vscode/settings.json with out settings

      {
          // files
          "files.exclude": {
            "**/.git": true,
            "**/.svn": true,
            "**/.hg": true,
            "**/CVS": true,
            "**/.DS_Store": true,
            "**/Thumbs.db": true,
            "**/__pycache__": true,
            ".pytest_cache": true
          },
          // python
          "python.testing.pytestArgs": [
            "test"
          ],
          "python.testing.unittestEnabled": false,
          "python.testing.pytestEnabled": true,
          "python.testing.autoTestDiscoverOnSaveEnabled": true,
          "[python]": {
            "editor.formatOnSave": true,
            "editor.defaultFormatter": "ms-python.black-formatter"
          },
          // git
          "git.ignoreLimitWarning": false,
          // editor
          "editor.formatOnPaste": true,
          "editor.formatOnSave": true,
          "editor.formatOnType": true,
          "editor.formatOnSaveMode": "file",
          "editor.detectIndentation": false,
          "editor.tabSize": 4,
          "editor.rulers": [
            80,
            100,
            120
          ],
          // black-formatter
          "black-formatter.args": [
            "--line-length=120"
          ],
          // json
          "[jsonc]": {
            "editor.tabSize": 2,
          },
      }
    • create .vscode/extensions.json with our prefer extensions

      {
        "recommendations": [
          "vscode-icons-team.vscode-icons",
          "ms-python.python",
          "LittleFoxTeam.vscode-python-test-adapter",
          "ms-python.flake8",
          "ms-python.black-formatter"
        ]
      }
  2. Commit chore: configuring git & lefthook

    • install lefthook and initialize

      pip install --upgrade pip && pip install lefthook && lefthook install

      will be created a file lefthook.yml

    • install the package commit-linter

      pip install commit-linter && commit-linter install
  3. Commit chore: configure pipenv & pytest

    • install dependencies:

      pipenv install -d pytest pytest-cov pytest-spec pytest-describe pytest-mock pytest-random-order flake8 black

    will be created Pipfile and Pipenv.lock

    • (optional if needed) edit the file Pipfile changing python version to 9
  4. Commit chore: configure docker

    • configure Dockerfile

      FROM python:3.9-slim-buster AS base
      
      ENV LANG C.UTF-8
      ENV LC_ALL C.UTF-8
      ENV PYTHONDONTWRITEBYTECODE 1
      ENV PYTHONFAULTHANDLER 1
      
      FROM base AS python-deps
      
      RUN pip install pipenv
      RUN apt-get update && apt-get install -y --no-install-recommends gcc
      
      COPY Pipfile .
      COPY Pipfile.lock .
      RUN PIPENV_VENV_IN_PROJECT=1 pipenv install --deploy
      
      FROM base AS runtime
      
      COPY --from=python-deps /.venv /.venv
      ENV PATH="/.venv/bin:$PATH"
      
      RUN useradd --create-home appuser
      WORKDIR /home/appuser
      USER appuser
      
      COPY . .
      
      CMD ["python", "-m", "src"]      
    • create db.sql to build database structure

      CREATE TABLE example (
      "id" BIGINT NOT NULL SERIAL,
      ...
      );
      /* migrations and seeds goes here*/
    • configure docker-compose.yml

      version: '3'
      networks:
        {{APP_NAME}}:
          name: {{APP_NAME}}
      services:
        postgres:
          image: postgres
          hostname: postgres
          container_name: postgres
          ports: [ '5432:5432' ]
          environment:
            POSTGRES_USER: postgres
            POSTGRES_PASSWORD: postgres
            POSTGRES_DB: {{APP_NAME}}
          volumes: [ './db.sql:/docker-entrypoint-initdb.d/db.sql' ]
          healthcheck:
            test: ['CMD-SHELL', 'pg_isready -U postgres']
            interval: 10s
            timeout: 5s
            start_period: 10s    
  5. Commit chore: configure debugging

    • create .vscode/launch.json file

      {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "dev",
            "type": "python",
            "request": "launch",
            "module": "src",
            "justMyCode": true
          }
        ],
        "compounds": []
      }      
  6. Start development of application.

Every change by each file you need make commit's in conventional-commit design structure

The Flake8 and Blakc config use my personal config, you can change this if needed

If you have any question with this gist, call me on Linkedin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment