Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.git
.github
.dockerignore
.gitignore

.idea
.vscode

__pycache__/
*.py[cod]
*$py.class
*.so
htmlcov/
.coverage
.coverage.*
.pytest_cache/
.mypy_cache
.venv
venv

# docs/
# tests/
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,11 @@ cython_debug/
.ionide

# End of https://www.toptal.com/developers/gitignore/api/pycharm+all,visualstudiocode,python,jupyternotebooks

# Custom ignores

/.ruff_cache/
public

# Scalene related
profile.*
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ repos:
args: ["--config=pyproject.toml"]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.246
rev: v0.0.252
hooks:
- id: ruff

Expand Down
20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ TEST_DIR=./tests
TEST_MARKER=placeholder
TEST_OUTPUT_DIR=tests_outputs
PRECOMMIT_FILE_PATHS=./python_template/__init__.py
PROFILE_FILE_PATH=./python_template/__init__.py
PYPI_URLS=

.PHONY: help install test clean build publish doc pre-commit format lint
.PHONY: help install test clean build publish doc pre-commit format lint profile
.DEFAULT_GOAL=help

help:
Expand All @@ -24,10 +25,10 @@ help:
update-pip:
${PYTHON} -m pip install -U pip

install-poetry: ## Install poetry if it is not already installed
install-poetry: ## Install poetry if it is not already installed (Installing poetry with official method is recommended)
$(MAKE) update-pip
! pip show poetry &> /dev/null && pip install poetry==1.3.1
poetry config virtualenvs.create false
! command -v poetry &> /dev/null && pip install poetry==1.3.2
# poetry config virtualenvs.create false
# poetry config repositories.private-pypi <PRIVATE_PYPI_URL>
# poetry config http-basic.private-pypi ${PYPI_USERNAME} ${PYPI_PASSWORD}

Expand Down Expand Up @@ -74,7 +75,7 @@ install-precommit: ## Install pre-commit hooks
pre-commit install

install-lint:
pip install black[d]==23.1.0 ruff==0.0.246
pip install black[d]==23.1.0 ruff==0.0.252

install-build:
############# PIP ############
Expand Down Expand Up @@ -207,3 +208,12 @@ typecheck-no-cache: ## Checks code with mypy no cache

typecheck-report: ## Checks code with mypy and generates html report
${PYTHON} -m mypy --package ${PACKAGE} --html-report mypy_report

profile: ## Profile the file with scalene and shows the report in the terminal
${PYTHON} -m scalene --cli --reduced-profile ${PROFILE_FILE_PATH}

profile-gui: ## Profile the file with scalene and shows the report in the browser
${PYTHON} -m scalene ${PROFILE_FILE_PATH}

