Python Example Project Structure

Overview

Python Example Project Structure

Example of statuses that can be in readme:

Go Report Card CII Best Practices Gitpod ready-to-code Fuzzing Status

Visit my docs for the full documentation, examples and guides.

With this project you get:

  • a minimal setup.py file
  • testing with PyTest
  • documentation (HTML and PDF) generated using Sphinx
  • a CLI entry point

Project Structure

example_project/
 |-- docs/
 |-- |-- build/
 |-- |-- source/
 |-- example_project/
 |-- |-- __init__.py
 |-- |-- __version__.py
 |-- |-- example_module.py
 |-- tests/
 |-- |-- test_data/
 |-- |   |-- example_class_data.json
 |-- |   __init__.py
 |-- |   conftest.py
 |-- |   test_example_class.py
 |-- .env
 |-- .gitignore
 |-- Pipfile
 |-- Pipfile.lock
 |-- README.md
 |-- setup.py

Example Project

  • example_module.py
  • cli.py

The example_module.py module contains sample code. tests folder contains tests using PyTest.

The cli.py module is referenced in the setup.py file via the entry_points definitions:

entry_points={
    'console_scripts': ['py-package-template=example_project.cli:main'],
}

Project Dependencies

Using pipenv. Use --dev flag for pkgs only needed for dev or test. This gives a deterministic build. Note pipenv is a reference implementation recommened by Python. I fully expect pip to eventually implement it internally.

Installing Pipenv

Assuming you have python installed (duh). On Mac (I exclusively code on mac now) I use brew to manage stuff as mac comes with python 2.x but the world has moved on and you MUST use 3.x, latest version at time of writing is 3.10.

Anyway Install pipenv

pip3 install pipenv

Initialise Your Pipenv shell!

pipenv shell

do this from the source folder where Pipfile is present i.e. root folder.

this will also create your virtual env if its not there, i suggest reading up a bit on pipenv (just a quick brush) so you know the fundamentals as its quite different to virtualenv

Installing this Projects' Dependencies

Make sure that you're in the project's root directory

pipenv install --dev

Running Python and IPython from the Project's Virtual Environment

I find using IPython in command line really useful when I am not in PyCharm IDE. I have included ipython as part of the --dev install above so you should be able to get into it by just doing

