python 模块

来源:互联网 发布:比特币高频交易算法 编辑:程序博客网 时间:2024/05/16 17:44

1.requests
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_code200>>> r.headers['content-type']'application/json; charset=utf8'>>> r.encoding'utf-8'>>> r.textu'{"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,Queueimport os, timedef 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 linecsvfile.close() 

xlrd,xlwd有一些复杂的合并单元格,单个单元格写入等特性,请自行百度.

0 0
原创粉丝点击