profile-builtin: ## Profile the file with cProfile and shows the report in the terminal
${PYTHON} -m cProfile -s tottime ${PROFILE_FILE_PATH}
60 changes: 57 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,47 @@
- One of the biggest problems with setuptools is that the use of an executable file (i.e. the setup.py) cannot be executed without knowing its dependencies. And there is really no way to know what these dependencies are unless you actually execute the file that contains the information related to package dependencies.
- The pyproject.toml file is supposed to solve the build-tool dependency chicken and egg problem since pip itself can read pyproject.yoml along with the version of setuptools or wheel the project requires.
- The pyproject.toml file was introduced in PEP-518 (2016) as a way of separating configuration of the build system from a specific, optional library (setuptools) and also enabling setuptools to install itself without already being installed. Subsequently PEP-621 (2020) introduces the idea that the pyproject.toml file be used for wider project configuration and PEP-660 (2021) proposes finally doing away with the need for setup.py for editable installation using pip.
- It uses [entry points](https://setuptools.pypa.io/en/latest/userguide/entry_point.html)

# Install

- Default installation
## Default installation

- Install poetry
```bash
curl -sSL https://install.python-poetry.org | python3 -
```

- Install the project dependencies
```bash
conda create -n python-template python=3.8 -y
conda activate python-template
make install
make -s install
```

- After running above command, the project installed in editable mode with all development and test dependencies installed.
- Moreover, a dummy `entry point` called `placeholder` will be available as a cli command.

## Docker

```bash
# Development build (800 MB)
docker build --tag python-template --file docker/Dockerfile --target development .

# Production build (145 MB)
docker build --tag python-template --file docker/Dockerfile --target production .
```

## IDE Setings
- To run command inside the container:

```bash
docker run --rm -it python-template:latest bash

# Temporary container
docker run -it python-template:latest bash
```

# IDE Setings

### Pycharm

Expand Down Expand Up @@ -47,3 +76,28 @@ Enable all except:
### Vscode

- All recommended settings and extensions can be found in `.vscode` directory.

# Useful Makefile commands
```bash
# All available commands
makefile
makefile help

# Run all tests
make -s test

# Run specific tests
make -s test-one TEST_MARKER=<TEST_MARKER>

# Remove unnecessary files such as build,test, cache
make -s clean

# Run all pre-commit hooks
make -s pre-commit

# Lint the project
make -s lint

# Profile a file
make -s profile PROFILE_FILE_PATH=<PATH_TO_FILE>
```
97 changes: 97 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Adapted from:
# https://github.com/NannyML/nannyml/blob/main/Dockerfile

ARG APP_NAME=python_template
ARG APP_PATH=/opt/$APP_NAME
ARG PYTHON_VERSION=3.10-slim-bullseye
ARG POETRY_VERSION=1.3.2

#
# Stage: staging
#
FROM python:$PYTHON_VERSION as staging
ARG APP_NAME
ARG APP_PATH
ARG POETRY_VERSION

ENV \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1
ENV \
POETRY_VERSION=$POETRY_VERSION \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1

RUN apt-get update && \
apt-get install --no-install-recommends -y \
curl \
build-essential

# Install Poetry - respects $POETRY_VERSION & $POETRY_HOME
RUN curl -sSL https://install.python-poetry.org | python -
ENV PATH="$POETRY_HOME/bin:$PATH"

# Import our project files
WORKDIR $APP_PATH
COPY ./poetry.lock ./pyproject.toml ./
COPY . .

#
# Stage: development
#
FROM staging as development
ARG APP_NAME
ARG APP_PATH

ENV POETRY_INSTALLER_MAX_WORKERS=10

# Install project in editable mode and with development dependencies
WORKDIR $APP_PATH
RUN poetry install

ENTRYPOINT ["poetry", "run"]
# CMD ["nml"]

#
# Stage: build
#
FROM staging as build
ARG APP_PATH

WORKDIR $APP_PATH
RUN poetry build --format wheel
RUN poetry export --format requirements.txt --output constraints.txt --without-hashes

#
# Stage: production
#
FROM python:$PYTHON_VERSION as production
ARG APP_NAME
ARG APP_PATH

ENV \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1

ENV \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100

# Get build artifact wheel and install it respecting dependency versions
WORKDIR $APP_PATH
COPY --from=build $APP_PATH/dist/*.whl ./
COPY --from=build $APP_PATH/constraints.txt ./
RUN pip install ./$APP_NAME*.whl --constraint constraints.txt

RUN useradd --create-home --shell /bin/bash $APP_NAME
WORKDIR /home/$APP_NAME
USER $APP_NAME

# export APP_NAME as environment variable for the CMD
ENV APP_NAME=$APP_NAME

CMD ["sh", "-c", "$APP_NAME"]
26 changes: 26 additions & 0 deletions docs/poetry.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
- All the documentation can be found [here](https://python-poetry.org/docs/). Below are some of the commands that is used frequently.

- Install (recommended)

```bash
curl -sSL https://install.python-poetry.org | python3 -
```

- Add `zsh` auto completion

```bash

# Open `~/.zshrc` and add the following lines
fpath+=~/.zfunc
autoload -Uz compinit && compinit

source ~/.zshrc

mkdir ~/.zfunc
poetry completions zsh > ~/.zfunc/_poetry
exec zsh
```

- Uninstall
```
curl -sSL https://install.python-poetry.org | python3 - --uninstall
```

- Update poetry

```bash
Expand Down
Loading