Reference site: What you need to know if you use Python! 10 useful libraries
There are many standard or external libraries in Python, but due to their abundance, it can be difficult to know which library to use. This time, I will introduce a Python library that is useful to know from among them.
datetime
A module that handles dates and times. There are convenient and easy-to-use items such as date / time acquisition, character string ⇔ date conversion, and date data n days later and n days ago. For example, you can easily get the current date and time by using a datetime object that can handle dates and times together.
from datetime import datetime
now = datetime.now()
print(now)
# 2016-04-13 02:29:50.464488
shutil
A module that provides a high level of file operations. It's very easy to write operations on copying files and directories.
import shutil
shutil.copy("/src/src.txt", "/hoge/hoge.txt")
Copy the src.txt file to the hoge.txt file.
collections
There are deque (append and pop at high speed), OrderedDict (ordered dictionary), defaultdict (dictionary with default value), Counter (dictionary with counter), etc. We will be indebted to you for efficient algorithm implementation and programming contests.
from collections import Counter
count = Counter('hogehoge')
print count
# Counter({'g': 2, 'e': 2, 'h': 2, 'o': 2})
pdb
A debugger that provides features such as breakpoint setting and single-step execution at the source line level. You can also run it in an interactive shell or as a script file such as .py.
timeit
A module that measures the execution time of Python programs. You can measure the time for each code, so you can measure fine performance.
import timeit
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
tqdm
When you want to check the progress of the loop, you can easily create a progress bar like the one below.
41%||█████████ | 41/100 [00:04<00:05, 10.00it/s]
py2exe
A library that converts Python scripts to .exe for Windows. The Mac version of "py2app" and the general-purpose "PyInstaller" are also famous.
simplejson
JSON encoding / decoding library. There is "json" in the standard library, but it can be used in the same way, and it is characterized by its faster operation.
import simplejson
requests
An easy-to-use HTTP library. There is "urllib" in the standard library, but it was a little inconvenient to use. requests are very easy to use and are often recommended in recent books.
import requests
r = requests.get('URL')
print r.text
pep8
A source code check tool. Python has a style guide called PEP8, which is a common coding convention. With pep8, your code will tell you where the violation is.
Recommended Posts