I created a template for a Python project that can be used universally

I use Python a lot, but I used to hit the same code every time to build the environment. This takes a lot of time & the parts used are the same, so I published the repository as a project template that can be used for general purposes. https://github.com/odrum428/python_setup

Purpose

When I started a Python project, I always entered the same commands, such as around CI and documents.

It's like initializing a project using Pipenv, defining a CI, putting in tests and lint, and documenting with Sphinx.

It took me a while to get these in place each time, so I created a project template with the aim of configuring it for any Python project.

It is made to be compatible with analysis, application, and model generation.

Configuration overview

package

Pipenv is used for package and environment management. The following articles are organized for these management tools, so I think you should refer to them. Understanding best practices around Python packages

At present, it is best to use pipenv and manage various settings with setup.py and setup.cfg. It feels like I've used it, and it's much easier to manage and build.

The packages used in the project created this time are summarized.

As a basic lint system, we have introduced ʻisort that arranges package import etc. and flake8that arranges code style. In addition to this, there are alsoyapf etc., but I adopted the standard PEP8 and made it a comfortable flake8`. Here is the code of the error part It is set to be done.

We adopted pytest for the test. To be honest, if you use pytest for python testing, I think that there is no problem. There are many things that pytest can do than standard unittest, and it is easy to handle.

In addition, this template uses Sphinx to generate documents. The documentation is generated from the test code and docstring defined in the tests folder. By generating documents only from test cases, we aim to actively write test cases and enrich the documents. The theme of the document is sphinx-rtd-theme.

CI We have also introduced CI using Circle CI. Run Lint with ʻisort and flake8to keep the code style, and run the test code withpytest. Tox, which enables multiple versions of test execution, and pytest-randomly`, which makes tests random, are excluded because they are not used for general purposes.

In addition to these, we have also implemented automatic documentation updates. When there is an update in the tests folder, I update the document with CI and even commit Git.

  is_docs_update:
    steps:
      - run:
          name: check tests folder is updated
          command: |
            if [[ ! $(git diff --diff-filter=d --name-only HEAD | grep tests/ ) = '' ]]; then
              echo "build and deploy"
            else
                echo "no need docs update"
                circleci step halt
            fi
      - run:
          name: make rst file
          command: |
            pipenv run sphinx-apidoc -f -o docs/ tests/
      - run:
          name: make html
          command: |
            pipenv run sphinx-build -a ./docs ./docs/public
      - run:
          name: git push
          command: | 
            git config --global user.email [email protected]
            git config --global user.name odrum428
            git add -A
            git commit -m 'updating docs [skip ci]'
            git push origin HEAD

file organization

The file structure is shown below.

 ├ .circleci/
 ├ .envrc
 ├ .github/
 ├ .gitignore
 ├ docs/
 │  ├ Makefile
 │  ├ conf.py
 │  ├ index.rst
 │  ├ make.bat
 │  ├ modules.rst
 │  ├ public/
 │  └ tests.rst
 │
 ├ src/
 ├ tests/
 ├ LICENSE
 ├ Pipfile
 ├ Pipfile.lock
 ├ README.md
 ├ setup.cfg
 └ setup.py

I easily separated the structure into source, test, and document.

App code, machine learning, analysis code, etc. are all managed in src in a nice way. Write the test code with the same file structure. Documentation is generated from the test code. It is an image like.

From here, I think we should expand it according to the application.

Packages and various settings are done in setup.cfg. This greatly simplifies the code and can be written as:

setup.py


from setuptools import setup

setup()

The actual contents of the settings are set in setup.cfg.

[metadata]
name = sample-package
version = '1.0'
auther = Keita Mizushima
auther_email = [email protected]
description = sample repogitory of python
description-file = file: README.md
url = http://example.com
license = MIT
license_file = LICENSE

[options]
install_requires=
packages = find:

[flake8]
show_source = True
max-line-length=120
max-complexity=15

