中文测试标题
该帖子纯粹为了测试中文,如果成功,将在博客上显示正确地中文~~
我的纯技术博客
If this appears on my Blogspot, then my python script for posting blogs is successful. The following is a test for Pygments code highlighting.
for a in [5, 4, 3, 2, 1]:
print a
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# flow_parser.py
# author: Rongzhou Shen
# date: 2009-02-11
from pyparsing import *
LSQUARE, RSQUARE, SEMI, ASSIGN, QUOTE = map(Suppress, '[];="')
LBRACE, RBRACE, AT, LBRACK, RBRACK = map(Suppress, '{}@()')
# Definition part of the flow_def file
node_id = Word(alphanums + "_")("id")
node_type = Word(alphas)("type")
declaration = node_type + LSQUARE + node_id + RSQUARE
value = QuotedString('"')
definition = Group(declaration("declaration") + ASSIGN + value("value") + SEMI)
# Flow part of the flow_def file
flow = Group(node_id + OneOrMore(Group('=>' + node_id)) + SEMI)
# Grammar definition of the whole file
flow_impl = OneOrMore(definition)("definitions") + OneOrMore(flow)("flows")
flow_def = AT + LBRACK + QuotedString('"')("flow_name") + RBRACK + LBRACE +\
flow_impl("flow_impl") + RBRACE
#sample = """
#@("A test flow_chart") {
# process[node1] = "This is a test process";
# process[node2] = "This is another test process";
# a => b => c;
# ffew => fweji;
#}"""
def test_parse():
expected = ['process', '[', 'node1', ']', '=', '"',
'This is a test process', '"', ';']
test_def = 'process[node1] = "This is a test process";'
data = definition.parseString(test_def)
assert len(data) == len(expected)
assert all([x == y for x, y in zip(expected, data)])
expected = ['a', '=>', 'b', '=>', 'c', ';']
test_flow = 'a => b => c;'
data = flow.parseString(test_flow)
assert len(data) == len(expected)
assert all([x == y for x, y in zip(expected, data)])
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
import yapgvb
import optparse
FORMATS = {"png" : yapgvb.formats.png,
"jpg" : yapgvb.formats.jpg,
"gif" : yapgvb.formats.gif}
ENGINES = {"dot" : yapgvb.engines.dot,
"neato" : yapgvb.engines.neato,
"circo" : yapgvb.engines.circo,
"twopi" : yapgvb.engines.twopi}
if __name__ == '__main__':
# args = sys.argv
# if len(args) < 2:
# print "Usage: python state_machine.py <def file>"
# sys.exit(0)
parser = optparse.OptionParser()
parser.add_option("-f", "--format", dest="format",
help="store the flow chart in FORMAT",
metavar="FORMAT", default="png")
parser.add_option("-o", "--output", dest="output",
help="save the flow chart to FILE",
metavar="FILE")
parser.add_option("-e", "--engine", dest="engine",
help="the layout ENGINE to use for the flow chart",
metavar="ENGINE", default="dot")
options, args = parser.parse_args()
if len(args) < 1:
parser.print_usage()
sys.exit(0)
graph = yapgvb.Graph("States")
node_dict = {}
with open(args[0]) as def_file:
lines = [l.strip() for l in def_file.readlines()]
for line in lines:
nodes = line.split("=>")
prev_node = None
for node in nodes[::-1]:
label = node.strip().split("#")
if not node_dict.has_key(label[0]):
node_in_graph = graph.add_node(label=label[1],
shape='ellipse', fillcolor='yellow',
style='filled', width=0.5)
node_dict[label[0]] = node_in_graph
else:
node_in_graph = node_dict[label[0]]
if prev_node:
edge = node_in_graph - prev_node
edge.arrowhead = 'normal'
prev_node = node_in_graph
graph.layout(ENGINES[options.engine])
format = FORMATS[options.format]
if options.output:
out_file = options.output
else:
out_file = args[0] + "." + format
graph.render(out_file, format)
## ## Put me in ~/.irssi/scripts, and then execute the following in irssi: ## ## /load perl ## /script load notify ## use strict; use Irssi; use vars qw($VERSION %IRSSI); $VERSION = "0.01"; %IRSSI = ( authors => 'Luke Macken', contact => 'lewk@csh.rit.edu', name => 'notify.pl' ); sub notify { my ($server, $msg, $nick, $address, $target) = @_; return if (!($msg =~ /anticlockwise/)); system("notify-send -i gtk-dialog-info -t 5000 '$nick' '$msg'"); } Irssi::signal_add('message public', 'notify');原始脚本采用的irssi信号是print text,虽然这个信号能够检测个人信息,却无法得到消息的内容(所谓的text是指的系统消息,却不是聊天消息)。所以就改成了message public,然后message public又没有一个通用的检测是否个人消息的方法,所以就只能用正则表达式来判断了,有些傻,不过管用。
#!/usr/bin/env python # -*- coding: utf-8 -*- import urllib import urllib2 import sys import jsonlib from optparse import OptionParser URL = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s%%7C%s" parser = OptionParser() parser.add_option("-f", "--from", dest="lang1", help="The language code to translate from", default="en") parser.add_option("-t", "--to", dest="lang2", help="The language code to translate to", default="zh") (options, args) = parser.parse_args() if len(args) < 1: print "Usage: python translate.py <translate text>" sys.exit(0) text = ' '.join(args) print "Translating %s from %s to %s" % (text, options.lang1, options.lang2) query = (URL % (urllib.quote(text), options.lang1, options.lang2)) req = urllib2.Request(query) req.add_header("Referer", "http://www.my-ajax-site.com") r = urllib2.urlopen(req) data = r.read() obj = jsonlib.read(data) if obj['responseStatus'] != 200L: print "Error: %s" % obj['responseDetails'] else: print "Translated text: %s" % obj['responseData']['translatedText']