Download presentation
Presentation is loading. Please wait.
Published byKellie Holmes Modified over 9 years ago
1
H3D API Training Part 3.1: Python – Quick overview
2
Python Python Design & Syntax Python / H3D Interface Python Modules
3
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
4
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”
5
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
6
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
7
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)
8
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.”
9
Python - Strings Concatenation ◦ a = “Hello” ◦ b = “World” ◦ c = a + “ “ + b Repetition ◦ a = “Hello!” ◦ print a*5
10
Python - Strings Sub-strings ◦ word = “Hello” ◦ print word[4] ◦ o ◦ print word[0:2] ◦ He ◦ print word[2:] ◦ llo ◦ print word[:-1] ◦ Hell
11
Python - Lists List Insertion ◦ a.append( 4 ) ◦ a.insert( 1, 5 ) ◦ print a.pop()
12
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”
13
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”
14
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
15
Python - Exceptions Similar to C++, try / except block ◦ def foo(): ◦ try: ◦ print bar ◦ except: ◦ print “No Bar” ◦ raise “Foo Failed”
16
Python - Functions “def” to define functions, argument list, default values ◦ def foo( bar = 0 ): ◦ print bar ◦ foo( 1 ) ◦ foo()
17
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()
18
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()
19
Python Modules Overview of useful Python library modules ◦ String manipulation ◦ File I/O ◦ http / ftp retrieval ◦ OS operations
20
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
21
Python Modules - String atof(str[,base]), atoi(), atol() find( str, sub ) split( str, c ) join( str, c ) replace( str, old, new )
22
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
23
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
24
Python Modules - OS chdir( path ) listdir( path ) mkdir( path ) rename( src, dst ) os.path: ◦ isfile( f ) ◦ isdir( f ) ◦ join( path1, path2 )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.