神秘eval
  eval我理解为一种内嵌的python解释器(这种解释可能会有偏差), 会解释字符串为对应的代码并执行, 并且将执行结果返回。
  看一下下面这个例子:
  #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  def test_first():
  return 3
  def test_second(num):
  return num
  action = {  # 可以看做是一个sandbox
  "para": 5,
  "test_first" : test_first,
  "test_second": test_second
  }
  def test_eavl(): 
  condition = "para == 5 and test_second(test_first) > 5"
  res = eval(condition, action)  # 解释condition并根据action对应的动作执行
  print res
  if __name__ == '_
  exec
  exec在Python中会忽略返回值, 总是返回None, eval会返回执行代码或语句的返回值
  exec和eval在执行代码时, 除了返回值其他行为都相同
  在传入字符串时, 会使用compile(source, ‘<string>’, mode)编译字节码。 mode的取值为exec和eval
  #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  def test_first():
  print "hello"
  def test_second():
  test_first()
  print "second"
  def test_third():
  print "third"
  action = {
  "test_second": test_second,
  "test_third": test_third
  }
  def test_exec():
  exec "test_second" in action
  if __name__ == '__main__':
  test_exec()  # 无法看到执行结果
  getattr
  getattr(object, name[, default])返回对象的命名属性,属性名必须是字符串。如果字符串是对象的属性名之一,结果是该属性的值。例如, getattr(x, ‘foobar’) 等价于 x.foobar。 如果属性名不存在,如果有默认值则返回默认值,否则触发 AttributeError 。
  # 使用范例
  class TestGetAttr(object):
  test = "test attribute"
  def say(self):
  print "test method"
  def test_getattr():
  my_test = TestGetAttr()
  try:
  print getattr(my_test, "test")
  except AttributeError:
  print "Attribute Error!"
  try:
  getattr(my_test, "say")()
  except AttributeError: # 没有该属性, 且没有指定返回值的情况下
  print "Method Error!"
  if __name__ == '__main__':
  test_getattr()
  # 输出结果
  test attribute
  test method
  命令行处理
  def process_command_line(argv):
  """
  Return a 2-tuple: (settings object, args list).
  `argv` is a list of arguments, or `None` for ``sys.argv[1:]``.
  """
  if argv is None:
  argv = sys.argv[1:]
  # initialize the parser object:
  parser = optparse.OptionParser(
  formatter=optparse.TitledHelpFormatter(width=78),
  add_help_option=None)
  # define options here:
  parser.add_option(      # customized description; put --help last
  '-h', '--help', action='help',
  help='Show this help message and exit.')
  settings, args = parser.parse_args(argv)
  # check number of arguments, verify values, etc.:
  if args:
  parser.error('program takes no command-line arguments; '
  '"%s" ignored.' % (args,))
  # further process settings & args if necessary
  return settings, args
  def main(argv=None):
  settings, args = process_command_line(argv)
  # application code here, like:
  # run(settings, args)
  return 0        # success
  if __name__ == '__main__':
  status = main()
  sys.exit(status)
  读写csv文件
  # 从csv中读取文件, 基本和传统文件读取类似
  import csv
  with open('data.csv', 'rb') as f:
  reader = csv.reader(f)
  for row in reader:
  print row
  # 向csv文件写入
  import csv
  with open( 'data.csv', 'wb') as f:
  writer = csv.writer(f)
  writer.writerow(['name', 'address', 'age'])  # 单行写入
  data = [
  ( 'xiaoming ','china','10'),
  ( 'Lily', 'USA', '12')]
  writer.writerows(data)  # 多行写入
  各种时间形式转换
  只发一张网上的图, 然后查文档好了, 这个是记不住的


  字符串格式化
  一个非常好用, 很多人又不知道的功能:
  >>> name = "andrew"
  >>> "my name is {name}".format(name=name)
  'my name is andrew'