python学习九:SocektServer学习、bisect、list的sort详解、清空字典的两种方法、zip初探
来源:互联网 发布:水手怕水 知乎 编辑:程序博客网 时间:2024/06/05 16:40
1. SocketServer学习
SocketServer基于socket提供一套快速建立socket服务器的框架,并可以通过Mix-in的技巧让单线程服务器进化为多线程或多进程服务器。
Socketserver.py里面的类很多,下面一个一个介绍并介绍它们之间关系。
BaseServer、TCPServer、UDPServer,前者是后两者的父类。在父类中实现了TCP与UDP服务器可以共用的方法,并留下了需要在子类中实现的函数。因此在实际工作中,我们一般不应该继承BaseServer类,除非你是为了自己去实现或重载某些方法。另外,还有UnixStreamServer和UnixDatagramServer分别继承自TCPserver和UDPServer。这两个带Unix字样的类是用来构建*nix下本地socket的服务器(本地socket访问速度更快,但只能用在本机的进程间通信)
接下来是ForkingMixIn和ThreadingMixIn两个混合类,它们都提供Server类中process_request方法的新实现,前者在处理每次用户连接的时候都会开启新的进程,而后者会开启新的线程。想要让Server类实现并发处理,只用利用多重继承即可。或者直接使用已经混合好的
class ForkingUDPServer(ForkingMixIn, UDPServer): pass
class ForkingTCPServer(ForkingMixIn, TCPServer): pass
class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
最后没有介绍的是BaseRequestHandler、StreamRequestHandler、DatagramRequestHandler,同Server一样,前者是后两者的父类用来提供公用的方法
通常在使用SocketServer的时候我们只用自己实例化一个合适的Server类,并在实例化时传递其监听的IP、端口,以及之前继承自某个RequestHandler的类(需要自行实现hanle方法,作为server的请求处理器)
2. bisect
This module provides support for maintaining a list in sorted order without having to sort the list after each insertion.
The module is called bisect because it uses a basic bisection algorithm to do its work.
bisect.bisect_left(a, x, lo=0, hi=len(a))
列表a默认升序排列,若a中含有一个或多个x元素,则返回最左边x元素的index,表征插入位置,lo和hi指定作用长度。
bisect.bisect_right(a, x, lo=0, hi=len(a))
bisect.bisect(a, x, lo=0, hi=len(a))
功能同bisect.bisect_left(),但返回最右边x元素的index+1,表征插入位置。
bisect.insort_left(a, x, lo=0, hi=len(a))
执行插入操作,相当于a.insert(bisect.bisect_left(a, x, lo, hi), x)
bisect.insort_right(a, x, lo=0, hi=len(a))
bisect.insort(a, x, lo=0, hi=len(a))
功能同bisect.insort_left
3. list的sort详解
s.sort([cmp[, key[, reverse]]])
5. zip初探
SocketServer基于socket提供一套快速建立socket服务器的框架,并可以通过Mix-in的技巧让单线程服务器进化为多线程或多进程服务器。
Socketserver.py里面的类很多,下面一个一个介绍并介绍它们之间关系。
BaseServer、TCPServer、UDPServer,前者是后两者的父类。在父类中实现了TCP与UDP服务器可以共用的方法,并留下了需要在子类中实现的函数。因此在实际工作中,我们一般不应该继承BaseServer类,除非你是为了自己去实现或重载某些方法。另外,还有UnixStreamServer和UnixDatagramServer分别继承自TCPserver和UDPServer。这两个带Unix字样的类是用来构建*nix下本地socket的服务器(本地socket访问速度更快,但只能用在本机的进程间通信)
接下来是ForkingMixIn和ThreadingMixIn两个混合类,它们都提供Server类中process_request方法的新实现,前者在处理每次用户连接的时候都会开启新的进程,而后者会开启新的线程。想要让Server类实现并发处理,只用利用多重继承即可。或者直接使用已经混合好的
class ForkingUDPServer(ForkingMixIn, UDPServer): pass
class ForkingTCPServer(ForkingMixIn, TCPServer): pass
class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
最后没有介绍的是BaseRequestHandler、StreamRequestHandler、DatagramRequestHandler,同Server一样,前者是后两者的父类用来提供公用的方法
通常在使用SocketServer的时候我们只用自己实例化一个合适的Server类,并在实例化时传递其监听的IP、端口,以及之前继承自某个RequestHandler的类(需要自行实现hanle方法,作为server的请求处理器)
2. bisect
This module provides support for maintaining a list in sorted order without having to sort the list after each insertion.
The module is called bisect because it uses a basic bisection algorithm to do its work.
bisect.bisect_left(a, x, lo=0, hi=len(a))
列表a默认升序排列,若a中含有一个或多个x元素,则返回最左边x元素的index,表征插入位置,lo和hi指定作用长度。
bisect.bisect_right(a, x, lo=0, hi=len(a))
bisect.bisect(a, x, lo=0, hi=len(a))
功能同bisect.bisect_left(),但返回最右边x元素的index+1,表征插入位置。
bisect.insort_left(a, x, lo=0, hi=len(a))
执行插入操作,相当于a.insert(bisect.bisect_left(a, x, lo, hi), x)
bisect.insort_right(a, x, lo=0, hi=len(a))
bisect.insort(a, x, lo=0, hi=len(a))
功能同bisect.insort_left
3. list的sort详解
s.sort([cmp[, key[, reverse]]])
# list.sort()a = [('apple', '4s', 4488), ('apple', '4', 3088), ('samsung', 's4', 5200), ('nokia', '1020', 5999)]a.sort()print aa.sort(key=lambda x: x[2])print a# cmp=lambda x,y: cmp(x.lower(), y.lower()
4. 清空字典的两种方法
# clear dictd = {'name': 1, 'age': 2}d2 = dd = {}print "d2:", d2d3 = d2d2.clear()print "d3:", d3
5. zip初探
# zip()x = [1,2,3]y = [4,5,6]zipped = zip(x, y)print zippedmatrix = [[1,2,3], [4,5,6], [7,8,9]]zipped = zip(*matrix)print zippedroll_matrix = map(list, zipped)print roll_matrix
- python学习九:SocektServer学习、bisect、list的sort详解、清空字典的两种方法、zip初探
- python清空字典的两种方法比较
- Python--清空list的方法
- 清空file的两种方法
- Python学习笔记(七)—— List.sort 与二进制搜索bisect
- Python中list的清空
- SQL Server清空日志的两种方法
- python 字典中取值的两种方法
- python清空列表的方法
- Python遍历列表的方法enumerate,以及清空list方法
- 清空数组的两种方式
- Collections.sort方法对list排序的两种方式
- Collections.sort方法对list排序的两种方式
- Collections.sort( )方法对list排序的两种方式
- Collections.sort方法对list的两种排序方式
- Collections.sort对List排序的两种方法
- Collections.sort对List排序的两种方法
- Collections.sort方法对list排序的两种方式
- C#抽象类
- 榜单
- boost::asio 的同步方式
- UVALive 3890 Most Distant Point from the Sea(凸包最大内接圆)
- 在python中引入rabbitmq消息中间件
- python学习九:SocektServer学习、bisect、list的sort详解、清空字典的两种方法、zip初探
- boost::asio的异步方式
- NOSQL启蒙: What? Why? When?
- 你必须非常努力,才能看起来毫不费力
- Codeforces Round #147 (Div. 2) / 237A Free Cash (模拟)
- 解结构体、类等内存字节对齐
- 我神圣的2,3,5
- CC2530 外部中断 提醒
- Ubuntu10.04.4嵌入式开发环境搭建