1 / 24

H3D API Training

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

Download Presentation

H3D API Training

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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) # succeedsre.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 )

More Related