❯ ipython
Python 3.10.0 (default, Oct 13 2021, 06:45:00) [Clang 13.0.0 (clang-1300.0.29.3)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.28.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: 

Automatic Loading of Environment Variables

Pipenv will automatically pickup any environment variables declared in the .env file, located in root directory. For example, adding,

DILLY=VANILLY

Will enable access to this variable from python

os.environ['DILLY']

Running Unit Tests

All test have been written using the PyTest package. Tests are kept in the tests folder and can be run from the command line

cd tests
pytest

The conftest.py module is used by PyTest - I've used it to add fixtures which is really cool feature of pytest, I recommend.

Linting Code

I used flake8 for linting code.

pipenv run flake8 example_project

And black for formatting.

 pipenv run black example_project

And you can use pre-commit to hook it all up (incl docs) so you never have to actually do anything manually by hand.

Static Type Checking

I think this is very useful. Think of all the times we said ah it might break some import or something but we wont know until we run, sure we can do extensive tests (we should) but this is like being able to do a compile of python and find problems.

it will barf about pandas/numpy etc which doesnt have stubs, so ignore it for now. am using MyPy package. You can configure what it does with mypy.ini options should be in their docs.

Also note Data Science Types is trying to fix above problem - but I have not tried it.

To run mypy do >

pipenv run python -m mypy example_project/*.py

MyPy options for this project can be defined in the mypy.ini file that MyPy will look for by default. For more information on the full set of options, see the mypy documentation.

Examples of type annotation and type checking for library development can be found in the py_pkg.curves.py module. This should also be cross-referenced with the improvement to readability (and usability) that this has on package documentation.

some terminal output from running above stuff

❯ pipenv run python -m mypy example_project/*.py
Loading .env environment variables...
example_project/example_module.py:12: error: Skipping analyzing "pandas": found module but no type hints or library stubs
example_project/example_module.py:12: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports


❯ pipenv run flake8 example_project
Loading .env environment variables...
example_project/__version__.py:11:13: W292 no newline at end of file
example_project/cli.py:14:10: E211 whitespace before '('
example_project/cli.py:14:20: W292 no newline at end of file

❯ pipenv run black example_project
Loading .env environment variables...
reformatted example_project/__version__.py
reformatted example_project/cli.py
reformatted example_project/example_module.py

Documentation

The documentation in the docs folder has been built using Sphinx. Its a powerful framework that can be used to generate docs on the fly into various formats.

I generated the initial source using:

sphinx-quickstart docs

And then you'd hope to see auto generated docs based on docstring but sadly it isnt so auto-magical... I had to add them in manually per module in docs/source/index.rst where you see I reference 2 files modules.rst and modules_test.rst

And then I did:

cd docs
make html

I like that sphinx ships with make, and we can definitely look to using make as our over-arching tool, plays in nicely with c++.

But alternatively you could also generate the docs by doing:

pipenv run sphinx-build -b html docs/source docs/build/html

Also you obviously should source pipenv shell, and you dont have to actually run pipenv run, but I just show the foolproof way here.

The resulting HTML documentation can be accessed by opening docs/build/html/index.html in a web browser.

If you are curious you'll see I've had to do quite bit of customisation for the config file so that it could generate the docs > docs/source/config.py

I also explored creating PDF docs, I think these are really important if wanting to send to end users, and that can be done using an addon called LatEx, but I havent set it up as yet - but I have explored how to do it.

Building Deployable Distributions

Finally to package this into a wheel! do the following:

pipenv run python setup.py bdist_wheel

This will create build, example_package.egg-info and dist directories. whl should be in dist.

Annoyingly, you cant use pipfile for setup.py requirements, so I had to take a shortcut of generate the requirements.txt by doing

pipenv run pip freeze > requirements.txt

and i wrote a func in setup.py to read the file and use the it to generate install_require, so that the generated wheel installs all the dependencies.

But I make persist requirements.txt in git intentionally, so you generate it everytime you want to create a distributable, i suppose it could be made part of a make command.

Bitflip Fault Simulation Platform by Daniele Rizzieri (2021)

BFSP [v1.05] Bitflip Fault Simulation Platform by Daniele Rizzieri (2021) The platform injects a random bitflip in each of N copies of a binary file.

Daniele Rizzieri 2 Nov 05, 2022
use Notepad++ for real-time sync after python appending new log text

FTP远程log同步工具 使用Notepad++配合来获取实时更新的log文档效果 适用于FTP协议的log远程同步工具,配合MT管理器开启FTP服务器使用,通过Notepad++监听文本变化,更便捷的使用电脑查看方法注入打印后的信息 功能 过滤器 对每行要打印的文本使用回调函数筛选,支持链式调用

Liuhaixv 1 Oct 17, 2021
python package to showcase, test and build your own version of Pickhardt Payments

Pickhardt Payments Package The pickhardtpayments package is a collection of classes and interfaces that help you to test and implement your dialect of

Rene Pickhardt 37 Dec 18, 2022
PyPI package for scaffolding out code for decision tree models that can learn to find relationships between the attributes of an object.

Decision Tree Writer This package allows you to train a binary classification decision tree on a list of labeled dictionaries or class instances, and

2 Apr 23, 2022
mrcal is a generic toolkit to solve calibration and SFM-like problems originating at NASA/JPL

mrcal is a generic toolkit to solve calibration and SFM-like problems originating at NASA/JPL. Functionality related to these problems is exposed as a set of C and Python libraries and some commandli

Dima Kogan 102 Dec 23, 2022
Node editor view image node

A Blender addon to quickly view images from image nodes in Blender's image viewer.

5 Nov 27, 2022
Functional interface for concurrent futures, including asynchronous I/O.

Futured provides a consistent interface for concurrent functional programming in Python. It wraps any callable to return a concurrent.futures.Future,

A. Coady 11 Nov 27, 2022
A program to generate random numbers b/w 0 to 10 using time

random-num-using-time A program to generate random numbers b/w 0 to 10 using time it uses python's in-built module datetime and an equation which retu

Atul Kushwaha 1 Oct 01, 2022
Exploring basic lambda calculus in Python

Lambda Exploring basic lambda calculus in Python. In this repo I have used the lambda function built into python to get a more intiutive feel of lambd

Bhardwaj Bhaskar 2 Nov 12, 2021
A streamlit app for exploring image search results from HuggingPics

title emoji colorFrom colorTo sdk app_file pinned huggingpics-explorer 🤗 blue red streamlit app.py false huggingpics-explorer A streamlit app for exp

Nathan Raw 4 Sep 10, 2022
Checkers Project Built Using Python

Checkers Project Built Using Python

Meekness Anyaeche 1 Nov 08, 2021
Imitate Moulinette written in Python

Imitate Moulinette written in Python

Pumidol Leelerdsakulvong 2 Jul 26, 2022
Visualization of COVID-19 Omicron wave data in Seoul, Osaka, Tokyo, Hong Kong and Shanghai. 首尔、大阪、东京、香港、上海由新冠病毒 Omicron 变异株引起的本轮疫情数据可视化分析。

COVID-19 in East Asian Megacities This repository holds original Python code for processing and visualization COVID-19 data in East Asian megacities a

STONE 10 May 18, 2022
A web interface for a soft serve Git server.

Soft Serve monitor Soft Sevre is a very nice git server. It offers a really nice TUI to browse the repositories on the server. Unfortunately, it does

Maxime Bouillot 5 Apr 26, 2022
Shared utility scripts for AI for Earth projects and team members

Overview Shared utilities developed by the Microsoft AI for Earth team The general convention in this repo is that users who want to consume these uti

Microsoft 38 Dec 30, 2022
An example repository for how to generate results using PyBaMM

PyBaMM results This repository provides a template for generating results (for example, for a paper) using PyBaMM Installation Install PyBaMM using a

PyBaMM Team 7 Oct 09, 2022
This repo is a collection of programs and websites templates too

📢 Register here for Hacktoberfest and make four pull requests (PRs) between October 1st-31st to grab free SWAGS 🔥 . IMPORTANT While making pull requ

Binayak Jha - 2 7 Oct 03, 2022
Sheet2export - FreeCAD macro to export spreadsheet

Description This is FreeCAD macro to export spreadsheet to file.

Darek L 3 Jul 09, 2022
little proyect to organize myself, but maybe can help someone else

TaskXT 0.1 Little proyect to organize myself, but maybe can help someone else Idea The main idea is to ogranize you work and stuff to do, but with onl

Gabriel Carmona 4 Oct 03, 2021
Basit bir cc generator'ü.

Basit bir cc generator'ü. Setup What To Do; Python Installation We install python from CLICK Generator Board After installing the file and python, we

Lâving 7 Jan 09, 2022