Python3 script to query free TOEFL seats

应同学要求,写了一个爬托福考试空闲考位的脚本。这是第一次真正用Python3写。

通过调用get_seat_status,传入省名(如’Jiangsu’),时间(如’201104′),您可以得到这样的数据结构:

[ [datetime.datetime(2011, 4, 23, 10, 0),  'STN80085B',  '南通市教育装备与勤工俭学管理中心',  '1415',  True],
 [datetime.datetime(2011, 4, 23, 10, 0), 'STN80086A', '扬州大学', '1415', True]]

下面是一个多线程调用的例子:

#! /usr/bin/python3

import threading
import pprint
from toeflgraber import get_seat_status

# 查询江浙沪 2011年3月和4月的考位
locations = ['Jiangsu', 'Shanghai', 'Zhejiang']
months = ['201103', '201104']

def descartes(x, y):
    for i in x:
        for j in y:
            yield (i, j)

lock = threading.RLock()
tasks = len(locations) * len(months)
e = threading.Event()
results = []

def task(location, month):
    global results, tasks
    all_seats = get_seat_status(location, month)
    available_seats = filter(lambda x: x[4], all_seats)
    with lock:
        results.extend(available_seats)
        tasks -= 1
        if tasks == 0:
            e.set()

for t in descartes(locations, months):
    t = threading.Thread(target=task, args=t)
    t.daemon = True
    t.start()

e.wait()
pprint.pprint(results)

Enjoy it.

宁夏川两头头尖

前天在豆瓣电台上听到赵牧阳的《黄河谣》,循声找过去发现这两天在上海有小演出,一时兴起叫上纯西北人亚洲就决定去听听。

地点在上海电视台后面的Wooden Box酒吧。鉴于是第一次在上海听这样的演出,所以也没有什么可比较的。酒馆不大,为了能占个好位置,9点开始的演出我们一行人7点半就坐在了整个酒馆最好的座位上,开始等着。事后证明这是正确的选择,节目开始整个酒馆连个落脚的地方都没有。

大约9点半赵牧阳出现了,先挂了一副艺术品,抱歉我实在没看懂画上画什么。头戴雷锋帽,身穿,身穿还是土豆上大家能看到那个视频的衣服,还专门做出了一道汗渍。当然啦,沧桑是装不出来的,吼两声就知道有没有。

第一环节是赵牧阳弹三弦唱秦腔,大约唱了三首。具体唱什么现在都忘掉了,以前觉得三弦一响都是何玉笙,何勇,现在老观念改改了。亚洲基本上都能听懂,而且能听出出处,虽然是新唱,但都是老曲老调,所以正宗西北人听得透。我之前补习过张佺和苏阳,宁夏川两头头尖,黄河水流过了家流过了兰州我还是知道一点的。

第二个环节是赵牧阳和酒吧老板出门抽烟,场子里周朝的吉他solo。周朝这个人我之前还不知道,不过去的好多人是周朝的朋友,以至于很多人不知道赵牧阳是谁,还得跟我这样的打听。周朝的吉他谈得没得说,在豆瓣上听几首就知道了。

第三阶段,三人合作,牧阳打鼓,周朝吉他,酒吧老板弹贝斯。到了兴头上赵牧阳会吼两嗓子,周朝顺势来了一首Another brick in the wall。后来又请上来一个女的,据说也是德国回来的,乍看还有点龚琳娜的意思,一开口整个一个有过之无不及,让人怀疑是不是德国走一趟都这股味了。四个人各显神通,即兴演出,场面越来越尽兴,那种感觉,等过几天土豆优酷上看视频吧!

为了赶末班地铁我不得不提前一个小时走了,最后肯定错过不少好场面。西北民谣确实挺适合我们干旱地区生长的人,听得带劲听得舒心,今后有机会还是想看看苏阳张佺什么的。

Using dojo on Chrome/Chromium from local file system

dojo.require uses XMLHttpRequest to load additional components. On default Chrome settings, XMLHttpRequest on local file system is not allowed. This will break whole dojo system.

To change the default policy, you can start Chrome with a command line option –allow-file-access-from-files.

This issue is a well known one for Chromium project and still under active discussion:
http://code.google.com/p/chromium/issues/detail?id=40787

Fix ogg/oga not play in Firefox 3.6

Native audio support was introduced in since Firefox 3.5 . Ogg is one of the media format supported by Firefox. However, sometimes you may find it doesn’t work even if you set the right source path. And you just check the network status of audio element:

document.getElementsByTagName(“audio”)[0].networkState

Then you get the constant of a 4, which is HTMLMediaElement.NETWORK_NO_SOURCE.

This is because firefox checks the Content-Type header to make sure it’s a media file. (Webkit based browsers don’t have this restriction.) However, ogg format is not configured on most http servers. You can check the content type by:
curl -I <url-to-media-file>

Take Apache as example, you can add following content to your configuration file:
AddType audio/ogg .oga
AddType video/ogg .ogv .ogg

For more, check this article:
https://developer.mozilla.org/en/Configuring_servers_for_Ogg_media

pgpoolII的管理功能

pgpool是postgresql的中间件,他的主要功能包括:

  • Connection Pooling 连接池
  • Replication 双写
  • Load Balance 负载均衡
  • Limiting Exceeding Connections 连接限制
  • Parallel Query 并行查询

除此以外,pgpool还提供了对数据库节点的健康检查功能,自动fail over。

对pgpool的管理,pgpool本身除了工作端口(默认5433)以外,还开放一个pcp端口9898,类似FTP的控制端口。通过pcp端口可以获取pgpool的运行时信息,也可以进行运行时的操作。这些功能罗列在:

http://pgpool.projects.postgresql.org/pgpool-II/doc/pgpool-en.html#reference

封装这些命令的二进制文件也随着pgpool发行,由于是通过网络通信,所以并不要求这些工具与pgpool安装在同一台机器上。

除此以外,为了用户友好,pgpool还有一个具有UI的用户管理工具。这个工具的功能包括:

  • 系统信息,主要是pcp的信息
  • 配置文件编辑,这个功能似乎要求pgpooladmin和pgpool安装在一台机器上以便直接都写配置文件
  • 切分规则编辑

另外,pgpool还通过一些自定义的SQL语句允许用户从工作端口提取一些配置信息和运行时信息:

http://pgpool.projects.postgresql.org/pgpool-II/doc/pgpool-en.html#show-commands