Python 代码片段整理
1、numpy.random.shuffle(x)
1 import numpy as np 2 3 x = [] 4 for i in range(10): 5 x.append(i) 6 7 print(x) 8 np.random.shuffle(x) 9 print (x)
2、python 下载文件 urllib
1 import urllib.request 2 import os 3 import sys 4 import tarfile 5 6 def download_and_uncompress_tarball(tarball_url, dataset_dir): 7 """Downloads the `tarball_url` and uncompresses it locally. 8 Args: 9 tarball_url: The URL of a tarball file. 10 dataset_dir: The directory where the temporary files are stored. 11 """ 12 filename = tarball_url.split('/')[-1] 13 filepath = os.path.join(dataset_dir, filename) 14 15 def _progress(count, block_size, total_size): 16 sys.stdout.write('\r>> Downloading %s %.1f%%' % ( 17 filename, float(count * block_size) / float(total_size) * 100.0)) 18 sys.stdout.flush() 19 filepath, _ = urllib.request.urlretrieve(tarball_url, filepath, _progress) 20 print() 21 statinfo = os.stat(filepath) 22 print('Successfully downloaded', filename, statinfo.st_size, 'bytes.') 23 tarfile.open(filepath, 'r:gz').extractall(dataset_dir) 24 25 DATA_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz' 26 DATA_DIR = 'data' 27 28 29 download_and_uncompress_tarball(DATA_URL, DATA_DIR)
a).使用 urllib.request.urlretrieve下载
b).内嵌入了一个 回调函数 _progress,显示其进度
c).使用 tar.file.open 进行解压