「学习笔记——Python」Google's Python Class 学习笔记

Google's Python Class


        学习完了《The Python Tutorial》后,又学习了一下Google's python class,感觉比前者要好。关键是提供了一些习题,让你通过这些习题了解Python的特性,让你尽快用Python去解决一些问题,做完这些习题,感觉比看完整本书还有感觉。这给我提了个醒,学完一个东西要主动找一些练习去做,不然看多少书都没有感觉。尤其是一些实践性很强的东西。下面是读的过程中记的一些简要的笔记。


1 Python Introduction

  • 函数需要在使用前定义,所以main函数一般放在最后
  • 使用空格,而不是TAB来缩进,可以设置编辑器使用空格代替TAB.空格和TAB混用会带来语法错误, 尽管看起来缩进的距离是一样的。
  • 代码只有在运行时才会检查,如果你调用函数时写错了函数名,且从来没有执行到调用这个函数的地方, 那么python会运行良好,它不会像其它语言一样在编译时对代码进行检查。
  • 最好使用 import sys,然后调用sys模块时使用sys.argv,而不要使用 from sys import argv,然后调用 时使用 argv.前者会让我们一眼就看出argv来自哪里。
  • 可以使用help函数得到函数和模块的文档,例如help(len)

2 Python String

  • String 方法:方法和函数类似,只不过方法是“运行在”对象上.
    • s.lower(),s.upper() – 大小写转换
    • s.trip() – 去掉字符串前后的空白
    • s.isalpha()/s.isdigit()/s.isspace() – 测试字符串是不是全部都是指定类型
    • s.startwith('other'), s.endwith('other') – 测试是否以指定字符串开始/结尾
    • s.find('other') – 查找子串,如果有,返回第一个下标,如果没有,返回-1
    • s.replace('old','new') – 将字符串中所有 'old' 替换为 'new'
    • s.split('delim') – 返回以'delim'分割的子串list
    • s.join(list) – 与split相反,将list中的元素以字符串s连接起来

3 Python List

  • List赋值操作并不会创建一份新数据,例如 c是个List,b = c执行后,b 和 c 指向同一内存区域。
  • in: 可以用于判断一个元素是否在一个集合中。例如:
    • for num in squares:
    • if 'curly' in mlist:
    • if character in mstring:
  • List常用函数:
    • list.append(elem) – 在list的最后添加一个元素,注意,这会改变list,而不是返回新的list
    • list.insert(index,elem) – 在list的index处插入一个元素
    • list.extend(list2) – 将list2添加到list的最后,也可以使用+ 或+=得到同样结果
    • list.index(elem) – 搜索元素,返回下标
    • list.remove(elem) – 搜索元素,然后删除(如果不存在会返回ValueError)
    • list.sort() – 对list进行排序
    • list.pop(index) – 删除并返回指定位置的元素

4 Python Sorting

  • sorted(list):list.sort() 只会对list排序,不会返回排序后的list. sorted(list)会返回排序后的list,而不改变list.
  • sorted(list, option):option可以定制排序:
    • option为'reverse=True'会给出逆序
    • option为'key=len'会根据元素长度排序,len会作用到list的每个元素上
    • 也可以定制自己的函数,再用key指定根据这个函数排序

5 Dic and files

  • 读文件时可以一行一行读,如果一次全读过来,内存可能放不下
  • 写大程序时,要分一个个阶段写,把每个阶段完成的目标定下,一个阶段完成后,测试,再开始下一阶段, 不要一次全写了

