📚
handbook
  • Introduction
  • 1.前言
    • 前言
    • 鸣谢
  • 2.环境篇
    • 工具部署和使用
      • 团队协作工具
        • Confluence
      • 开发工具
        • Docker
          • 镜像导入导出
          • 安装
        • Docker Compose
      • 持续集成工具
        • Gerrit
        • Sonarqube
          • 分析参数设定
          • Prerequisite
          • 服务端设置
        • Ubuntu Ci Deploy
          • ubuntu使用docker部署jenkins+sonarqube
        • 持续集成部署
      • 文本编辑工具
        • Gitbook相关注意事项
        • Markdown快速入门
      • 版本控制
        • Git
          • 1.基础
            • Git基础(一)
            • Git基础(二)
            • Git基础(三)
            • Git基础(四)
            • Git基础(五)
          • 2.命令详解
            • 命令速查
          • 3.进阶技巧
            • git技巧
      • 自动化测试工具
        • Appium
          • capability参数配置
          • 安装
          • 简介
      • 项目管理工具
        • Jira
    • 开发环境配置
      • 通用
        • Homebrew安装与使用
        • Git服务器添加SSH Key
        • koroFileHeader使用
        • nodejs与npm的安装
        • npm更换国内源
        • pip使用相关
        • PostgreSQL安装与使用
        • proxychain安装与使用
        • shell配置环境变量
        • snapd安装与使用
        • terminal走代理
    • 快捷键速查
      • shell常用快捷键
  • 3.语言篇
    • C
      • 代码规范
      • 语言技巧
    • Cpp
      • 代码规范
      • 基础知识
        • 理解C++中的左值和右值
      • 语言技巧
        • 并发编程
          • 简单的线程池实现
    • Golang
      • 代码规范
        • 避免使用转义字符串
        • 避免参数语义不明确
        • 嵌套式结构体
        • 函数的分组与顺序
        • 函数命名
        • 声明一致性
        • 导入别名
        • 使用字段名初始化结构体
        • 本地变量声明
        • map初始化
        • nil用法
        • 包命名
        • 命名Printf样式的函数
        • 减少嵌套
        • 缩小变量作用域
        • struct引用初始化
        • 测试表声明
        • 顶层变量声明
        • 不必要的else
      • 环境配置
        • 代码检查格式化工具
          • Go Fmt
          • Goimports
          • Golint
          • Go Vet
        • go mod详解
        • golang安装
        • Golang开发环境
        • Troubleshooting
      • 语言技巧
        • 如何分包
    • Java
      • 代码规范
      • 语言技巧
        • 注解编程
        • 动态代理
    • Js
      • 语言技巧
        • Rollup
    • Kotlin
      • 基础知识
        • 写给开发者Kotlin指引(一)
        • 写给开发者Kotlin指引(二)
    • Python
      • 语言技巧
        • Best Practice Of Python S Project Structure
  • 4.规范篇
    • Git message规范
  • 5.技术篇
    • Android技术
      • Hook
        • EdXposed例子
        • Android 10 上安装Magisk和EdXposed
      • Tinker
        • 1.Tinker及其使用
      • 准备
        • ADB连接设备步骤及注意事项
        • adb连接设备
        • aosp编译
      • 基础
        • Binder接口调用的鉴权方法
        • Make 及 Android 编译系统介绍
        • 使用Content Provider为其他应用提供数据
      • 源码阅读
        • Framework源码分析 Looper Handler
        • Framework源码分析 启动流程 ServiceManager的初始化
        • Framework源码分析 启动流程 Zygote启动SystemServer
    • JS Bridge
      • JSBridge初探
    • Kernel技术
      • kallsyms子系统
    • Test技术
      • 软件测试
        • jnekin+sonar 部署 问题总结
        • 性能测试基础
        • 软件测试的背景
        • 测试基础
        • 测试人员的核心竞争力
    • 操作系统原理
      • 处理器如何实现原子操作
Powered by GitBook
On this page
  • 1.Chnagelog
  • 2.README
  • 3.build script
  • 4.setup.py
  • 5.code structure

Was this helpful?

  1. 3.语言篇
  2. Python
  3. 语言技巧

Best Practice Of Python S Project Structure

There's no standard answer about how to structure the python's project.I'll introduce one relatively reasonable solution.Here's the example.

.
├── ChangeLog
├── README.md
├── build.sh
├── build_and_install_egg.sh
├── clean_build.sh
├── docker
│   ├── DockerfileTemplate
│   ├── docker-compose-template.yaml
│   ├── env.sh
│   └── start.sh
├── setup.py
├── src
│   └── salvage
│       ├── __init__.py
│       ├── configuration
│       ├── controller
│       ├── core
│       ├── main.py
│       ├── model
│       ├── repository
│       ├── resource
│       ├── service
│       └── util
└── test
    └── salvage
        └── configuration

1.Chnagelog

Changelog is very important for a project.It records the release history of the project and helps the developer find bugs quickly.Here is the recommended example of a Changelog.

[1.0.0]
* first release

