Skip to content

Instantly share code, notes, and snippets.

@k-sriram
Last active March 16, 2022 12:22
Show Gist options
  • Save k-sriram/567545ef0f45dafae6614a5ec48fc5ad to your computer and use it in GitHub Desktop.
Save k-sriram/567545ef0f45dafae6614a5ec48fc5ad to your computer and use it in GitHub Desktop.
Quickly initialize a python project
DEV_REQS="black isort flake8 pytest mypy pre-commit"
AUTHOR="Sriram Krishna"
EMAIL="ksri91@gmail.com"
proj=$1
if [ -z "$proj" ]; then
echo "Usage: $0 <project> [<python executable>]"
exit 1
fi
if [ -z "$2" ]; then
pyexec="python"
else
pyexec=$2
fi
mkdir -p $proj
(
cd $proj
mkdir -p src/$proj
mkdir -p tests
touch tests/__init__.py src/$proj/__init__.py
$pyexec -m venv .venv
. .venv/bin/activate
if [ -z "$VIRTUAL_ENV" ]; then
echo "Failed to activate virtualenv"
exit 1
fi
pip install --upgrade pip
pip install $DEV_REQS
pip freeze > dev_requirements.txt
pytest_version=$(python -c "import pytest; print(pytest.__version__)")
curl --location https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore >.gitignore
cat >pyproject.toml <<EOF
[build-system]
requires = ["setuptools>=42.0", "wheel"]
build-backend = "setuptools.build_meta"
[tool.pytest.ini_options]
minversion = "$pytest_version"
testpaths = ["tests"]
[tool.mypy]
files = ["src/$proj", "tests"]
warn_unused_ignores = true
show_error_codes = true
ignore_missing_imports = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
[[tool.mypy.overrides]]
module = ["tests.*"]
disallow_untyped_defs = false
[tool.isort]
profile = "black"
EOF
cat >setup.cfg <<EOF
[metadata]
name = $proj
author = $AUTHOR
author_email = $EMAIL
description =
version = 0.1.0
#url =
#keywords =
classifiers =
Programming Language :: Python :: 3.10
License :: OSI Approved :: MIT License
license_files =
LICENSE
[options]
python_requires = >= 3.10
packages = $proj
zip_safe = True
package_dir =
=src
#install_requires =
#include_package_data = True
[flake8]
max-line-length = 88
EOF
cat >LICENSE <<EOF
MIT License
Copyright (c) $(date +%G) $EMAIL
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
EOF
pip install -e .
deactivate
git init
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment