Notes on knowledge points in Python

Source: Internet
Author: User

Notes on knowledge points in Python

Wentao sun. nov.14, 2008


I have been in this company for 11 months. At first, I used up a notebook, which contains some pieces of work records. I saw a piece of notes I recorded when I learned/wrote the python program, decided to put

For your reference.


1. The Perfix attribute in SYS. prefix sys indicates c: \ Program Files \ python25, that is, the python installation path;

2. python can install a module or software package in the following way: Python setup. py install, or you can add -- prefix =/xxx to indicate the installation path.

On Some Linux platforms or Mac OS X, when the root account cannot be enabled, you can install Python software elsewhere (using t for/usr/lib or/usr/include ).

3. python is case sensitive, case sensitive;

4. Using Environment in scons, I have been exploring this for a long time:

ENV = scons. Script. Environment ()

5. frameworkversion =... framework is a concept in OS x. Many software modules in Mac OS X are in the framework package;

6. Pay attention to the platform module;

7. Use Unicode programming in Python. Just add a 'u' to the front;

8... indicates that two layers are jumped up,.../indicates the current directory,.../indicates the previous layer;

9. Use cppdefines to represent the Preprocessor definitions part;

10. Some useful code snippets (which have been used and tested for a long time)

(1) cyclically search for directories, including sub-paths:

# 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:
File = self. Files [self. Index]
Self. Index = self. index + 1
Failed t indexerror:
# Pop next directory from Stack
Self. Directory = self. Stack. Pop ()
Self. Files = OS. listdir (self. Directory)
Self. Index = 0
# 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) Add what you want to add to a vs project file

# Add Preprocessor definition to project deployments 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 ')
Projecthandle. Write (line)
Projecthandle. Close ()
Bakhandle. Close ()
OS. Remove (bakfile)


(3) return a string of vcproj files from a VS solution file, or similar situations

# 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:
Icprojectfile = OS. Path. dirname (solutionfile) + \
"\" + OS. Path. splitext (origprojectfile) [0] + \
Print icprojectfile
Projfilelist. append (icprojectfile)
Slnfilehandler. Close ()
Return projfilelist


(4) Delete an object

# Print iccprojects
For proj in iccprojects:
Print proj
OS. Remove (proj. rstrip ())
If OS. Access (proj, OS. f_ OK ):
OS. Remove (proj. rstrip ())


11. Note the different meanings of the List append and extend methods in Python.

List = ['1', '2', '3']

List. append (['4', '5']) => List = ['1', '2', '3', ['4', '5']

List. Extend (['4', '5']) => List = ['1', '2', '3', '4', '5']

That is to say, when linking two linked lists, extend is the link element elements, while append adds an entire list append to it.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.