使用 Python 内置的 defaultdict,我们可以很容易的定义一个树形数据结构:
def tree(): return defaultdict(tree)就是这样!
| #coding=utf8 | |
| from collections import OrderedDict, defaultdict | |
| import json | |
| import codecs | |
| class OrderedDefaultDict(OrderedDict, defaultdict): | |
| def __init__(self, default_factory=None, *args, **kwargs): | |
| #in python3 you can omit the args to super | |
| super(OrderedDefaultDict, self).__init__(*args, **kwargs) |
使用 Python 内置的 defaultdict,我们可以很容易的定义一个树形数据结构:
def tree(): return defaultdict(tree)就是这样!
libuv 和 libev ,两个名字相当相近的 I/O Library,最近有幸用两个 Library 都写了一些东西,下面就来说一说我本人对两者共同与不同点的主观表述。
高性能网络编程这个话题已经被讨论烂了。异步,异步,还是异步。不管是 epoll 也好,kqueue 也罢,总是免不了异步这个话题。
libuv是异步的,libev是同步的多路IO复用。
libev 是系统I/O复用的简单封装,基本上来说,它解决了 epoll ,kqueuq 与 select 之间 API 不同的问题。保证使用 livev 的 API 编写出的程序可以在大多数 *nix 平台上运行。但是 libev 的缺点也是显而易见,由于基本只是封装了 Event Library,用起来有诸多不便。比如 accept(3) 连接以后需要手动 setnonblocking。从 socket 读写时需要检测 EAGAIN 、EWOULDBLOCK 和 EINTER 。这也是大多数人认为异步程序难写的根本原因。
libuv 则显得更为高层。libuv 是 joyent 给 Node 做的一套 I/O Library 。而这也导致了 libuv 最大的特点就是处处回调。基本上只要有可能阻塞的地方,libuv 都使用回调处理。这样做实际上大大减轻了程序员的工作量。因为当回调被 call 的时候,libuv 保证你有事可做,这样 EAGAIN 和 EWOULDBLOCK 之类的 handle 就不是程序员的工作了,libuv 会默默的帮你搞定。
| #!/usr/bin/python | |
| #coding=utf8 | |
| import Image as pil | |
| import pytesseract as tss | |
| from os import listdir | |
| from os.path import isfile, join | |
| from os import system | |
| from random import randint |
| from bottle import route, request, run | |
| map_html = ''' | |
| <!doctype html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
| <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"> | |
| <title>点标记</title> |
| #!/usr/bin/env python | |
| #-*- coding:utf-8 -*- | |
| import httplib, urllib | |
| import socket | |
| import time | |
| #这个不用在意 | |
| params = dict( | |
| login_email="[email protected]", # replace with your email |