Python is currently my primary programming language for work, computation, and data mining (except for symbolic computation, where I use Mathematica). Of course, basic Python functionality is not inherently powerful, but it excels due to its massive ecosystem of third-party extension libraries. When choosing a third-party Python library, I always consider it carefully, hoping to pick the simplest and most intuitive one (as I am not particularly clever and cannot handle overly complex tools).
In terms of data processing, I use Numpy and Pandas the most; these two are absolute kings in the field. I should also mention Scipy, though I rarely use it directly, usually calling it indirectly through Pandas. For visualization, Matplotlib is the obvious choice. For modeling, I use Keras to build deep learning models directly; Keras has already become a very popular deep learning framework. If I am doing text mining, I usually use jieba (for word segmentation) and Gensim (for topic modeling, which includes models like word2vec). For machine learning, there is the popular Scikit Learn, though I use it less frequently. Regarding networking, I use requests for writing crawlers, as it is a very user-friendly library. For web development, I use bottle, which is a mini-framework in a single file where everything is defined by the user; I don’t write large-scale websites, usually just simple interfaces. Finally, for parallelism, I generally use multiprocessing directly.
However, none of the above are what I want to recommend in this article. What I want to recommend are two libraries that can permeate your daily coding. They implement functions we often need but without requiring much additional code—they are truly eye-opening.
1. tqdm
The introduction to tqdm can be summarized with a single GIF.
Simply put, it is used to display progress bars. It is beautiful,
intuitive to use (just wrap your loop with tqdm), and it
has almost no impact on the efficiency of the original program. It is
truly "too powerful and beautiful"! It makes writing programs with long
execution times much more comfortable.
2. retry
As the name suggests, retry is used to implement retry logic. We often need retry functionality; for example, when writing a crawler, network issues may cause a crawl to fail, necessitating a retry. Typically, I would write it like this (retrying every two seconds, for a total of 5 times):
import time
def do_something():
xxx
for i in range(5):
try:
do_something()
break
except:
time.sleep(2)This is somewhat cumbersome. With retry, you only
need:
from retry import retry
@retry(tries=5, delay=2)
def do_something():
xxx
do_something()In other words, you just need to add @retry before the
function definition.
Python is indeed absolutely worry-free.
When reprinting, please include the original address of this article: https://kexue.fm/archives/3902
For more detailed reprinting matters, please refer to: Scientific Space FAQ