Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1416101
  • 博文数量: 264
  • 博客积分: 5810
  • 博客等级: 大校
  • 技术积分: 3528
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-13 17:15
文章分类

全部博文(264)

文章存档

2011年(264)

分类: Python/Ruby

2011-05-07 11:41:36

  1. #coding:utf-8
  2. from subprocess import Popen, PIPE
  3. import locale,os
  4. locale_encoding = locale.getpreferredencoding()
  5. def run_svn(args,fail_if_stderr=False,encoding="utf-8"):
  6.     #内部函数以_开头,一般应用于map filter等运算符
  7.     def _transform_data(a):
  8.         if isinstance(a, unicode):
  9.             a = a.encode(encoding or locale_encoding)
  10.         elif not isinstance(a, str):
  11.             a = str(a)
  12.         return a
  13.     def _shell_quote(s):
  14.         if os.name == "nt":
  15.             q = '"'
  16.         else:
  17.             q = "'"
  18.         return q + s.replace('\\', '\\\\').replace("'", "'\"'\"'") + q
  19.     t_args = map(_transform_data, args)
  20.     cmd = "svn"
  21.     cmd_string = str(" ".join(map(_shell_quote, [cmd] + t_args)))
  22.     pipe = Popen([cmd] + t_args,executable = cmd,stdout = PIPE,stderr=PIPE)
  23.     out,err = pipe.communicate()
  24.     return out

  25. """
  26. 1.如何对一个变量进行encode处理即将一个unicode类型的字符转义成utf-8或gbk
  27. if isinstance(a,unicode):
  28.    a = a.encoding("utf-8",locale.getpreferredencoding())
  29. elif not isinstance(a,str):
  30.     a = str(a)
  31. return a
  32. 2.如何能够利用子函数里面应用于map filter等函数操作
  33. def _transform(a)
  34. map(_transform,args)

  35. """
  36. """
  37. 解析SVN 输出的XML字符串信息输出到一个字典数据
  38. """
  39. def parse_svn_info_xml(xml_string):
  40.     try:
  41.         from xml.etree import cElementTree as ET
  42.     except ImportError:
  43.         try:
  44.             from xml.etree import ElementTree as ET
  45.         except ImportError:
  46.             try:
  47.                 import cElementTree as ET
  48.             except ImportError:
  49.                 from elementtree import ElementTree as ET
  50.     d = {}
  51.     tree = ET.fromstring(xml_string)
  52.     entry = tree.find(".//entry")
  53.     if entry:
  54.         d['url'] = entry.find("url").text
  55.         d['revision'] = entry.find("revision").text
  56.         d['repos_url'] = entry.find("repos_url").text
  57.         d['last_changed_rev'] = entry.find("last_changed_rev").text
  58.         d['kind'] = entry.find("kind").text
  59.     return d
1. 函数里面套函数应用
2. map filter等应用
3. 字典的妙用
阅读(827) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

网络安全服务2011-05-13 16:40:24

给力``写代码```