Write a Python package

April 8, 2020 · View on GitHub

Courses

Developer Guide

Structure

Code Format

  • It is good practice to follow well-established code format. Not only it can help you write codes that are nice looking and easy to maintain, it will help others to read and contribute to the code.
  • For Python, the PEP8 style guide is the most important one. Some of these rules feel unecessary and annoying, but there are always good reasons behind them.

Python setup.py file

Readme

Document

General instructions

Tools

Test

-nose2 - Nicer testing for Python * nose2's purpose is to extend unittest to make testing nicer and easier to understand.

Code Coverage

In computer science, test coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs. A program with high test coverage, measured as a percentage, has had more of its source code executed during testing, which suggests it has a lower chance of containing undetected software bugs compared to a program with low test coverage. -- Wikipedia

Optimization

  • Optimizing Python Code - Scipy Lecture Notes

      1. Make it work; 2: Make it work reliably; 3: Optimization
    • No optimization without measuring: profiling and timing
    • Moving computation or memory allocation outside a for loop; Vectorizing for loops; Broadcasting; Use in place operations; Be easy on the memory: use views, and not copies;
  • LSST DM Python performance profiling

    • Very good guide.
  • The Python Profilers

    • Python comes with a series of profiling tools. The most useful ones are cProfile, profile, and pstats (convert profiling results into a report)
  • Profiling Python using cProfile: a concrete case

    • cProfile 对于发现程序中的瓶颈很有帮助
  • line_profiler and kernprof - Line-by-line profiling for Python

    • line_profiler is a module for doing line-by-line profiling of functions. kernprof is a convenient script for running either line_profiler or the Python standard library's cProfile or profile modules, depending on what is available.
    • Can use cProfile to identify "hotspot" (function that is the "bottleneck"), then use line_profiler to exame the issue carefully.

Visualization

Upload Your Package to PyPI

  • PyPI is the Python Package Index. It is a repository of software for the Python programming language.

    • "It helps you find and install software developed and shared by the Python community"
    • Basically, once you upload your project to PyPI, people can use pip install to install it.
  • It is pretty straightforward to upload your project. Please read this tutorial