[0.2.1]
* fix the deadlock problem

[0.2.0]
* add multiprocessing and improve the performance

[0.1.0]
* first commit

Normally,we often use three numbers to represent the version number.The first number tracks major changes,the second tracks minor changes,the last tracks patches or bug fix.

2.README

README or README.md is used to introduce the basic information of the project,including :

  • the purpose of the project

  • how to run the project

  • how to develop the project

  • introduce the structure of the code

3.build script

Python use wheel or egg to pack the code,we can use build script to reduce the redundant typing work.Here's the example.

#!/bin/bash

python3 setup.py bdist_egg bdist_wheel

We can also use a clean_build.sh to clean the build products.

#!/bin/sh

if [ -d build ]; then
  rm -dr build
fi
if [ -d dist ]; then
  rm -dr dist
fi
if [ -d src/salvage.egg-info ]; then
  rm -dr src/salvage.egg-info
fi

You maybe want to build and install with easy install.

#!/bin/bash

source build.sh
sudo easy_install dist/salvage-*-py3.8.egg
source clean_build.sh

Cleaning the build products is recommended because it will influence the navigate operation of some IDE.You may modify the built python code ,not the project files.Clean save your time!

4.setup.py

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import setuptools

def find_version(file_name):
    with open(file_name, encoding='utf-8') as file_handle:
        lines = file_handle.readlines()
        latest_version = lines[0].strip(os.linesep).rstrip(']').lstrip('[')
        print("Salvage:", latest_version)
        return latest_version

setuptools.setup(
    name='salvage',
    version=find_version("./ChangeLog"),
    description='toolbox',
    long_description='operation_toolbox',
    long_description_content_type='text/markdown',
    # packages=['salvage'],
    packages=setuptools.find_packages(where='src', include=['salvage*', ]),
    package_dir={'': 'src'},
    install_requires=[
        'requests',
        'pymysql',
        'wisbec',
        'bottle',
        'bottle-redis',
        'sqlalchemy',
        'sqlalchemy_utils',
        'tldextract',
        'redis'
    ],
    package_data={
        '': [
            'www/*',
            'config/*'
        ]
    },
    include_package_data=True,
    entry_points={
        "console_scripts": [
            "salvage = salvage.main:main",
        ]
    },
    python_requires='>=3.7,<3.9',
    platforms=['any'],
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: Unix',
        'Operating System :: POSIX',
        'Operating System :: Microsoft :: Windows',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: Implementation :: CPython',
        'Programming Language :: Python :: Implementation :: PyPy',
        'Topic :: Utilities',
    ],
    zip_safe=False)

setuptools use setup method to make the package.We just need to specify some parameters.Here's the meaning of some important arguments.

  • name:the name of the project

  • version:the version of the project ,you can specify it each time you make changes to your project.I recommend use a simple function find_version to read the version number from your Changelog file.You can just cpoy the function to your setup.py ,feel free to use it😏!

  • description:the description of your project .

  • long_description:similar to description ,not important.

  • packages:this parameter is used to specify which Python packages you want to pack.Usually ,we use setuptools.find_packages to find the Python packages.where means the package location ,include means package name .You can use the shell blob to represent your Python package name.Here the salvage* means all the Python packages which has salvage as the prefix .For example ,salvagecore and so on.You can also use exclude to exclude some Python packages which you do not want to include in the package.

  • package_dir :the : in the example means root directory ,src means the Python package dir.

  • install_requires:the dependency of the project.easy_install and pip will automatically install these packges you specifed .

  • package_data:the resource file you want to pack .Sometimes you need to add some non-python file in your package.The '' in the example means root directory.**Attention,these means the package root.setuptools will look up in all of your target packages' directories.Here is the files in src :

    .
    └── salvage
        ├── __init__.py
        ├── main.py
        ├── resource
        │   ├── __init__.py
        │   ├── config
        │   │   ├── dev.ini
        │   │   └── release.ini
        │   ├── runtime.py
        │   └── www
        │       ├── adb.bundle.js
        │       └── index.html
        └── util
            └── __init__.py

    the www/* means pack all files in www directory ,so adb.bundle.js and index.html will be include in the package.

  • include_package_data:if this parameter is True ,the package_data make sense .

  • entry_points:this argument specify the entry point of your package.console_scripts means you can run the code from console after you installed the package.salvage = salvage.main:main means the console script name is salvage and the script entry point is the main function in salvage package's main.py file.Python will generate a script file on the PATH ,which is usually /usr/local/bin.

  • python_requires:this stipulated the ranges the Python version of your packages.>=3.7,<3.9 means you can use this package with Python 3.7/3.8/3.9.

  • platform:this means the platform of your package ,any means all platform is compatible .

5.code structure

I recommend use src to place all of your code and test to place your test code.You can also use the project name to replace src.

Previous语言技巧Next4.规范篇

Last updated 4 years ago

Was this helpful?

We always use setuptools as the package tools.The documentation of setuptools is .I'll only give some instructions about the basic usage of this tool base on one example.

here