Category Archives: 编程相关
IN:Python, 编程相关 Tags: gae, PyFetion, Python Comments:44最近有同学反映PyFetion不能在GAE上使用,我看了下别人给出的错误,问题不出在PyFetion上,无论是TCP方式还是HTTP方式,使用本机测试都可以正常工作。
应该是移动的飞信服务器屏蔽了GAE的出口IP地址(某同学反应GAE的urlopen使用的是同一IP)。
原因大概是广大GAE的爱好者众多,玩PyFetion的又比较多,所以移动不爽了,就封掉你。
问题出来了,原因也大概知道了,那怎么解决方案呢,下面就给出几个吧:
- 强烈要求GAE增加代理支持。
- 打电话给10086,然后说你的飞信上不出了,然后给客服MM说你的IP是:[GAE_IP(哪位知道这个IP来补充一下)]。
- 脱离GAE,使用自己的服务器。
- 继续使用GAE,使用另一服务器做为跳板。
解释一下,GAE不能直接访问飞信服务器了,可以在另一个地方,比如cocobear.info/PyFetion.py?phone=13838381438&pwd=383838&msg=^_^
做这样的一个的接口,然后使用GAE去访问这个服务器。
Update(09.4.27):
据gohsy同学反映,GAE上不能使用是因为服务器返回了405错误,而别的fetion实现会忽略405错误,继续进行,而PyFetion遇到这个错误会产生异常。记得以前调PyFetion的时候也遇到过405错误,应该是发送的数据格式不对而造成的,所以我猜想这里是因为网络环境影响造成的数据不完整,从而使得飞信服务器返回405错误。因为在GAE上PyFetion还是可以偶尔发送成功的,所以出错的原因应该不是程序本身。
为了在GAE上能更好的使用,我修改了一下PyFetion,增加了遇到405错误时重试功能,默认重试5次,这样会使得发送的成功率提高很多,但是由于405的出现,重试多次会影响到发送的速度,这一点是无法避免的。更新请到google code查看:http://code.google.com/p/pytool/source/detail?r=30
其实根本的原因我想还是GAE上用PyFetion的人过多造成的:-)
为了在GAE上调PyFetion,在gohsy同学的帮助下,搭建了GAE环境,感觉还不错,做应用挺方便的,不错的东东。
IN:Lua Tags: Lua Comments:3花了差不多两周的时间读完了Lua Programming in Lua (first edition) ,没找到pdf格式的,一直在官网在线看的,不是很舒服。看的似乎有点慢,里面的英文很简单,讲的内容难点也不多,也没有太多的代码去动手实践,似乎是受别人打击了:-)(看到人家博客里写道:“花两天的时间过了一遍PIL”)。
Lua作为一个脚本语言在很多地方与Python有相似之处,不过小巧了很多,数据类型、表达式、语句等都很精简,但是这并不影响Lua成为一个流行的脚本语言,灵活的table成就了Lua的强大,table可以演化出list,dict,更强大的是table可以实现OO,Lua并没有在语言级提供OO的实现,不过Lua的作者在PIL中展示了使用table完全可以实现近乎完美的OO编程,我觉得这是对整天鼓吹OO思想的人一个极大讽刺,OO其实没什么,C语言中使用结构体,函数指针也可以有模有样的进行OO编程。
Lua中也有很多函数式编程语言的影子,所以很多人把Lua和Scheme归为了近亲,不过俺不了解Scheme,对函数式编程也是一知半解,只能凑个热闹。
Lua之所以在游戏中使用比较广泛,是因为它可以很好的和C/C++结合在一起,可以很方便的互操作,通过使用Stack的方式。还有一个很重要的原因,Lua提供的是搭积木的原料,而不是搭好的建筑,所以你可以很灵活的用自己的方式去写程序。比如你可以用table实现一个完整的OO系统,然后在OO上面写程序(这里有一点很重要,基于table实现OO是很轻量的,而不是语言级的OO);你也可以用metatable来实现Python中很强大的filter,map,reduce。
Lua中的table有点类似道家学说里面的”道”,”道生一,一生二,二生三,三生万物”,而table就是Lua中的“道”。
还得提一点Lua是完全用ANSI C编写的。
cocobear@0-0 /home/cocobear/Codes/lua-5.1.4/src $ wc *.c *.h -l | tail -1
17002 total
IN:Python Tags: Lua, Python Comments:3
> function foo (n)
>> if n > 0 then return foo(n - 1) end
>> end
> foo(100)
> foo(1000)
> foo(10000)
> foo(100000)
> foo(1000000)
如果函数最后一句是return g(…)这样的形式,Lua将会把这句解释为goto g(x),因为这里除了对g函数调用,再没有别的事做,也不需要保存堆栈里调用函数的信息。因此上面即使调用很多次也没有出现堆栈溢出的问题,把上面的代码转换为Python:
>>> def foo(n):
... if n>0: return foo(n-1)
...
>>> foo(100)
>>> foo(1000)
...
File "<stdin>", line 2, in foo
File "</stdin><stdin>", line 2, in foo
File "</stdin><stdin>", line 2, in foo
RuntimeError: maximum recursion depth exceeded
很快出就现了堆栈溢出的问题,在这里表现为达到了递归调用的最大限制。Python自身没有实现尾部调用优化,不过也可以通过实现一个decorator办法来实现:
#!/usr/bin/env python2.4
# This program shows off a python decorator(
# which implements tail call optimization. It
# does this by throwing an exception if it is
# it's own grandparent, and catching such
# exceptions to recall the stack.
import sys
class TailRecurseException:
def __init__(self, args, kwargs):
self.args = args
self.kwargs = kwargs
def tail_call_optimized(g):
"""
This function decorates a function with tail call
optimization. It does this by throwing an exception
if it is it's own grandparent, and catching such
exceptions to fake the tail call optimization.
This function fails if the decorated
function recurses in a non-tail context.
"""
def func(*args, **kwargs):
f = sys._getframe()
if f.f_back and f.f_back.f_back
and f.f_back.f_back.f_code == f.f_code:
raise TailRecurseException(args, kwargs)
else:
while 1:
try:
return g(*args, **kwargs)
except TailRecurseException, e:
args = e.args
kwargs = e.kwargs
func.__doc__ = g.__doc__
return func
@tail_call_optimized
def factorial(n, acc=1):
"calculate a factorial"
if n == 0:
return acc
return factorial(n-1, n*acc)
print factorial(10000)
# prints a big, big number,
# but doesn't hit the recursion limit.
@tail_call_optimized
def fib(i, current = 0, next = 1):
if i == 0:
return current
else:
return fib(i - 1, next, current + next)
print fib(10000)
# also prints a big number,
# but doesn't hit the recursion limit.
代码来自http://code.activestate.com/recipes/474088/
在这段代码里tail_call_optimized是一个decorator,在执行factorial函数前,这个decorator先执行,tail_call_optimized中通过sys._getframe()这个方法会返回一个frame对象,包含了堆栈顶部的信息,当发现当前调用是一个递归调用:
if f.f_back and f.f_back.f_back
and f.f_back.f_back.f_code == f.f_code:
则抛出一个异常,下面的代码则截获异常,继续执行,这样就避免了堆栈的使用,很巧妙的一种方式。
在python-dev的邮件列表里,有人曾经做过一个Python的尾调用优化的补丁,不过Guido拒绝了这个补丁:
I’m not interested in adding this to the official Python release.
One reason is that if an exception happens in such a tail-recursive
call, the stack trace will be confusing.
Another reason is that I don’t think it’s a good idea to try to
encourage a Scheme-ish “solve everything with recursion” programming
style in Python.
But feel free to maintain this as an independent modification, a la
Stackless — I’m sure there are people who would like to try this
out.
–Guido van Rossum (home page: http://www.python.org/~guido/)
IN:Lua, Python Tags: Lua, Python Comments:3Lua:
> function newCounter ()
>> local i = 0
>> return function () -- anonymous function
>> i = i + 1
>> return i
>> end
>> end
>
> c1 = newCounter()
> print(c1())
1
> print(c1())
2
Lua同Python类似,也可以嵌套定义函数,不过Lua嵌套函数可以访问上层闭包函数的局部变量,而在这个内嵌函数中这些变量不是全局变量,也不是局部变量,而是一种upvalue,与C语言中的static修饰的变量类似,所以在这里可以利用这个特性完成这个计数器。
Python:
>>> def counter(last=[1]):
... next = last[0] + 1
... last[0] = next
... return next
...
>>> counter()
2
>>> counter()
3
今天看Lua的时候觉得Python也应该有比较简单的办法写个计数器,找到一篇文章介绍这个小技巧,其中有一句话:
However, if named parameters are given mutable default values, the parameters can act as persistent memories of previous invocations. Lists, specifically, are handy mutable objects that can conveniently even hold multiple values.
Lua作为一个嵌入式(嵌入到其它语言中)的脚本语言,在很多地方与Python类似,这几天在看Programming in Lua 。
IN:Python Tags: getsong, PyFetion, Python Comments:8给PyFetion加入了HTTP代理的支持,HTTP发送方式中。TCP发送方式也可能会加上吧,使用SocksiPy这个库吧,有时间再说。
改PyFetion的时候发现urllib2的一个问题:
先看下面这段代码:
import urllib2
headers = [("Content-Type","application/oct-stream"),]
opener = urllib2.build_opener()
opener.addheaders = headers
urllib2.install_opener(opener)
print "after install_opener"
ret = opener.open('http://www.dict.cn',data="word=ss")
print ret.read()
抓包发现发送的内容为:
POST / HTTP/1.1
Accept-Encoding: identity
Content-Length: 7
Host: www.dict.cn
Content-Type: application/x-www-form-urlencoded
Connection: close
word=ss
这里我在代码里已经指定了HTTP的header: Content-Type,但是发出去的时候却被改变了。
通过分析urllib2的代码,找到:
if request.has_data(): # POST
data = request.get_data()
if not request.has_header('Content-type'):
request.add_unredirected_header(
'Content-type',
'application/x-www-form-urlencoded')
if not request.has_header('Content-length'):
request.add_unredirected_header(
'Content-length', '%d' % len(data))
scheme, sel = splittype(request.get_selector())
sel_host, sel_path = splithost(sel)
if not request.has_header('Host'):
request.add_unredirected_header('Host', sel_host
or host)
for name, value in self.parent.addheaders:
#这里的parent是opener对象
name = name.capitalize()
if not request.has_header(name):
request.add_unredirected_header(name, value)
urllib2发现如果是POST数据的话自己添加了Content-Type,接着才去追加opener对象中的headers,这时已经有Content-Type了,所以opener对象增加的Content-Type就无效了。
解决办法是创建request对象,在request对象中设置Content-Type:
request = urllib2.Request(url,headers=headers,data=body)
不知道是上面的示例代码写法不规范呢?还是算Python的一个小问题呢?
最近发现周董的不少歌挺好听,于是找个脚本来下载周董的歌(从百度mp3中), getsong就进入了眼的视野,不过不支持下载某个歌手的全部歌曲,俺自己加上去,正在下载周董的歌:
[cocobear@cocobear getsong]$ ./getsong.py -a 周杰伦
正在下载第1首(共104首) 歌手:周杰伦 曲名:她的睫毛
已经成功下载《周杰伦 – 她的睫毛》
…….
不过这会儿断了,getsong好像又有bug了,今天就不折腾了,明天再整整吧,看书,睡觉了。
IN:Python Tags: pysdcv, Python, sdcv Comments:4一个是以前写的导出抓虾收藏的工具,增加了保存抓虾上文章的功能,因为只导出链接有些文章地址可能已经失效。
另一个就是pysdcv,前面文章介绍过了,整理了一下,测试了一下,然后放在了google code上面,直接使用星际译王的词典,查一个单词大概0.15秒的时间:
cocobear@0-0 /home/cocobear/Work/pytool/pysdcv $ make
gcc lookup.c -g -I/usr/include/python2.5 -lz -shared -fPIC -o lookup.so
cocobear@0-0 /home/cocobear/Work/pytool/pysdcv $ time ./pysdcv.py test
*[test]
n. 测试, 试验, 化验, 检验, 考验, 甲壳
vt. 测试, 试验, 化验
vi. 接受测验, 进行测试
【医】 试验, 测验
【经】 检验, 试验, 测试
相关词组:
put sth to the test
stand the test
give a test
take a test
test sb’s ability
real 0m0.164s
user 0m0.143s
sys 0m0.010s
如果是未使用gzip压缩过的dict文件,则0.05秒左右。
Page 3 of 22«12345»1020...Last »