穿越Python Challenge(0
一个有趣的网站:
http://www.pythonchallenge.com/
集娱乐与学习于一体,在开动脑筋闯关的过程中,不但扩展了思维,还对Python加深了理解。
一共33关,每闯过一关都可以在提示下查看作者给出的Solution。
第0关(指导关):
出现一幅画面,上面写着2**38,教你如何进入下一关。
通过Python客户端,可以简单计算出2**38 = 274877906944
那么按照提示只要将该关网址中0.htm改为274877906944.html 即可
第1关:字符转换
图片上给出了三对字母“K->M, O->Q, E->G",通过分析知道这是一个简单的字符转换,转换规则是new_c=chr(ord(old_c)+2) 超过自动回转。
我们可以看到,该关的网址为http://www.pythonchallenge.com/pc/def/map.html。那么按照指导关给出的策略,只要将map替换为ocr即可通关。但如果仅是如此,那么就完全撇开了Python,该关将彻彻底底地退化为一般的脑筋急转弯。
实际上在图片下有一串看不懂的字符串,按照上述字符转换规则,这应该可以通过python做简单处理,从而得到可识别的字串,代码如下:
import string
frm = "abcdefghijklmnopqrstuvwxyz"
to = "cdefghijklmnopqrstuvwxyzab"
data = """g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp.
bmgle gr gl zw fylbgq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle.
sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."""
print string.translate(data, string.maketrans(frm,to))
运行脚本,我们将得到下面一段描述:
”i hope you didnt translate it by hand. thats what computers are for.
doing it in by hand is inefficient and that's why this text is so long.
using string.maketrans() is recommended. now apply on the url. “
当我们通关之后,通过http://www.pythonchallenge.com/pcc/def/ocr.html
将得到更多关于本关的信息。
第2关:提取有效信息
该关图片上展示了一本书,而图下有一段话“
recognize the characters. maybe they are in the book, but MAYBE
相关文档:
Python中文件操作可以通过open函数,这的确很像C语言中的fopen。通过open函数获取一个file object,然后调用read(),write()等方法对文件进行读写操作。
1.open
使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。
file_object = open('thefile.txt' ......
计划这个星期学习用python实现决策树算法。今晚就碰到了好多问题,好久没有用python了,并且3.0和书本上的有些东西不太一致,这里记录下几个地方。
1) import module 如果不是在标准目录下(系统的path,python的目录,当前目录),那么需要先import sys,然后sys.path.append('');
2) 改变源文件后,再次import ......
Python强的功能就在于它无所不能。
使用win32com模块开发window ActiveX的示例:(如果你还没有装win32com模块的话,请到http://python.net/crew/skippy/win32/Downloads.html下载)。
# SimpleCOMServer.py
class PythonUtilities:
_public_methods_ = ['SplitString']
_reg_progid_ = "Python.Utilities"
......
原文:http://www.klipdas.com/blog/?p=python-decorator
python装饰器介绍
Python 2.2中引入的 classmethod() 和 staticmethod() 内置函数,你可以这样调用classmethod():
class A:
def foo(self, y):
print y
foo = classmethod(foo)
也可以这样:
class A:
@classmethod
def foo(sel ......