2009.1.
16
Make&Python
Make&PythonTutorial
AhRamKim ReinitzLab
Make&Python
WhatsMake?
[Link](defaultname=makefile orMakefile),itcreatesasequenceofcommandsforexecutionbythe UNIX/LINUXshell.
WhyMake?
Evenrelativelysmallprojectstypicallyinvolveanumberoffilesthatdependupon [Link]:Ifyoumodifyoneormore sourcefiles,youmustrelinktheprogramafterrecompilingsomebutnot [Link]. Yourecordonceandforalltherelationshipsamongasetoffilesandthenlet makeautomaticallyperformallupdatingtaskveryeasily.
[Link]
Make&Python
Background:Asimplecompilation
Imagefrom[Link]
[Link]:.cfileisconvertedintoalowerlevellanguagecalledAssembly language;[Link]. [Link]:.sfileisthenconvertedintoobjectcode(.o)whichare fragmentsofcodewhichthecomputerunderstandsdirectly. [Link]:[Link]"builtin" functions,[Link],[Link]
Command:cc/gcc/iccfile.c
Make&Python
Background:Compilingwithseveralfiles
Thethreedifferenttasksrequiredtoproducetheexecutableprogram: cccgreen.c cccblue.c [Link].o
Make&Python
Background:Dependencies
[Link] data.h main.c io.h 4differenttasksrequired cccmain.c cccdata.c cccio.c [Link].o Ifyouusemake,thensimply
makeproject1
Make&Python
Background:Dependencies
[Link] data.h main.c io.h [Link],youshould cccmain.c [Link].o
Make&Python
Background:Dependencies
[Link] data.h main.c io.h [Link],youshould cccmain.c [Link].o Ifyouusemake,then makeproject1
Make&Python
HowtowriteMakefile(ormakefile)?
target:sourcefile(s) command(mustbeprecededbyatab) project1:[Link].o [Link].o data.o:[Link].h cccdata.c main.o:[Link].c cccmain.c io.o:[Link].c cccio.c Tip:Touseotherfilesuchasxfile, e.g.makefxfileproject1
Determining dependencies
Make&Python
Features
Macro(byyourself) OBJECTS=[Link].o project1:$(OBJECTS) ccoproject1$(OBJECTS) sameas $@:fullnameofthecurrenttarget project1:[Link].o [Link].o Tip:Macroonthecommandline [Link]=gccproject1 $<:sourcefileofthecurrent dependency
Builtinmacro CC:containscurrentCcompiler CFLAGS:additionaloptionstobuiltin Crule
$?:alistofprerequisitesnewerthan currenttarget
Make&Python
Predefinedrules
SuffixRule: [Link],use$(CC)$(CFLAGS)conthecorrespondingxxx.c file. [Link].o:data.h sameas data.o:[Link].h $(CC)$(CFLAGS)cdata.c
Make&Python
[Link]
POBJ=[Link].o project1:$(POBJ) cco$@$(POBJ) data.o:data.h main.o:[Link].h io.o:io.h
project1:[Link].o [Link].o data.o:[Link].h cccdata.c main.o:[Link].c cccmain.c io.o:[Link].c cccio.c POBJ=[Link].o project1:$(POBJ) cco$@$(POBJ) project1:[Link].h
POBJ=[Link].o project1:$(POBJ) cco$@$(POBJ) [Link].o:[Link].h
Make&Python
Realexample:Makefilefortransc
Make&Python
WhatsPython?(wasbornin1989Christmas)
[Link] [Link]'selegantsyntaxanddynamictyping,togetherwith itsinterpretednature,makeitagoodlanguageforscriptingandrapidapplication developmentinmanyareasonmostplatforms.
Make&Python
Howdiditworkforme?
TSV/TXPLOTandsoon..
Make&Python
Keyfeatures
[Link] [Link] [Link](Linux,Unix,Windows,MacOS,Dos,Amiga) [Link] [Link] [Link]:[Link] [Link]:pythonscriptinC/C++programs [Link]:forDB,CGI,ftp,XML,GUIandsoon [Link] [Link]&masking:<<,>>,&,|,^,~
Make&Python
Basicgrammar
1. No semi-colon 2. No declaration, No data type definition 3. Grouping of statements by indentation, NOT by curly braces { } 4. # for single line comments, for multiline comments 5. \ for explicit line joining 6. Variable only refers to the object and does not represent the object itself! 7. Five data types: - Numbers - Strings - Lists - Tuples e.g. 123, 12345L, 1.43, 5+4j e.g. ham, egg, milk e.g. [ham,egg,3] e.g (ham,egg,3) e.g. {ham:4,egg:5}
- Dictionaries
8. import libraries and your own modules using import
Make&Python
Referencesinpython
a 1 2 3
Assignment manipulates references x = y does not make a copy of y x = y makes x reference the object y references Example: >>> a = [1, 2, 3] >>> b = a >>> [Link](4) >>> print b [1, 2, 3, 4] >>> c = b[:]
a 1 b a 1 b c 1 2 3 4 2 3 4 2 3
Make&Python
Numbers
1. int object (4 byte == long type in C) 2. long object (unlimited) 3. float object (8 byte == double type in C) 4. complex object (real = 8 byte, imaj = 8 byte) e.g >>a = 12345678L >>b = 4+5j >> [Link] 4.0 >> [Link] 5.0
Make&Python
Strings
Result "helloworld" "hellohellohello" "h" "o" "ell" 5 1 1 1 # concatenation # repetition # indexing # (from end) # slicing # size # comparison # search # type
Operation "hello"+"world" "hello" * 3 "hello"[0] "hello"[-1] "hello"[1:4] len("hello") "hello" < "jello" "e" in "hello" "1235".isdigit()
Make&Python
Lists
Mutable and flexible arrays (Tuples are exactly same as Lists but immutable) a = [99, "bottles of beer", ["on", "the", "wall"]] Same operators as for strings a+b, a*3, a[0], a[-1], a[1:], len(a) Operation a[0] a[1:2] = ["bottles", "of", "beer"] del a[-1] a = range(5) [Link](5) [Link]() [Link](0, 42) [Link](0) [Link]() [Link]() Result 98 [98, "bottles", "of", "beer", ["on", "the", "wall"]] [98, "bottles", "of", "beer"] [0,1,2,3,4] [0,1,2,3,4,5] [0,1,2,3,4] and returns 5 [42,0,1,2,3,4] [0,1,2,3,4] and returns 42 [4,3,2,1,0] [0,1,2,3,4]
Make&Python
Dictionaries
Hash tables, "associative arrays" dic = {key1:value1,key2:value2,...} d = {"duck": "eend", "water": "water"} Operation d["duck"] d["back"] del d["water"] d["back"] = "rug" d["duck"] = "duik" Result "eend" # raises KeyError exception {"duck": "eend"} {"duck": "eend", "back": "rug"} {"duck": "duik", "back": "rug"} # delete # insert # overwrite
Make&Python
[Link]
Make&Python
[Link]
[Link](10,3)returnsreferenceto[0,3,6,9] 0,1,2,3,4,5,6,7,8,9