h3d api training part 3.1: python – quick overview

24
H3D API Training Part 3.1: Python – Quick overview

Upload: kellie-holmes

Post on 12-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: H3D API Training  Part 3.1: Python – Quick overview

H3D API Training

Part 3.1: Python – Quick overview

Page 2: H3D API Training  Part 3.1: Python – Quick overview

Python Python Design & Syntax Python / H3D Interface Python Modules

Page 3: H3D API Training  Part 3.1: Python – Quick overview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Python Modules - OS chdir( path ) listdir( path ) mkdir( path ) rename( src, dst ) os.path:◦ isfile( f )◦ isdir( f )◦ join( path1, path2 )