The benefits of reading open source projects have been demonstrated by many people. Whether it is to increase the understanding of tools used in daily development, or to learn to divide the project structure and improve the quality of the code better, reading open source projects is essential.
There are some interesting findings during the progress.Take the famous requests which gained over 3.5K stars as an example. There is a file (core.py) in the first version v0.2.3 listed on github that used tab as a whitespace,it certainly doesn’t satisfy the requirements of PEP8 (:D
What is Pythonic Code
Pythonic code is to implement code in a pythonic way.Having a summary:
OOPconsiders code reuse and extension. Many can use a basic class / ABC to provide an interface, other sub-class inherit from it just needs to implement the necessary function.
Customize BaseException, such as
class ProjectBasedException(Exception), and then use the code of the various inheritance exception classes to handle the specific exception, so that the prompt information can be reported more specifically.
Define some small and common functions in utils, abstract the multiplexed code into @decorators; define the configuration files that will be used in settings, such as url, to avoid hard-coded.
Defensive programming. Although _prefix can be used to define the private attribute to some extent, the parameters passed to the API must be type tested. Use a reasonable data structure or other tools (redis/celery) to limit the use of resources.
meta-programming implements the
Standard comments for
docstringand writing unit tests, logging important actions to files.
On the basis of the above, some details, like replacing the string splicing with
''.join, replacing the loop with
list-comprehensionalso should be noticed.
Not all high-star projects on Github are useful. For example, the code style of a
1000+ star project is not good, and a lot of code does not meet the principle of
DRY(Don't Repeat Yourself) . There are still many projects that directly hack Python itself(so many variables and functions begin with
__, it’s hard to understand what the logic behind it is)
Although the project has been deprecated in the description, it can still run smoothly. A forum project that includes the
a. The implementation of OOP is very thorough, the following is an example:
b. there is a decorator
@require_user, which is the permission management for the user. A user may have multiple roles. If you define a decorator for each of these roles, there will be too many repetitions. In this case let’s see what the author did:
zhihu-oauth by @7sDream
The second project is zhihu-oauth written by @7sDream. The overall structure is very beautiful, and the meta-programming is also very good. Especially considering that the author and I are peers, it is especially amazing. BTW, I also provide two PRs: pull-27 and pull-28
The author reversely parses the zhihu Android client and encapsulates the oauth interface. Unlike other libraries that use simulated login and Beautifuoup to parse web content, zhihu-oauth can provide a more stable API. And it is also less likely to be blocked by ip.
The whole project is divided into three parts:
oauth for verification,
zhcls for class description, and
client for combining the two to provide a login API.
imZhihuAndroidClient classis defined in im_android.py, inheriting requests.authbase. The methods that are used in building parameters such as api_version/app_version/zpp_za/ua are defined in
__init__. Also use the method self._api_version=api_version or API_VERSION (from setting.py) to allow the user to customize some parameters. The following
__call__(self,r)is the mechanism of authbase and will be called automatically when posting a request.
Before_login_auth.py defines the BeforeLoginAuth class, inherits the imZhihuAndroidClient class above, adds client_id to imZhihuAndroidClient, and uses self._client_id=client_id to execute validation before login. The implementation of
__call__is as follows:
In setting.py, some parameters are used, such as
LOGIN_URL=ZHIHU_API_ROOT+'/signin', all in uppercase.
In token.py, the ZhihuToken class is defined, and the token is generated after the access is known. So obviously, according to OOP’s thoughts, the following job can be done:
self._cretate_at=time.time(), self._expires_in=expires_in initialization tool is defined in
__init__ and these are also provided:
Look again at the implementation of
exception.py. As mentioned earlier,base exception should be provided for the entire project.
Based on requirements,I ever investigaed the use of
jedis to see how it handlers multi-thread situation.
Apache.commons.pool and it is called
super.getResource() method, which in turn calls the
borrowObject method in the pool.In the
borrowObject method, it has implemented the consideration of multithreading: