新建项目
一个完整的 Python Project 应该包括
project/
src/ --- source code
tests/ --- test code
__init__.py
conftest.py
test_xxx.py
setup.py --- organize files into a package
setup.cfg
MANIFEST.in
requirements.txt
README.md
pyproject.toml
其中,setup.py 用来构建包,譬如:
from setuptools import find_packages, setup
setup(
name='flaskr',
version='1.0.0',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=[
'flask',
],
)
并可以用 MANIFEST.in 补充添加二进制文件。此外, tests
为测试文件夹,测试时 coverage run -m pytest
, coverage report
. 可用 setup.cfg
配置
[tool:pytest]
testpaths = tests
[coverage:run]
branch = True
source =
src
用 pipreqs
生成 requirements.txt
:
pipreqs src
选择 flake8
作为 linter, black
作为 formatter
,配置 pyproject.toml
[tool.black]
line-length = 99
target-version = ['py38']
[tool.isort]
profile = 'black'
src_paths = ['src']
line_length = 99
lines_after_imports = 2
# force_single_line = 'True'
force_sort_within_sections = 'True'
order_by_type = 'False'
Thread
Python 可支持多线程和多进程
- multi-thread: 由于 CPython 的 GIL 存在,所以近乎并发
Thread(target=func)
class my_thread(Thread)
- multi-procses
进程间通信
- Event
- Condition
- Queue
信息隔离:Local 定义在主线程中,主线程与子线程,子线程与子线程中 local 均为不同变量
协程:使用 yield, async 实现
Miscellaneous
-
iter(partial(func, ""), "")
-
for x,y in product(X,Y)
-
inspect
库 -
python -m site
检查 python 路径 -
timeit.timeit(func, number=5)
-
@functools.lru_cache
-
@atexit
-
回调
with contextlib.ExitStack() as stack: stack.callback(cb)
-
run a command:
result = subprocess.run(cmd_list, stdout=subprocess.PIPE).stdout.decode('utf-8')
-
正则表达式
re.findall(r"\d+", s)[0]
-
sched 包
def func(sc, arg):
# do something
sc.enter(interval, 1, func, (sc, arg))
s = sched.schedular(time.time, time.sleep)
s.enter(interval, 1, func, (s, arg))
s.run()
MVC
- M(Model): CRUD
- V(View, output): html, xml, json
- C(Control, input): 转换格式,验证输入