python 模块
2016-05-03 19:00
543 查看
1.requests
Github: https://github.com/kennethreitz/requests
大神kennethreitz的作品,简易明了的HTTP请求操作库, 是urllib2的理想替代品。requests is an elegant HTTP library.
API简洁明了,这才是Python开发者喜欢的:
2.purl
github: https://github.com/codeinthehole/purl
拥有简洁接口的URL处理器:
3.进程和队列
Queue.qsize() 返回队列的大小
Queue.empty() 如果队列为空,返回True,反之False
Queue.full() 如果队列满了,返回True,反之False
Queue.get([block[, timeout]]) 获取队列,timeout等待时间
Queue.get_nowait() 相当Queue.get(False)
非阻塞 Queue.put(item) 写入队列,timeout等待时间
Queue.put_nowait(item) 相当Queue.put(item, False)
如果main函数写成上面的样本,本来我想要的是将会得到一个队列,将其作为参数传入进程池子里的每个子进程,但是却得到
RuntimeError: Queue objects should only be shared between processes through inheritance的错误,查了下,大意是队列对象不能在父进程与子进程间通信,这个如果想要使用进程池中使用队列则要使用multiprocess的Manager类
4.uuid
1、uuid1()——基于时间戳
由MAC地址、当前时间戳、随机数生成。可以保证全球范围内的唯一性,但MAC的使用同时带来安全性问题,局域网中可以使用IP来代替MAC。
2、uuid3()——基于名字的MD5散列值
通过计算名字和命名空间的MD5散列值得到,保证了同一命名空间中不同名字的唯一性,和不同命名空间的唯一性,但同一命名空间的同一名字生成相同的uuid。
3、uuid5()——基于名字的SHA-1散列值
算法与uuid3相同,不同的是使用 Secure Hash Algorithm 1 算法
5.processbar
6.excel
basic:csv
advanced:读:xlrd,写:xlwt
xlrd,xlwd有一些复杂的合并单元格,单个单元格写入等特性,请自行百度.
Github: https://github.com/kennethreitz/requests
大神kennethreitz的作品,简易明了的HTTP请求操作库, 是urllib2的理想替代品。requests is an elegant HTTP library.
API简洁明了,这才是Python开发者喜欢的:
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass')) >>> r.status_code 200 >>> r.headers['content-type'] 'application/json; charset=utf8' >>> r.encoding 'utf-8' >>> r.text u'{"type":"User"...' >>> r.json() {u'private_gists': 419, u'total_private_repos': 77, ...}
2.purl
github: https://github.com/codeinthehole/purl
拥有简洁接口的URL处理器:
>>> from purl import URL >>> from_str = URL('https://www.google.com/search?q=testing') >>> u.query_param('q') u'testing' >>> u.host() u'www.google.com'
3.进程和队列
Queue.qsize() 返回队列的大小
Queue.empty() 如果队列为空,返回True,反之False
Queue.full() 如果队列满了,返回True,反之False
Queue.get([block[, timeout]]) 获取队列,timeout等待时间
Queue.get_nowait() 相当Queue.get(False)
非阻塞 Queue.put(item) 写入队列,timeout等待时间
Queue.put_nowait(item) 相当Queue.put(item, False)
from multiprocessing import Process #可以通过Process来构造一个子进程 p = Process(target=fun,args=(args)) #再通过p.start()来启动子进程 #再通过p.join()方法来使得子进程运行结束后再执行父进程 #如果需要多个子进程时可以考虑使用进程池(pool)来管理 from multiprocessing import Pool,Queue import os, time def long_time_task(name): print 'Run task %s (%s)...' % (name, os.getpid()) start = time.time() time.sleep(3) end = time.time() print 'Task %s runs %0.2f seconds.' % (name, (end - start)) if __name__=='__main__': print 'Parent process %s.' % os.getpid() p = Pool() q = Queue() for i in range(5): p.apply_async(long_time_task, args=(q,)) print 'Waiting for all subprocesses done...' p.close() p.join() print 'All subprocesses done.'
如果main函数写成上面的样本,本来我想要的是将会得到一个队列,将其作为参数传入进程池子里的每个子进程,但是却得到
RuntimeError: Queue objects should only be shared between processes through inheritance的错误,查了下,大意是队列对象不能在父进程与子进程间通信,这个如果想要使用进程池中使用队列则要使用multiprocess的Manager类
manager = multiprocessing.Manager() # 父进程创建Queue,并传给各个子进程: q = manager.Queue()
4.uuid
1、uuid1()——基于时间戳
由MAC地址、当前时间戳、随机数生成。可以保证全球范围内的唯一性,但MAC的使用同时带来安全性问题,局域网中可以使用IP来代替MAC。
2、uuid3()——基于名字的MD5散列值
通过计算名字和命名空间的MD5散列值得到,保证了同一命名空间中不同名字的唯一性,和不同命名空间的唯一性,但同一命名空间的同一名字生成相同的uuid。
3、uuid5()——基于名字的SHA-1散列值
算法与uuid3相同,不同的是使用 Secure Hash Algorithm 1 算法
5.processbar
from progressbar import ProgressBar import time pbar = ProgressBar(maxval=10) for i in range(1, 11): pbar.update(i) time.sleep(1) pbar.finish() # 60%########################################################
6.excel
basic:csv
advanced:读:xlrd,写:xlwt
CSV模块是Python的内置模块,直接import csv就可调用。csv模块主要就两个函数:csv.reader()——读取csv文件数据,csv.writer()——写入csv文件数据。简单实用。 import csv #写操作 csvfile = file('test.csv', 'wb') writer = csv.writer(csvfile) writer.writerow(['姓名', '年龄', '电话']) data = [ ('a', '25', '1234567'), ('b', '18', '789456')] writer.writerows(data) csvfile.close() #读取操作 csvfile = file('csv_test.csv', 'rb') reader = csv.reader(csvfile) for line in reader: print line csvfile.close()
xlrd,xlwd有一些复杂的合并单元格,单个单元格写入等特性,请自行百度.
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法