table of Contents
1 datetime module (dates and times of the standard library)
datetime and timestamp conversion
datetime add or subtract, use this class timedelta
namedtuple function (using the attribute reference tuple element)
a defaultdict (return value may be set when the key does not exist)
OrderedDict (ordered dictionary, iteration by iteration order of addition)
Counter (counter, dict subclass)
3 base64 (with 64 characters to represent arbitrary binary data)
4 struct (bytes and solve other types of conversion)
5 hashlib, Hmac (provide common digest algorithm, MD5, SHA1, etc. .Hmac mixed key)
6 itertools (provides a very useful function for operating the object iteration)
7 contextlib (context management, the use of context management function can be used with)
8 urllib, use the program to perform various http requests, XML, HTMLParser
9 pillow, image processing, https://pillow.readthedocs.org/
10 requests, handling url, convenient than urllib, third-party libraries
12 psutil, monitoring system status
13 virtualenv create a set of isolated operating environment
Built-in module
1 datetime module (dates and times of the standard library)
datetime and timestamp conversion
datetime is the module, the module further comprises a datetime datetime class
from datetime import datetime
# 获取当前日期和时间
now = datetime.now() # 获取当前的datetime
print(now)
# 获取指定日期和时间
dt = datetime(2015, 4, 19, 12, 20) # 用指定日期和时间创建一个datetime对象
print(dt)
# 将datetime转换为timestamp
dt_ts = dt.timestamp() # 调用datetime的timestamp()方法
print(dt_ts)
# 将timestamp转换为datetime
dt2 = datetime.fromtimestamp(dt_ts) # 调用datetime的fromtimestamp()方法传入timestamp参数,转换到了本机时间
dt3 = datetime.utcfromtimestamp(dt_ts) # 转换到utc时间
str with datetime conversion
from datetime import datetime
# str转datetime
cday = datetime.strptime('2019-10-1 19:45:59', '%Y-%m-%d %H:%M:%S') # 转换后是没有时区信息的
print(cday)
# datetime转str
cday_str = cday.strftime(%a %b %d %H:%M:%S)
print(cday_str)
The date and time format strings: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
datetime add or subtract, use this class timedelta
from datetime import datetime, timedelta
now = datetime.now()
now_plus_10days = now + timedelta(days=10)
Converted to UTC time
A datetime time has a time zone property tzinfo, the default is None, so can not tell what time zone unless forced to set a time zone datetime
from datetime import datetime, timedalta, timezone
tz_utc_8 = timezone(timedelta(hours=8)) # 创建时区UTC+8
now = datetime.now() # 本地时间,tzinfo默认为None
print(now)
local_time = now.replace(tzinfo=tz_utc_8) # 强制设置时区为UTC+8
print(local_time)
Time Zone Converter
UTC time can get through utcnow (), then zone tzinfo = timezone.utc setting time as a reference, and then use the time zone when the zone parameters obtained incoming astimezone
from datetime import datetime, timezone, timedelta
utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc) # 拿到utc时间并设置时区
bj_dt = utc_dt.astimezone(timezone(timedelta(hours=8))) # 传入时区参数获得时区时间
The key conversion time zone that a time zone get datetime, obtaining the reference time zone forcibly set, then use datetime with time zone, to convert an arbitrary time zone method by astimeone ()
2 collectioins module
collections python is a collection of built-in module that provides many useful collections
namedtuple function (using the attribute reference tuple element)
namedtuple function to create a custom objects, and specifies the number of elements of the tuple, and attribute can be used instead of an index to refer to an element.
So that we can easily define a data type, it has a tuple invariance, and can be referenced by attribute , easy to use
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y']) # 创建一个类
p = Point(1, 2) # 创建实例
print('Point type:', type(Point))
print(p)
deque two-way list
In order to achieve efficient two-way list insertions and deletions, in addition to the ordinary list method, there popleft () and appendleft () to delete and add the header element
a defaultdict (return value may be set when the key does not exist)
from collections import defaultdict
dd = defaultdict(lambda: 'N/A')
print(dd['a']) # 返回'N/A'而不是报错,其他和dict一样
OrderedDict (ordered dictionary, iteration by iteration order of addition)
Can implement a FIFO dict, when capacity exceeds the limit, delete the first key
ChainMap
ChainMap can string together a group of dict, dict logical form, a dict ChainMap itself, but will find according to the order in dict when looking inside. Application: via the command line, environment variables and parameters to achieve the priority to find the default parameters passed parameters ChainMap
Counter (counter, dict subclass)
For example, counting the number of characters
from collections import Counter
c1 = Counter()
for ch in 'Programming':
c1[ch] += 1
print(c1)
3 base64 (with 64 characters to represent arbitrary binary data)
a base 64 with an arbitrary binary characters to represent a method, a group of 3 bytes, a total of 24, and then divided into 4 groups of 6, 6 according to this lookup table 64 corresponding to the character character. [ 'A', 'B', ... 'a', 'b', ... '0', '1', ... '+', '/'], a 33% increase in length.
3 bytes not used \ x00 up, re-encoding the end plus 1 or 2 equals sign indicates how many bytes make up, automatically remove decoding
import base64
b = base64.b64encode(b'binary string') # 把二进制字节类型的数据根据64字符表进行编码
eb = base64.b64decode(b) # 将编码后的二进制数据进行解码
b = urlsafe_b64encode(b'binary string') # urlsafe的编码 由于+和/在url中不能直接出现 其实是换成了-和_
eb = urlsafe_b64decode(b) # urlsafe解码
Since = b64 may also occur in the coding, but the equal sign in the url, cookies which can cause ambiguity, so a lot of base64 encoding will remove =
import base64
# 能够处理编码后去掉等号的
def safe_base64_decode(s):
x = len(s) % 4
if not x:
return base64.b64decode(s)
else:
s = s.decode(encoding='utf-8') + '=' * x
s = bytes(s, encoding='utf-8')
return base64.b64decode(s)
4 struct (bytes and solve other types of conversion)
https://docs.python.org/3/library/struct.html#format-characters
5 hashlib, Hmac (provide common digest algorithm, MD5, SHA1, etc. .Hmac mixed key)
6 itertools (provides a very useful function for operating the object iteration)
itertools.count (), create an infinite iterator, print out the sequence of natural numbers
cycle (), the incoming sequence continues indefinitely,
REPEAT (), the element continues indefinitely, passing the second parameter may define the number of repetitions
takewhile (condition, seq), under the conditions taken a finite sequence
Provide iterators
catena alberghiera (), the object is a string iteration together to form a larger iterator
GroupBy (), the iterator adjacent repeating elements singled out put together, the selection rules may be provided by the function
7 contextlib (context management, the use of context management function can be used with)
8 urllib, use the program to perform various http requests, XML, HTMLParser
Third party modules
9 pillow, image processing, https://pillow.readthedocs.org/
10 requests, handling url, convenient than urllib, third-party libraries
11 chardet, detection coding
12 psutil, monitoring system status
Get CPU information, memory information, disk information, network information, process information
13 virtualenv create a set of isolated operating environment
- - Create a directory and into the directory
- - Create an independent runtime environment python named venv, use the command: virtualenv --no-site-packages venv
Parameters --no-site-packages have been installed to enable all third-party system package python environment will not be copied, get a clean operating environment
- - into the environment, use the command: source venv / bin / activate
Prompt changed noted, that the current environment is an environment named venv the python, pip installed used in this environment are mounted to the lower package venv environment, the system is not affected python.
- - Quit environment, use the command: deactivate