设为首页 收藏本站
查看: 607|回复: 0

[经验分享] Python中知识点笔记

[复制链接]

尚未签到

发表于 2015-4-25 09:05:51 | 显示全部楼层 |阅读模式
Python中知识点笔记

Wentao Sun. Nov.14, 2008

  
  来这个公司11个月了,最开始来的一个笔记本用完了,里面都是工作时记录的一些片段,看到一块自己当时学/写 python程序时记录的笔记,决定放到
  网上,供大家参考。
  
  1. sys.prefix sys模块中的perfix属性表示的是C:\Program files\python25,即python的安装路径;
  2. python对一个module或软件包安装的方法是:python setup.py install,或者可以加入--prefix=/XXX表示安装路径。
  在一些Linux平台或Mac OS X上,当无法开启root账户时则可以将一些python系的软件安装到其他地方(except for /usr/lib or /usr/include).
  3. python是case sensitive的,区分大小写;
  4. SCons中 Environment的使用,我摸索了很长时间才知道这点的:
  env = SCons.Script.Environment()
  5. frameworkversion=... framework是OS X中的一个概念,很多软件模块在Mac OS X上都是以framework包为单位的;
  6. 注意platform这一module;
  7. python中使用unicode编程,在前面加一个'u'即可;
  8. ../..表示向上跳两层, ./表示当前目录, ../表示上一层;
  9. 用CPPDEFINES表示preprocessor definitions部分;
  10. 一些用用的代码片段(经过长期使用和测试的)
  (1) 循环搜索目录,包括子路径:

# Directory Walker  is used to search all files in a sepecfied directory
class DirectoryWalker:
    # a forward iterator that traverses a directory tree

    def __init__(self, directory):
        self.stack = [directory]
        self.files = []
        self.index = 0

    def __getitem__(self, index):
        while 1:
            try:
                file = self.files[self.index]
                self.index = self.index + 1
            except IndexError:
                # pop next directory from stack
                self.directory = self.stack.pop()
                self.files = os.listdir(self.directory)
                self.index = 0
            else:
                # got a filename
                fullname = os.path.join(self.directory, file)
                if os.path.isdir(fullname) and not os.path.islink(fullname):
                    self.stack.append(fullname)
                if os.path.isfile(fullname):
                    return fullname  
  (2) 向一个VS工程文件中添加你想加的东西

# Add preprocessor definition to project configurations in solution file
def AddPreprocessDefinition(projectFile, defList):
    shutil.copyfile(projectFile, projectFile+".bak")
    bakFile = projectFile+".bak"
    bakHandle = open(bakFile,'r')
    os.remove(projectFile)
    projectHandle = open (projectFile, 'w+')
    for line in bakHandle.readlines( ):
        matchObj = re.match(r'(\s+)PreprocessorDefinitions="(.*)".*',line)
        if matchObj:
            prefixPart = matchObj.group(1)
            predeflist = matchObj.group(2)
            for predef in defList:
                predeflist = predeflist + ';' + predef
            projectHandle.write(prefixPart + r'PreprocessorDefinitions="' + \
                              predeflist+'"\n')
        else:
            projectHandle.write(line)
    projectHandle.close()
    bakHandle.close()
    os.remove(bakFile)  
  (3) 从一个VS的solution文件中返回一串vcproj文件,或类似的情形

# return icproject files in solution files
def ProjectsInSolution(solutionFile, projPostFix):
    projFileList = []
    slnFileHandler = open(solutionFile, 'r')
    fileContent = slnFileHandler.readlines()
    for line in fileContent:
        matchObj = re.match(r'^Project\(\"\{.*\}\"\) = \".*\", \"(.*)\", .*',line)
        if matchObj:
            origProjectFile = matchObj.groups(0)[0]
            if os.path.splitext(origProjectFile)[1] != projPostFix:
                continue
            icProjectFile = os.path.dirname(solutionFile) + \
                            "\\" + os.path.splitext(origProjectFile)[0] + \
                            projPostFix
            print icProjectFile
            projFileList.append(icProjectFile)
    slnFileHandler.close()
    return projFileList  
  (4) 删除文件

#print iccprojects
for proj in iccprojects:
    print proj
    os.remove(proj.rstrip())
    if os.access(proj, os.F_OK):
        os.remove(proj.rstrip())  
  11. 注意python中list的append和extend方法的不同意义。
  list =  ['1', '2', '3']
  list.append(['4', '5']) ==> list = ['1', '2', '3', ['4', '5']]
  list.extend(['4', '5']) ==> list = ['1', '2', '3', '4', '5']
  也就是说,在链接两个链表的时候,extend是链接元素elements,而append则将一整个list append添加到它的后面。
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-60429-1-1.html 上篇帖子: Selenium终极自动化测试环境搭建(二):Selenium+Eclipse+Python 下篇帖子: Python Web编程(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表