Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
H3D API Training Part 3.1: Python – Quick overview Python Python Design & Syntax Python / H3D Interface Python Modules 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 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” 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 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 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) 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.” Python - Strings Concatenation ◦ a = “Hello” ◦ b = “World” ◦ c = a + “ “ + b Repetition ◦ a = “Hello!” ◦ print a*5 Python - Strings Sub-strings ◦ word = “Hello” ◦ print word[4] ◦ ◦ ◦ ◦ ◦ ◦ ◦ o print word[0:2] He print word[2:] llo print word[:-1] Hell Python - Lists List Insertion ◦ a.append( 4 ) ◦ a.insert( 1, 5 ) ◦ print a.pop() 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” 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” 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 Python - Exceptions Similar to C++, try / except block ◦ ◦ ◦ ◦ def foo(): try: print bar except: ◦ print “No Bar” ◦ raise “Foo Failed” Python - Functions “def” to define functions, argument list, default values ◦ ◦ def foo( bar = 0 ): print bar ◦ foo( 1 ) ◦ foo() 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() 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() Python Modules Overview of useful Python library modules ◦ ◦ ◦ ◦ String manipulation File I/O http / ftp retrieval OS operations 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 Python Modules - String atof(str[,base]), atoi(), atol() find( str, sub ) split( str, c ) join( str, c ) replace( str, old, new ) 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 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 Python Modules - OS chdir( path ) listdir( path ) mkdir( path ) rename( src, dst ) os.path: ◦ isfile( f ) ◦ isdir( f ) ◦ join( path1, path2 )