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
set your python path:
export PYTHONPATH="${PYTHONPATH}:$(pwd)"
Run the application:
uv run srcRun tests:
uvx pytestRun linter:
uvx flake8Run pre-commit:
uvx pre-commitDocumentation:
source .venv/bin/activate
make htmlUse jupyter notebook in the virtual environment:
source .venv/bin/activate
ipython kernel install --user --name=venv
uvx jupyter labAnd you can select the "venv" kernel inside jupyter ui.
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
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