@kpatrick
2022-07-05T20:32:17.000000Z
字数 5575
阅读 140
Git
产险
代码格式,PEP指南:
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Python 库的命名约定有点混乱,不可能完全一致。但依然有些普遍推荐的命名规范的。新的模块和包 (包括第三方的框架) 应该遵循这些标准。对不同风格的已有的库,建议保持内部的一致性。
如今代码库中有多种命名风格:
有下划线的情况:
from M import
不会导入以下划线开头的对象。Tkinter.Toplevel(master, class_='ClassName')
YES
# 对准左括号
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 不对准左括号,但加多一层缩进,以和后面内容区别。
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
# 悬挂缩进必须加多一层缩进.
foo = long_function_name(
var_one, var_two,
var_three, var_four)
NO
# 不使用垂直对齐时,第一行不能有参数。
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 参数的缩进和后续内容缩进不能区别。
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
YES
def add(a: int, b: int) -> int:
return a + b
def sub(a: int, b: int) -> int:
return a - b
class Calculator:
def __init__(self):
pass
def add(self, a: int, b: int) -> int:
return a + b
def sub(self, a: int, b: int) -> int:
return a - b
变量尽量只用于模块内部,约定类似函数。
对设计为通过 from M import
来使用的模块,应采用 __all__ 机制来防止导入全局变量;或者为全局变量加一个前置下划线。
......
pre-commit
, prepare-commit-msg
, commit-msg
, post-commit
等,主要在服务端接收提交对象时、推送到服务器之前调用。pre-receive
, post-receive
, update
等,主要在服务端接收提交对象时、推送到服务器之前调用。但是如果直接通过编写 git hooks 脚本来实现代码规范检查,会有如下的一些问题:
为了更方便的管理 pre-commit 的设置,于是有了一个同名的工具项目 pre-commit,一个用于管理和维护多语言预提交挂钩的框架。官网地址:https://pre-commit.com/
pip install pre-commit
.pre-commit-config.yaml
,需要手动在根目录创建此文件。pre-commit install
pre-commit run --all-files
根据个人开发经验,提交代码前完成对基础格式、文档、import 和 type hint 等方面进行检查,推荐的插件有以下 6 个:
通过此配置可以完成上述 6 个插件的安装(通过例子进行演示):.pre-commit-config.yaml
- repo: https://github.com/pre-commit/pre-commit-hooks.git
rev: v4.3.0
hooks:
- id: check-merge-conflict
- id: check-symlinks
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/fsouza/autoflake8
rev: v0.3.2
hooks:
- id: autoflake8
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
- repo: https://github.com/pycqa/pydocstyle
rev: 6.1.1
hooks:
- id: pydocstyle
args: [--convention, google, --add-ignore, D100]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.961
hooks:
- id: mypy
args: [--disallow-untyped-defs, --ignore-missing-imports]
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
当前基于 Git 的分支策略主要有 Gitflow、GitHub flow、GitLab flow 三个派别。如果无需执行严格的版本计划和保证定期发布相关需求的特性,一般可以采取最简便的GitHub flow。
GitHub flow 推荐的策略可以分成以下6步:
一般而言,当我们需要从一个分支获取信息,并且合并到当前分支时,我们可以采用 Rebase 或者 Merge 的操作。如图所示:你在一个 Feature 分支进行新特性的开发,与此同时,master 分支的也有新的提交。
git merge master feature
Merge 特点:
git log --graph
或者 GUI 工具时,如果 commit 比较频繁,看到分支很杂乱。
git checkout feature
git rebase master
Rebase 特点:
Rebase 冲突:
合并时如果出现冲突需要按照如下步骤解决
注意,不能在公共分支上执行 Rebase !Rebase 后不仅历史发生改变,所有开发者都受其影响。
从观念上,要把 Code Review 置于与代码开发和测试同等重要的地位。具体要做到以下几点: