Skip to content

San7o/modern-python-template

Repository files navigation

modern-python-template

This project serves as a template for modern python libraries. The following is already configured:

  • uv as a project manager
  • testing with pytest
  • linting with flake8
  • mypy for static type checking
  • nix development environment
  • documentation with sphinx
  • jupyter notebook
  • directory structure

Usage

set your python path:

export PYTHONPATH="${PYTHONPATH}:$(pwd)"

Run the application:

uv run src

Run tests:

uvx pytest

Run linter:

uvx flake8

Run pre-commit:

uvx pre-commit

Documentation:

source .venv/bin/activate
make html

Use jupyter notebook in the virtual environment:

source .venv/bin/activate
ipython kernel install --user --name=venv
uvx jupyter lab

And you can select the "venv" kernel inside jupyter ui.

uv usage

Init a project

uv init .

Install stuff:

uv add flake8      # linter
uv add pre-commit  # run stuff before committing
uv add pytest      # testing framework
uv add pytest-randomly  # randomize tests
uv add pytest-cov  # get code coverage of tests
uv add mypy        # static type checker

Tips and tricks

Use @datalass to create default functions (init, eq...):

from dataclasses import dataclass

@dataclass
class InventoryItem:
    ...

Use f-strings instead of formatting:

>>> name = "Fred"
>>> f"He said his name is {name!r}."
"He said his name is 'Fred'."

Use breakpoints when debugging:

breakpoint(*args, **kws)

Use the standard logging libary:

# myapp.py
import logging
import mylib
logger = logging.getLogger(__name__)

def main():
    logging.basicConfig(filename='myapp.log', level=logging.INFO)
    logger.info('Started')
    mylib.do_something()
    logger.info('Finished')

if __name__ == '__main__':
    main()

Use argparse to parse command line arguments

Use pathlib to handle paths instead of strings

About

Modern python template

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages