/template_python

Just to save time when starting a new repo

Primary LanguagePythonMIT LicenseMIT

My Project

A brand new project.

Onboarding / Reminder

Clone this repo

Install Python

Get it from https://www.python.org/downloads/

Get ready to run an example

  • Open a command/terminal and navigate to the cloned repo location.
  • Create a venv [OPTIONAL]
    • python -m venv .venv
    • Windows: "C:\Program Files\Python311\python.exe" -m venv .venv311 if you want to target a specific python version.
  • Enable your venv [OPTIONAL]
    • Windows/PowerShell: .venv\Scripts\Activate.ps1
    • Windows/PowerShell: .venv311\Scripts\Activate.ps1 if you want to target your specific python version.
    • When you're not sure to have your venv enabled, you can check if your default python executable is located in your virtual environment.
      Check where is located your default python executable python -c "import sys; print(sys.executable)"
      If your venv is enable, it should return something like: .venv\Scripts\python.exe
  • Upgrade pip
    python -m pip install --upgrade pip
  • Install the dependencies with pip:
    pip install -r requirements.txt
  • Run it!
    python.exe -m examples.main main

Development Environment

Tests

Run tests

  • All the tests:
    python -m unittest discover -v -s "tests" -p "*_test.py" -t "."
  • A test file:
    python -m unittest -v 'tests.unit.fibonacci_test'
  • A test class:
    python -m unittest -v 'tests.unit.fibonacci_test.Fibo_Test'
  • A single test:
    python -m unittest -v 'tests.unit.fibonacci_test.Fibo_Test.test_callWithValidInput_0'

Code Coverage

coverage is part the installed packages, you just need to:

  • Run the unit tests with coverage:
    • All the unit tests: coverage run -m unittest discover -s "tests" -p "*_test.py" -t "."
    • A specific unit test: coverage run -m unittest -v 'tests.unit.fibonacci_test.Fibo_Test.test_callWithValidInput_0'
  • Generate the coverage results:
    • coverage report
      Text version of the coverage results
    • coverage report --format=total
      Total coverage result (no details)
    • coverage lcov
      Will generate an lcov.info file that will be processed by Coverage Gutters extension in Visual Studio Code. .vscode/settings.json is configured accordingly.
    • coverage html
      Will generate an HTML version of the coverage results. You may access those from <repo>/htmlcov/index.html.

Linter

flake8 is part the installed packages, you just need to:

  • run flake8 on the repo:
    python -m flake8 .
  • Check if any error has been reported.

You may also access flake8 from VSCode if you installed flake 8 extension

IDE

Keep dependencies up-to-date

  • Keep track of your dependencies:
    • pip list List all the installed dependencies...meaning all the dependencies you've explicitly installed and their implicit dependencies)
    • pip freeze Same as pip list, but respecting the requirements file format
  • Update dependencies
    Edit the requirements.txt file. Only add the explicit dependency you need, not their internal/recursive dependencies. To not lose your sanity, you may want to install pip-chill and/or pipdeptree.
  • Check if your dependencies have all they need: pip check
  • Want to start with a clean slate:
    • pip freeze > clean_dependencies.txt
    • pip uninstall -r clean_dependencies.txt -y