随笔分类 -  Python

摘要:Problem StatementYou have a collection of music files with names formatted as “genre-artist-album-song” (quotes for clarity only), where genre, artist, album, and song each consist of only lowercase l... 阅读全文
posted @ 2014-07-26 19:41 Andy Cheung 阅读(257) 评论(0) 推荐(0)
摘要:Python学习笔记1.基础知识· 用于实现整除的操作符://· 幂运算符:**· Python中变量没有类型。类型的强制转换使用函数int(32.9);而C中强制转换使用(int)32.9· round():将浮点数四舍五入;floor():向下取整;ceil():向上取整· 跨多行的字符串使用三... 阅读全文
posted @ 2014-07-21 15:22 Andy Cheung 阅读(405) 评论(0) 推荐(0)
摘要:1.什么是元类元类让你来定义某些类是如何被创建的,从根本上说,赋予你如何创建类的控制权。可以把元类想成是一个类中类,或是一个类,它的实例是其它的类。当某个类调用type()函数时,你就会看到它到底是谁的实例。>>> class C(object):pass>>> class CC:pass>>> type(C)>>> type(CC)>>> import types>>> type(CC) is types.ClassTypeTrue2.什么时候使用元类元类一般用于创建类。在执行类定义时 阅读全文
posted @ 2013-09-21 22:28 Andy Cheung 阅读(498) 评论(0) 推荐(0)
摘要:下面是一些常用的Python字符串常量string.digits:包含0-9的字符串string.letters:包含所有大小写字母的字符串string.lowercase:所有小写字母string.printable:包含所有可以打印字符的字符串string.punctuation:包含所有标点的字符串string.uppercase:包含所有大写字母的字符串字母字符串常量与地区有关,比如string.letters,也就是说,其具体值取决于python所配置的语言,如果确认自己使用的是ascii,那么可以在签名加上ascii_前缀。例如:string.ascii_letters 阅读全文
posted @ 2013-09-21 22:28 Andy Cheung 阅读(2410) 评论(0) 推荐(0)
摘要:1 match = re.search(pat,str)If the search is successful, search() returns a match object or None otherwise.The codematch = re.search(pat, str)stores the search result in a variable named "match". Then the if-statement tests the match -- if true the search succeeded and match.group() is the 阅读全文
posted @ 2013-09-21 22:25 Andy Cheung 阅读(581) 评论(0) 推荐(0)
摘要:1.将代码移植到Python2.6建议任何要将代码移植到Python3的用户首先将代码移植到Python2.6。Python2.6不仅与Python2.5向后兼容,而且支持Python3中的部分新特性。另外,如果使用-3命令行选项运行Python2.6,它会对已废弃的特性发出警告信息。用户参考这些消息,就可以在迁移到Python3之前,尽力保证程序在Python2.6上运行时不会出现任何警告。2.提供测试覆盖Python有一些很有用的测试模块,包括doctext和unitest。确保在尝试移植到Python3之前,对应用程序进行全面测试。要确保测试范围尽可能大,而且程序在Python2.6上运 阅读全文
posted @ 2013-09-21 22:24 Andy Cheung 阅读(580) 评论(0) 推荐(0)
摘要:1.源代码编码和标识符 Python3假定源代码使用UTF-8编码。另外,关于标识符中哪些字符是合法的规则也放宽了。特别是,标识符可以包含代码点为U+0080及以上的任意有效Unicode字符。例如: π = 3.14159262.集合字面量 在{}中放入一组值就可以定义一个集合,例如: days = {'Mon','Tue','Wed','Thu','Fri','Sat','Sun'}同: days = set(['Mon','Tue','W 阅读全文
posted @ 2013-09-21 22:22 Andy Cheung 阅读(1113) 评论(0) 推荐(0)
摘要:1.文本与字节Python3对文本字符串(字符)和二进制数据(字节)进行了严格区分,'hello'表示一个以Unicode编码保存的文本字符串,而b'hello'表示一个字节字符串。在Python3中,无论什么情况都不能混用str和bytes类型,而在Python2中,会根据需要将字节字符串自动转换为Unicode。要将文本字符串转换为字节,必须使用s.encode(encoding)方法。例如,s.encode('uff-8')可将s转换为一个UTF-8编码的字节字符串。要将字节字符串t转换为文本,必须使用t.decode(encoding)方 阅读全文
posted @ 2013-09-21 22:22 Andy Cheung 阅读(1287) 评论(0) 推荐(0)