More development, tools with more, will inevitably be curious about the source code of some libraries, want to know how to use what skills, knowledge. But most of the time, we will feel at a loss what to do. Here is my experience.

The first thing to look at is a library that you use a lot in your work, so you’ll at least know what it does. If you don’t even know what the function is, then you certainly have no purpose to read the source code. It’s easy to get lost in looking at the source code without a problem, and even if it takes time, the results are not good.

Then, at the beginning of reading the source code of some libraries, it is best to choose some less code to feel the first. Don’t think that the amount of code is not necessary to see, in fact, many small projects code style and skills, are worth learning. In later big projects, we may also encounter similar skills, this time we are relatively experienced.

Finally, whether you’re working on a small project or a large one, whether you’re doing research or something else, it’s good to have a general structure in mind. It is easy for us to encounter a point to read, at this time, we do not need to go to the dead knock, first understand its function is what. When you connect the dots throughout the project, you get a better idea of the design philosophy behind the project. As for what we failed to understand and fall behind before, it may be due to some skills we have not been exposed to before, or some theoretical knowledge we lack. At this time, we can slowly make up for it. In this way, we can be a certain improvement.

What libraries should we look at? I combined with their own recommendation of a few, hoping to trigger everyone’s inspiration.

As anyone who has read my previous post knows, I prefer to use the Kennethreitz/Records library when supporting export work. Sometimes empty, want to see how it is implemented, just, this library code is not much, the core logic is still a single file, I gave it to see. After watching it, I still feel like I got some code tricks. The other library, Kennethreitz /tablib, also has a small amount of code, which you can check out if you are interested. There is also the Gleitz/Howdoi library I mentioned earlier, which is also a single file, so it’s good to check it out. Similar projects also do not mean that the more the better, find a few recognized relatively good line.

I mainly write back end, so I use Flask, SQLAlchemy, Celery, etc. When encountering many business requirements, I will gradually find myself uncertain about many details. Flask, for example, can run in multiple threads, but how does the framework guarantee that the current request global variable belongs to the current thread? After looking at the source code, there is a Local class that uses a little trick (thread ID) to make it look like data is isolated between threads. How Flask supports WSGI? Look at the Werkzeug library. How to implement template rendering can be seen in the Jinja library. It’s much better to look at the code when you have a problem. Gradually, the general structure of the Flask becomes clear, and we can dig for points that we might not have understood before. If you are doing the back end I recommend looking at the Flask, Werkzeug, Requests libraries and then SQLAlchemy, Celery etc for your own use.

Once you know which libraries to look at, how do you get started? Flask for a demonstration.

First, clone the library locally, go to the project root directory, and execute the command

pip install --editable .
Copy the code

Flask is installed in the current directory so we can modify the tests.

Finally, run a Demo. Go to the examples/ Flaskr directory and follow README

(1) Edit the configuration

(2) PIP install –editable

(3) export FLASK_APP= FLASKR

(4) Flask initDB

Flask run

Finally, open the link http://localhost:5000 to see that the program has started normally. If there is a problem, Baidu/Google can usually solve it.

Here, go to the source code you are interested in it. If necessary, modify the code in the framework or FlaskR and reinstall it to validate your ideas.

This article was first published on the public account “Little Back end”.