H3D API Training Part 3.1: Python – Quick overview.
-
Upload
kellie-holmes -
Category
Documents
-
view
229 -
download
0
Transcript of H3D API Training Part 3.1: Python – Quick overview.
![Page 1: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/1.jpg)
H3D API Training
Part 3.1: Python – Quick overview
![Page 2: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/2.jpg)
Python Python Design & Syntax Python / H3D Interface Python Modules
![Page 3: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/3.jpg)
Python Design
◦ Written in C◦ Python language is Object Oriented◦ Exception handling◦ Uses modules to offer specific features / functionality◦ Can be extended to support new types / modules written in
C or C++ (e.g. H3D API’s basic types)◦ Memory management and automatic garbage collection
![Page 4: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/4.jpg)
Python - Scope Python uses indentation to define scope Note: tabs and spaces are not equivalient in
determining scope in python - be careful of mixing tabs and spaces!
◦ if (x != 0):
◦ print x
◦ else:
◦ print “NONE”
![Page 5: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/5.jpg)
Python - Modules Python modules located via PYTHONPATH
environment variable Can import module, or import all symbols in a module
into the current scope:
◦ import os
◦ from math import *
◦ from math import cos
![Page 6: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/6.jpg)
Python - Maths C-style New line terminates a statement
◦ x = 3
◦ y = 5
◦ avg = (x + y) / 2
◦ print avg
Use ‘\’ to join two or more lines into a single statment
![Page 7: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/7.jpg)
Python - Maths import math
a = float(1)
b = int(1.6)
c = math.floor(1.6)
d = math.sqrt(9)
e = math.sin(3.1415)
f = math.log10(5)
![Page 8: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/8.jpg)
Python - Strings Delimited by either single or double quotes
◦ x = “Hello World”
◦ y = ‘Hello World’
◦ z = ‘”Yes,” he said.’
◦ w = “\”Yes,\” he said.”
Can span multiple lines using ‘\’◦ hello = "This is a rather long string containing\n\
◦ several lines of text just as you would do in C.\n\
◦ Note that whitespace at the beginning of the line is\
◦ significant.”
![Page 9: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/9.jpg)
Python - Strings Concatenation
◦ a = “Hello”
◦ b = “World”
◦ c = a + “ “ + b
Repetition◦ a = “Hello!”
◦ print a*5
![Page 10: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/10.jpg)
Python - Strings Sub-strings
◦ word = “Hello”
◦ print word[4]
◦ o
◦ print word[0:2]
◦ He
◦ print word[2:]
◦ llo
◦ print word[:-1]
◦ Hell
![Page 11: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/11.jpg)
Python - Lists
List Insertion
◦ a.append( 4 )
◦ a.insert( 1, 5 )
◦ print a.pop()
![Page 12: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/12.jpg)
Python - Globals
Scope of variables in functions defined by first use◦ First use an assignment, presumed local◦ First use a reference, presumed global◦ Can override with global operator
◦ X = “test”
◦ def foo():
◦ global X
◦ X = “bar”
![Page 13: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/13.jpg)
Python - If Standard C style, if, else, elif Condition can be any valid python expression that
returns a value Does not require parenthesis
◦ if x > 5:
◦ print “Large”
◦ elif x > 2:
◦ print “Medium”
◦ else:
◦ print “Small”
![Page 14: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/14.jpg)
Python - For Unlike C, set based
◦ iterate over strings, lists and tuples
◦ items = [ “A”, “B”, “C” ]
◦ for i in items:
◦ print i
◦ str = “Hello”
◦ for c in str:
◦ print c
![Page 15: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/15.jpg)
Python - Exceptions Similar to C++, try / except block
◦ def foo():
◦ try:
◦ print bar
◦ except:
◦ print “No Bar”
◦ raise “Foo Failed”
![Page 16: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/16.jpg)
Python - Functions
“def” to define functions, argument list, default values
◦ def foo( bar = 0 ):
◦ print bar
◦ foo( 1 )
◦ foo()
![Page 17: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/17.jpg)
Python - Classes
Similar to C++◦ Constructor, base class (multiple inheritance), static
members, etc◦ class Basic:◦ def __init__( self, name ):
◦ self.name = name
◦ def getName( self ):
◦ print self.name
◦ x = Basic( “X” )
◦ y = Basic( “y” )
◦ x.getName()
![Page 18: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/18.jpg)
Python - Classes
Inheritance◦ class Special(Basic):◦ def __init__( self, name ):
◦ Basic.__init__( self, name )
◦ def getName( self ):
◦ print self.name
◦ x = Basic( “X” )
◦ y = Special( “y” )
◦ y.getName()
![Page 19: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/19.jpg)
Python Modules Overview of useful Python library modules◦ String manipulation◦ File I/O◦ http / ftp retrieval◦OS operations
![Page 20: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/20.jpg)
Python Modules - Math
sin(), cos(), tan(), acos(), etc degrees( a ), radians( a ) floor( x ) fmod( x, y ) log( x, base ) pow( x, y ) constants: pi, e
![Page 21: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/21.jpg)
Python Modules - String atof(str[,base]), atoi(), atol() find( str, sub ) split( str, c ) join( str, c ) replace( str, old, new )
![Page 22: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/22.jpg)
Python Modules - re Regular expression module◦ String matching, basic parsing◦ compile( re_str ) # compile a RE◦ search( pattern, str )◦ split( pattern, str )–
re.compile("a").match("ba", 1) # succeeds
re.compile("^a").search("ba", 1) # fails
re.compile("^a").search(”\na", 1) # fails
![Page 23: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/23.jpg)
Python Modules - File I/O Builtin functions◦ f = open( filename, “r” )◦ f.write( text )◦ text = f.read()◦ f.close()
OS Module contains file / directory manipulation
![Page 24: H3D API Training Part 3.1: Python – Quick overview.](https://reader035.fdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba7285/html5/thumbnails/24.jpg)
Python Modules - OS chdir( path ) listdir( path ) mkdir( path ) rename( src, dst ) os.path:◦ isfile( f )◦ isdir( f )◦ join( path1, path2 )