6 Python Regular Expression

  • match = re.search (pat, str)
  • 注意,pat写成r'words:\w\w\w'表示words后有三个字母,r表示raw.
  • 重复
    • + :1个或多个左边的模式: r'pi+' 可以匹配'piiig'中的'piii'
    • * :0个或多个
    • ? :0个或1个
  • 选择
    • [abc]:a或b或c
    • [\w.-]:一个字母或者一个点或者一个连字符
  • 分组
    • 模式:'([\w.-]+)@([\w.-]+)',得到一个match
      • match.group() :匹配整个模式
      • match.group(1):匹配第一个括号中的 ([\w.-]+),即@之前的部分
      • match.group(2):匹配第二个括号中的 ([\w.-]+),即@之后的部分
    • >>> str = 'purple alice-b@google.com monkey dishwasher'
      >>> import re
      >>> match = re.search('([\w.-]+)@([\w.-]+)', str)
      >>> print match.group()
      alice-b@google.com
      >>> print match.group(2)
      google.com
      >>> print match.group(1)
      alice-b
      >>> matchx = re.search('([\w.-]+)(@)([\w.-]+)', str)
      >>> print matchx.group()
      alice-b@google.com
      >>> print matchx.group(1)
      alice-b
      >>> print matchx.group(2)
      @
      >>> print matchx.group(3)
      google.com
      
  • findall
    • re.search只找第一个匹配的,findall找所有匹配的
      >>> str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher'
      >>> emails = re.findall(r'[\w\.-]+@[\w\.-]+', str)
      >>> emails
      ['alice@google.com', 'bob@abc.com']
      
    • findall还可以用于在文件中查找
      • strings = re.findall(r'some pattern', f.read())
    • findall与group
      >>> str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher'
      >>> tuples = re.findall(r'([\w\.-]+)@([\w\.-]+)', str)
      >>> tuples
      [('alice', 'google.com'), ('bob', 'abc.com')]
      
  • 选项:search()和findall()都有相应参数作为选项
    • IGNORECASE :匹配时忽略大小写
    • DOTALL :允许点号(.)匹配新行,默认情况.是不匹配新行的
    • MULTILINE: 如果一个字符串由多行组成,使^ $匹配每一行,而非整个字符串
  • 贪心匹配
    • '<.*>' 会匹配到最后一个,而非第一个'>',这叫做贪心匹配,如果想使用非贪心匹配,可以用?
      >>> str = '<b>foo</b> and <i>so on</i>'
      >>> match1 = re.search('(<.*>)', str)
      >>> match1.group()
      '<b>foo</b> and <i>so on</i>'
      >>> match2 = re.search('(<.*?>)', str)
      >>> match2.group()
      '<b>'
      
  • 替换:re.sub(pat, replacement, str)

7 Python Utilities

  • File System–os,os.path,shutil
    • os.listdir(dir) :列出目录下的所有文件名,返回一个list
    • os.path.join(dir, filename) :把目录和文件名组合在一起,返回整个路径
    • os.path.abspath(path):返回全路径
    • os.path.dirname(path),os.path.basename(path):得到path中的目录,文件名
    • os.path.exists(path):指定路径是否存在
    • os.mkdir(dir_ path):建立一个目录
    • os.makedirs(dir_ path):建立整个路径上所有需要的目录
    • shutil.cpy(source_ path, dest_ path):复制文件
  • Running External Processes – command
    • (status, output) = commands.getstatusoutput(cmd) :运行命令,返回执行结果和输出结果
    • output = commands.getoutput(cmd) :同上,只是不返回执行结果
    • os.system(cmd) :同上,不返回执行结果和输出结果,但将结果输出到标准输出
  • HTTP – urllib 和 urlparse
    • ufile = urllib.urlopen(url) :返回与url相关的类文件对象 
    • text = ufile.read() :读取内容
    • info = ufile.info() :请求的 meta info, 例如 Content-Language,Data, Content-Type等信息
    • baseurl = ufile.geturl –得到请求的base url,由于有转向,可能和原来url不同
    • urllib.urlretrieve(url, filename) –从指定的url下载文件,存储在本地的filename路径中(包含新文件名)
    • urlparse.urljoin(baseurl,url) –得到全路径

另外,这里还有一份Google Python Style Guide,描述了Google建议的Python编程风格




posted @ 2013-03-10 20:26  Iambda  阅读(206)  评论(0编辑  收藏  举报