import glob
import itertools as it
import os
files = glob.glob("*.py") # 使用模式匹配搜索文件
print(files)
def multiple_file_types(*patterns):
return it.chain.from_iterable(glob.glob(pattern) for pattern in patterns)
for filename in multiple_file_types("*.txt", "*.py"):
print(os.path.realpath(filename))
import uuid
print(uuid.uuid1)
import zlib
string = """ xxxx """
compressed = zlib.compress(string)
decompressed = zlib.decompress(compressed)
import atexit
def shutdown():
print("Exception appear!")
atexit.register(shutdown) # 注册异常退出的时候最近处理函数
import pysnooper
@pysnooper.snoop("./log", prefix="-----")
def func():
...
# pip install tqdm
import time
from tqdm import tqdm
from tqdm._tqdm import trange
for i in tqdm(range(100)): # 等同于 for j in trange(100):
time.sleep(0.01)
# 包裹 list
bar = tqdm(list("abc"))
for letter in bar:
bar.set_description(f"Now get {letter}")
# 手动控制更新
with tqdm(total=100) as pbar:
for i in range(10):
pbar.update(10)
pbar = tqdm(total=100)
for i in range(10):
pbar.update(10)
pbar.close()
# 结合 pandas 使用
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0, 100, (10000000, 6)))
tqdm.pandas(desc="my bar!")
df.progress_apply(lambda x: x**2)
find . -name '*.py' -exec cat \\{} \\; | tqdm | wc -l
find . -name '*.py' -exec cat \\{} \\; | tqdm --unit loc --unit_scale --total 857366 >> /dev/null
7z a -bd -r backup.7z docs/ | grep Compressing | tqdm --total $(find docs/ -type f | wc -l) --unit files >> backup.log
一个时间增强库
# pip install arrow
>>> a = arrow.now() # 当前本地时间
>>> a
<Arrow [2018-08-24T07:09:03.468562+08:00]>
>>> arrow.utcnow() # 当前utc时间
<Arrow [2018-08-23T23:11:50.147585+00:00]>
>>> t = a.datetime
>>> type(t)
<class 'datetime.datetime'>
>>> t
datetime.datetime(2018, 8, 24, 7, 17, 14, 884750, tzinfo=tzlocal())
>>> a.timestamp
1535066234
>>> a.year
2018
>>> a.month
8
>>> a.day
24
>>> a.hour
7
>>> a.date()
datetime.date(2018, 8, 24)
>>> a.time()
datetime.time(7, 9, 3, 468562)
>>> a.shift(months=-1)
<Arrow [2018-07-24T07:09:03.468562+08:00]>
>>> a.shift(months=-1).format("YYYYMM")
'201807'
>>> a.shift(months=1)
<Arrow [2018-09-24T07:09:03.468562+08:00]>
>>> a.shift(years=-2)
<Arrow [2016-08-24T07:09:03.468562+08:00]>