Teach you to read Python open source project code

 

https://zhuanlan.zhihu.com/p/22275595?refer=python-cn

Many people on Zhihu ask and pay attention to the question of reading open source project code, such as " Beginner Python, which Pythonic source code is recommended to read? ", " What are the Python projects in github that are suitable for newcomers to read? ", " How to read And learn the source code of some excellent open source frameworks? " etc. This article will discuss this topic from two aspects: recommended Python projects to read and how to read.

There are three main reasons for reading Python open source project code:

1. There are some problems encountered in the work process, and websites such as Google and StackOverFlow cannot find a solution, so they can only go to the source code.

2. I am very interested in some projects or directions and want to go deeper.

3. When learning encounters a bottleneck, it is necessary to learn from the experience and usage of open source projects to improve.

Reading open source projects without purpose is hooliganism. Time wasted, but little to be learned. How to read according to your own situation?

1. Match your interests and work. For example, I don't have the opportunity to use Celery at work, but I don't want to build a wheel myself. What do I do by reading its source code? So choose from those items that you can usually come into contact with. For example, I definitely don't look at Django's code, because I rarely encounter it in my daily work, and it's fine if I encounter it.

2. Just look at one or two typical ones in one direction. For example, for web frameworks, I have only seen the source code of Bottle and Flask (in fact, I have seen Django's before, but I have only tasted it), and I have seen Bottle several years ago. It's not that it's better to have more piles, but sometimes it's confusing to choose too many.

3. Select projects with different code amount and complexity at different technical stages, which will be specifically recommended below.

4. Be clear about the purpose of your code. Are you looking at the code to understand how people design, debug bugs, or just want to learn the correct programming usage? In fact, there is no need to dig into the details of each code. Sometimes when you look at it as a black box, it is enough to know the input and output.

my personal preference

It’s similar to looking at other people’s code at work. Basically, everyone, every project, and every team has their own style of writing code, such as variable naming style, the use of certain language features, code specification requirements, directory style, etc. In fact, open source projects The author of . Look at code, such as people (team). First, let me introduce my preferences (in order of priority):

1.  kennethreitz . Author of requests and python-guide. He also has a very inspiring story. If you are interested, you can see  who said that programmers are not potential stocks?

2.  mitsuhiko . flask, Jinja2, werkzeug Japanese flask-sqlalchemy author.

3.  sigmavirus24 . Main contributor and maintainer of flake8, pycodestyle (formerly pep8), requests, urllib3 and other projects.

4.  ask . Author of Celery and related dependencies.

5.  ajdavis . Major contributor to projects such as mongo-python-driver (pymongo), tornado, etc.

6.  bitprophet . Author of fabric, paramiko (Python's ssh library).

The top 2 are recognized as the best and most creative engineers who code in Python.

Recommended Reading Items for Beginners

Beginners can first read some projects with a relatively small amount of code, preferably single-file projects:

1. GitHub - kennethreitz/pip-pop: Tools for managing requirements files.

2. GitHub - kennethreitz/envoy: Python Subprocesses for Humans™.

3. GitHub - kennethreitz/records: SQL for Humans™

4. GitHub - mitsuhiko/pluginbase: A simple but flexible plugin system for Python.

5.  GitHub - mitsuhiko / pipsi: pip script Installer

6. GitHub - mitsuhiko/unp: Unpacks things.

7. GitHub - chrisallenlane/cheat

8. GitHub - jek/blinker: A fast Python in-process signal/event dispatching system.

9. GitHub - mitsuhiko/platter: A useful helper for wheel deployments.

10. GitHub - kennethreitz/tablib: Python Module for Tabular Datasets in XLS, CSV, JSON, YAML, &c.

Looking at the code is mainly to understand the way others write code, and the grammar to practice these contents. After reading it, you can write a project yourself based on the problems that these projects can solve.

Advanced Reading Program

进阶的时候就要阅读一些相对复杂的项目,它们能帮助你提升Python编程技巧:

1. faif/python-patterns。使用Python实现一些设计模式的例子。

2. pallets/werkzeug。flask的WSGI工具集。其中包含了实现非常好的LocalProxy、cached_property、import_string、find_modules、TypeConversionDict等。

3. bottlepy/bottle。阅读一个Web框架对Web开发就会有更深刻的理解,flask太大,bottle就4k多行,当然如果你有毅力和兴趣直接看flask是最好了的。

4. msiemens/tinydb。了解用Python实现数据库。

5. coleifer/peewee。了解ORM的实现。

6. pallets/click。click已经内置于在flask 0.11里,提供命令行功能,值得阅读。

7. mitsuhiko/flask-sqlalchemy。了解一个flask插件是怎么实现的。

除此之外Web开发者可以阅读一些相关的项目:

1. runscope/httpbin。使用flask,网站是httpbin(1): HTTP Client Testing Service

2. jahaja/psdash。使用flask和psutils的获取Linux系统信息的面板应用。

3. pallets/flask-website。 flask官方网站应用。

4. pypa/warehouse。如果你使用pyramid,这个新版的PYPI网站,可以帮助你理解很多。

当然,2个学习flask重要的资源必须爆一爆:

1. GitHub - realpython/discover-flask: Full Stack Web Development with Flask

2. The Flask Mega-Tutorial。 这个就是《Flask Web开发:基于Python的Web应用开发实战》的原始博客。

500lines

推荐一个非常厉害的项目 GitHub - aosabook/500lines: 500 Lines or Less, 它里面包含了22个由该领域的专家完成,用不到500行的代码实现一个特定功能的子项目。连Guido van Rossum都亲自来写基于asyncio爬虫了,Nick Coghlan、ajdavis也出场了。更具体的介绍可以看Python 的练手项目有哪些值得推荐? - 小小搬运工的回答

怎么阅读开源项目源码

我基于个人兴趣和日常开发需要阅读过一些开源项目,我列一些比较通用的经验和感悟:

1. 不要畏惧。我发现大家都经常会感叹XXX强大,YYY流行,无形中你会把它放在一个不可触及到的地位,感觉它很难,而令自己不敢去挑战它。其实是人就会产出bug,假如你发现它有问题,就应该抓住机会去验证它。这个过程中,它的神秘感也就消失了,有过这么几次经验你就有信心了。其次是不要怕你提交的PR被拒绝。这是非常正常的,我有很多PR是被拒绝的,尤其是给标准库提交的Patch,绝大多数都被拒绝了。

2. 带着问题去阅读代码,这也是我认为最有效的方式。这会让你在阅读时候有个主线,比较有针对性。

3. 断点调试。在Python代码中使用pdb一般不太好使,因为代码复杂的话,这种断点需要你使用多个n跳到对应的位置,我一般都是先抛出异常,然后使用pdb的up/down/n等命令调试。当然在目标位置添加一些print日志或注释部分代码然后直接使用exit()退出也是可以的。

4. 善用文档,官网例子。阅读一个项目一开始会有点无从下手,那么就先好好这些内容,它们一般都是作者表达这个项目的第一个入口。quickstart、tutorial等内容中的最小化的例子其实就是最好的阅读入口,先去看这些引用的模块和调用的对应方法或者函数的对应实现,从下至上去阅读。

5. 理解作者的思考方式。不同的项目要有不同的思考方式来阅读,不要拧着自己的习惯去阅读,这样会很累,得尝试接受别人的观点,甚至于改变自己。

6. 阅读项目的早期版本。一些项目随着时间演进已经非常复杂了,读起来有难度,那么你可以回到项目的早期版本上,先去看相对简单地版本,然后设置几个时间点或者版本节点,渐进的来阅读。

7. 记忆并绘制项目架构。项目就是一堆代码的组合,除了学习编程技巧,还要了解项目的架构决策,这对于未来自己写大型项目非常用用。这种理解越补充,你会对它就越来越清晰。

8. 参与。如果只是看和思考而不参与,其实效果大打折扣。我建议要参与一些issue讨论、向作者提问、提PR添加新的功能、帮助改善文档等等。很多时候你理解的其实和作者是有冲突的,无论是你说服他和被他说服,对自己的成长都是很有帮助的。

 

无耻的广告:《Python Web开发实战》上市了!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326625613&siteId=291194637