Python中知识点笔记

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添加到它的后面。

原文地址:https://www.cnblogs.com/SunWentao/p/1333651.html