How to use

Simply register a new project based on this repository.

  1. Create a GitHub repository.

This time I created a repository called new_project. image.png

  1. Start a new project based on this repository
git clone [email protected]:odrum428/python_setup.git new_poject
cd new_project
git remote set-url origin [email protected]:user_name/new_project.git
git push origin master

Now you can create a new project while taking over this repository.

3 Install pipenv.

pip install pipenv

As a bonus, direnv automatically switches between virtual environments, so it's recommended! You don't have to activate the environment every time. https://github.com/direnv/direnv

Summary

I will continue to update it every time the environment changes, so please use it if you have trouble with the configuration. Also, I would be grateful if you could give us a PR to the repository.

https://github.com/odrum428/python_setup

Recommended Posts

I created a template for a Python project that can be used universally
I made a familiar function that can be used in statistics with Python
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
Created a library for python that can easily handle morpheme division
I made a shuffle that can be reset (reverted) with Python
Functions that can be used in for statements
Mathematical optimization that can be used for free work with Python + PuLP
Understand the probabilities and statistics that can be used for progress management with a python program
A class for PYTHON that can be operated without being aware of LDAP
I want to create a priority queue that can be updated in Python (2.7)
How to install a Python library that can be used by pharmaceutical companies
I made a VM that runs OpenCV for Python
Python knowledge notes that can be used with AtCoder
[Django] About users that can be used on template
Scripts that can be used when using bottle in Python
Can I be a data scientist?
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
[Atcoder] [C ++] I made a test automation tool that can be used during the contest
I made a data extension class for tensorflow> = 2.0 because ImageDataGenerator can no longer be used.
I made a tool to automatically generate a state transition diagram that can be used for both web development and application development
[Python] Make a graph that can be moved around with Plotly
[Python] I made my own library that can be imported dynamically
I made a package that can compare morphological analyzers with Python
A timer (ticker) that can be used in the field (can be used anywhere)
[python] I created a follow-up correlation diagram for twitter (Gremlin edition)
Python standard input summary that can be used in competition pro
Python standard module that can be used on the command line
[python] I made a class that can write a file tree quickly
The Python project template I think of.
I created a password tool in Python.
About the matter that the re.compiled object can be used for the re.match pattern
[Python] A program that finds a pair that can be divided by a specified value
[Python] I made a utility that can access dict type like a path
I made a simple timer that can be started from the terminal
I made a module PyNanaco that can charge nanaco credit with python
I wanted to quickly create a mail server that can be used freely with postfix + dovecot on EC2
A memo for making a figure that can be posted to a journal with matplotlib
++ and-cannot be used for increment / decrement in python
I made a python dictionary file for Neocomplete
Project cannot be created with Python3.5 (Windows) + django1.7.1
[Python3] Code that can be used when you want to cut out an image in a specific size
Created a Python wrapper for the Qiita API
File types that can be used with Go
A memo that I wrote a quicksort in Python
I tried to create a class that can easily serialize Json in Python
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
A personal memo of Pandas related operations that can be used in practice
If you want to make a Windows application (exe) that can be actually used now using only Python
I made a Docker image that can call FBX SDK Python from Node.js
[Python] I created an app that automatically downloads the audio file of each word used for the English study app.
I created a Dockerfile for Django's development environment
[Python] f strings should be used for embedding strings
"Gazpacho", a scraping module that can be used more easily than Beautiful Soup
Overview and useful features of scikit-learn that can also be used for deep learning
Convert images from FlyCapture SDK to a form that can be used with openCV
Summary of statistical data analysis methods using Python that can be used in business
[Python] Building an environment for competitive programming with Atom (input () can be used!) [Mac]
A template that I often use when making Discord BOT in Python (memorial note)
Set up an FTP server that can be created and destroyed immediately (in Python)
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
I tried to make a memo app that can be pomodoro, but a reflection record