learning python (part1 & part2)

90
Learning Python A short Python language presentation

Upload: asuras

Post on 08-Sep-2015

28 views

Category:

Documents


2 download

DESCRIPTION

Learning Python (Part1 & Part2)

TRANSCRIPT

  • Learning Python

    A short Python language presentation

    *

    Nota mea

  • Learning Python

    Python fundamentals

    Part I

    *

    Nota mea

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Overview

    IntroductionTypes and OperationsStatements and Syntax

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Introduction

    What is Python?

    Python is a general purpose programming language that is often applied in scripting roles. It is commonly defined as an object-oriented scripting languagea definition that blends support for OOP with an overall orientation toward scripting roles.

    What is the Python Philosophy ?

    Software quality

    Developer productivity

    Program portability

    Component integration

    Support libraries

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Introduction

    What's Python Good For?

    System utilities - portable command-line tools, testing, system administration scriptsGUIs - with tools such as Tk, wxPython, Qt, Gtk, PythonCard, Dabo, BoaConstructor Component integration - C/C++ library frontends, product customizationRapid-prototyping/developmentLanguage-based modules - Replacing special-purpose parsers with PythonDistributed programming - With client/server APIsAnd more - Internet scripting, Image processing, numeric programming, gaming, AI, etc.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Introduction

    Python Program Structure

    Python programs can be decomposed into modules, statements, expressions, and objects, as follows:

    Programs are composed of modules.Modules contain statements.Statements contain expressions.Expressions create and process objects.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    PYTHON

    Types and Operations

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    The Dynamic Typing

    Types are determined automatically at runtime. A variable is created when it is first assigned a value. For example, when we say this:

    >>> a = 3

    Python will perform three distinct steps to carry out the request:

    Create an object to represent the value 3.

    Create the variable a, if it does not yet exist.

    Link the variable a to the new object 3.

    The net result will be a structure inside Python:

    (this steps reflect the operation of all assignments in the Python language)

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    The Dynamic Typing Model (continued)

    Let's extend the example and watch the net result for the next

    assignments :

    >>> a = 3

    >>> b = a

    Next, suppose we extend the session with one more statement:

    >>> a = 3

    >>> b = a

    >>> a = 'spam'

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Numbers

    Normal integers (C longs)e.g. 1234, -24, 0Long integers (unlimited size)e.g. 9999999999999999999LFloating-point (C doubles)e.g. 1.23, 3.14e-10, 4E210, 4.0e+210Octal and hexadecimal numberse.g. 0177, 0x9ff, 0XFFComplex numberse.g. 3+4j, 3.0+4.0j, 3J

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Python expression operators and precedence

    OperatorsDescriptionlambda args: expressionanonymous function generationx or ylogical or (y is evaluated only if x is false)x and ylogical and (y is evaluated only if x is true)not xlogical negationx < y, x y, x >= y, x == y,x y, x != y, x is y, x is not y, x in y, x not in y comparison operators, value equality operators, object identity tests, sequence membershipx | y bitwise orx ^ ybitwise exclusive orx & ybitwise andx > y shift x left or right by y bits-x + y, x - yaddition / concatenation, subtraction

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Performing operations:

    operator precedence rule: operators lower in the table have higher precedence In mixed type expressions: converts operands up to the type of the most complicated operand:

    integers -> long integers -> floating-point numbers -> complex numbers.

    OperatorsDescriptionx * y, x % y, x / y,x // ymultiplication / repetitionremainderclassic divisionfloor division-x, +x, ~x, x ** yunary negation, identity, bitwise complement; binary powerx[i], x[i:j], x.attr, x(...)indexing, slicing, qualification, function calls(...), [...], {...}, `...`tuple, list, dictionary, string conversion

    *

    How does Python know which operator to perform first? The solution to this lies in operator precedence: operators lower in the table have higher precedence and so bind more tightly in mixed expressions.

    In mixed type expressions, Python first converts operands up to the type of the most complicated operand, and then performs the math on same-type operands. Python ranks the complexity of numeric types like so: integers are simpler than long integers, which are simpler than floating-point numbers, which are simpler than complex numbers.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Strings

    Common string literals and operations: OperationInterpretations1 = Empty strings2 = "spam's"Double quotesblock = """..."""Triple-quoted blockss3 = r'\temp\spam'Raw stringss4 = u'spam'Unicode Stringss1 + s2 s2 * 3Concatenate, repeats2[i] s2[i:j] len(s2)Index, slice, length"a %s parrot" % 'dead'String formattingS2.find('pa') s2.replace('pa', 'xx') s1.split( )String method callsfor x in s2 'm' in s2Iteration, membership"s\tp\naEscape sequences

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Basic string operations:OperationResultMeaning"hello"+"world "helloworldconcatenation"hello"*3"hellohellohello"repetition"hello"[0]"h"indexing"hello"[-1]"o"indexing from end"hello"[1:4]"ell"slicinglen("hello")5size"hello" < "jello"1 (or boolean value True)comparison"e" in "hello"1 (or boolean value True)search

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Escape Sequences Code Special Bytes: Backslashes are used to introduce special byte codings, known as escape sequences. Escape sequences let us embed byte codes in strings that cannot be easily typed on a keyboard. EscapeMeaning\\Backslash (keeps a \)\Single quote (keeps `)\Double quote (keeps ")\aBell\bBackspace\fFormfeed\nNewline (linefeed)\rCarriage return\tHorizontal tab\vVertical tab

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    For example:

    >>> s = 'a\nb\tc' # five-character string that embeds a newline and a tab

    >>> s

    'a\nb\tc'

    >>> print s# NEWLINE and TAB effect on print statement

    a

    b c

    EscapeMeaning\uhhhhUnicode 16-bit hex\UhhhhhhhhUnicode 32-bit hex \xhhHex digits value hh\oooOctal digits value\0Null (doesn't end string)\otherNot an escape (kept)

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    String Methods:

    There are usually two ways to invoke advanced string operations:

    # A method: by calling object methods

    X.method(arguments)

    # B method: calling string module functions and passing in the object as an argument

    # (presume that you have already imported the native python module string)

    string.method(X, arguments)

    A methodB method>>> S = 'a+b+c+' >>> S = 'a+b+c+' >>> x = S.replace('+', 'spam') >>> x >>> import string >>> y = string.replace(S, '+', 'spam') >>> y 'aspambspamcspam''aspambspamcspam'

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Lists

    Properties of Python lists:

    Ordered collections of arbitrary objects;

    Accessed by offset;

    Variable length, heterogeneous, arbitrarily nest-able;

    Of the category mutable sequence

    Arrays of object references

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Common list literals and operations:OperationInterpretationL1 = [ ]an empty listL2 = [0, 1, 2, 3]four items: indexes 0..3L3 = ['abc', ['def', 'ghi']]nested sublistsL2[i] or L3[i][j], L2[i:j], len(L2)index, slice, lengthL1 + L2 L2 * 3concatenate, repeatfor x in L2, 3 in L2iteration, membershipL2.append(4), L2.extend([5,6,7]) L2.sort( ) L2.index(1) L2.reverse( )growsort, search (Methods)reversedel L2[k], del L2[i:j], L2.pop( ), L2[i:j] = [ ]shrinkingL2[i] = 1, L2[i:j] = [4,5,6]index assignment, slice assignmentrange(4), xrange(0, 4)make lists / tuples of integers

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Basic list operations:OperationResultMeaningL = ['spam', 'Spam', 'SPAM!']L[2]L[-2]L[1:]'SPAM!' 'Spam' ['Spam', 'SPAM!'] indexingindexing from endslicing[1, 2, 3] + [4, 6][1, 2, 3, 4, 6]concatenation['Ni!'] * 4['Ni!', 'Ni!', 'Ni!', 'Ni!']repetitionlen([1, 2, 3])3length3 in [1, 2, 3]1 (or boolean value True )membershipfor x in [1, 2, 3]: print x>>> 1 2 3iteration

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Dictionaries

    Properties of Python dictionaries:

    Unordered collections of arbitrary objects

    Accessed by key, not offset

    Variable length, heterogeneous, arbitrarily nest-able

    Of the category mutable mapping

    Tables of object references (hash tables)

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Common dictionary literals and operations:OperationInterpretationD1 = { }empty dictionaryD2 = {'spam': 2, 'eggs': 3}two-item dictionaryD3 = {'food': {'ham': 1, 'egg': 2}}nestingD2['eggs'] D3['food']['ham']indexing by keyD2.has_key('eggs') 'eggs' in D2D2.keys( ) D2.values( ) D2.copy( ) D2.get(key, default) D2.update(D1)membership test, keys listvalues listcopies (Methods)defaultsmergelen(d1)length (number stored entries)D2[key] = 42 del D2[key]adding / changing, deleting

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Tuples

    Properties of Python tuples:

    Ordered collections of arbitrary objects

    Accessed by offset

    Fixed length, heterogeneous, arbitrarily nest-able

    Of the category immutable sequence

    Arrays of object references

    Tuples work exactly like lists, except that tuples can't be changed in-place (they're immutable) and don't support any method calls.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Common tuple literals and operations:OperationInterpretation( )an empty tuplet1 = (0,)a one-item tuple (not an expression)t2 = (0, 'Ni', 1.2, 3)a four-item tuplet2 = 0, 'Ni', 1.2, 3another four-item tuple (same as prior line)t3 = ('abc', ('def', 'ghi'))nested tuplest1[i], t3[i][j]t1[i:j]len(t1)indexslicelengtht1 + t2t2 * 3concatenaterepeat for x in t2 3 in t2iterationmembership

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    References Versus Copies

    >>> X = [1, 2, 3]

    >>> L = ['a', X, 'b'] # Embed references to X's object.

    >>> D = {'x':X, 'y':2}

    >>> X[1] = 'surprise' # Changes all three references!

    >>> L

    ['a', [1, 'surprise', 3], 'b']

    >>> D

    {'x': [1, 'surprise', 3], 'y': 2}

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    References Versus Copies (continued)

    The other way around is to simply assign copies to the other variables, not references to the same objects:

    >>> A = L[:] # Instead of: A = L (or list(L))

    >>> B = D.copy( ) # Instead of: B = D

    This way, changes made from other variables change only the copies, not the originals.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Files

    Common file operations:OperationInterpretationoutput = open('/tmp/spam', 'w')Create output file ('w' means write).input = open('data', 'r')Create input file ('r' means read).S = input.read( )Read entire file into a single string.S = input.read(N)Read N bytes (1 or more).S = input.readline( )Read next line (through end-line marker).L = input.readlines( )Read entire file into list of line strings.output.write(S)Write string S into file.output.writelines(L)Write all line strings from list L into file.output.close( )Manual close

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Type categories

    Next table classifies all the types we've seen:If an object type is immutable, you cannot change its value in-place; Python raises an error if you try it. Instead, run code to make a new object for a new value. Generally, immutable types give some degree of integrity, by guaranteeing that an object won't be changed by another part of a program. Object typeCategoryMutable?NumbersNumericNoStringsSequenceNoListsSequenceYesDictionariesMappingYesTuplesSequenceNoFilesExtensionn/a

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Pythons Type Hierarchies

    All the built-in object types available in Python are summarized in figure:

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Types and Operations

    Exercises

    Create a module named Numbers.py. In this module, define two variables n and m at which assign two numbers. Display in the interactive window the results of sum, multiplication, division and power between n and m.

    Create a new module named Strings.py. Define two strings S1 = "black" and S2 = "white". Replace "ck" with "xx" in string S1. Display in the interactive window the results of concatenating S1 and S2, repeating S1 twice and the length of S2 string.

    Create a new module named Lists.py. Define a list named L that contains four numbers (e.g., L=[0,1,2,3]). Add a new element to the list L. Display the elements which have the indexes from 1 to 3. Delete the element 3 and then display the list L.

    Create a dictionary named D with three entries, for keys 'a', 'b', and 'c'. What happens if you try to index a nonexistent key (D['d'])? What does Python do if you try to assign to a nonexistent key d (e.g., D['d']=3)? Display the dictionary and then only the values of dictionary.

    Write a script that creates a new output file called myfile.txt and writes the string "Hello file world!" into it. Then write another script that opens myfile.txt, and reads and prints its contents.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    PYTHON

    Statements and Syntax

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Statements and Syntax

    Assignment Statements

    Assignments create object references. Names are created when first assigned. Names must be assigned before being referenced. Implicit assignments: import, from, def, class, for, function arguments.

    Next table illustrates the different assignment statements in Python:

    OperationInterpretationspam = 'Spam'Basic formspam, ham = 'yum', 'YUM'Tuple assignment (positional)[spam, ham] = ['yum', 'YUM']List assignment (positional)spam = ham = 'lunch'Multiple-target

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Statements and Syntax

    Python Statements

    At its core, Python syntax is composed of statements and expressions:

    Expressions process objects, and are embedded in statements. Statements :

    use and direct expressions to process the objects;

    are where objects spring into existence (e.g., in expressions within assignment statements);

    (some of) create entirely new kinds of objects (functions, classes, and so on);

    always exist in modules, which themselves are managed with statements;

    StatementRoleExampleAssignmentCreating referencescurly, moe, larry = 'good', 'bad', 'ugly'CallsRunning functionsstdout.write("spam, ham, toast\n")printPrinting objectsprint 'The Killer', jokeif / elif / elseSelecting actionsif "python" in text: print textfor/elseSequence iterationfor x in mylist: print xwhile / elseGeneral loopswhile 1: print 'hello'

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Statements and Syntax

    Python Statements (continued)

    StatementRoleExamplepassEmpty placeholderwhile 1: passbreak, continueLoop jumpswhile 1: if not line: breaktry / except / finallyCatching exceptionstry: action( )except: print 'action error'raiseTriggering exceptionraise endSearch, locationimport, fromModule accessimport sys; from sys import stdindef, return, yieldBuilding functionsdef f(a, b, c=1, *d): return a+b+c+d[0]def gen(n): for i in n, yield i*2classBuilding objectsclass subclass: staticData = [ ]globalNamespacesdef function( ): global x, y; x = 'new'delDeleting referencesdel data[k]; del data[i:j]; del obj.attrexecRunning code stringsexec "import " + modName in gdict, ldictassertDebugging checksassert X > Y

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Statements and Syntax

    Variable Name Rules

    Syntax: (underscore or letter) + (any number of letters, digits, or underscores)

    Variable names must start with an underscore or letter, and be followed by any number of letters, digits, or underscores

    Case matters: SPAM is not the same as spam

    Python always pays attention to case in programs, both in names you create and in reserved words. For instance, names X and x refer to two different variables.

    Reserved words

    Names we define cannot be the same as words that mean special things in the Python language. (ex.: if we try to use a variable name like class, Python will raise a syntax error, but klass and Class work fine)

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Statements and Syntax

    Naming conventions

    Besides these rules, there is also a set of naming conventionsrules which are not required, but are used in normal practice:

    Names that begin with a single underscore (_X) are not imported by a from module import * statement.Names that have two leading and trailing underscores (__X__) are system-defined names, which have special meaning to the interpreter.Names that begin with two underscores and do not end with two more (__X) are localized ("mangled") to enclosing classes.The name that is just a single underscore (_) retains the result of the last expression, when working interactively.

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Statements and Syntax

    Python Reserved Words

    anddelforisraiseasserteliffromlambdareturnbreakelseglobalnottryclassexceptiforwhilecontinueexecimportpassyielddeffinallyinprint

    *

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Statements and Syntax

    Expression Statements

    Expressions are commonly used as statements in two situations:

    For calls to functions and methods - Some functions and methods do lots of work without returning a value. Since you're not interested in retaining the value that they return, you can call such functions with an expression statement.

    For printing values at the interactive prompt - Python echoes back the results of expressions typed at the interactive command line. Technically, these are expression statements too; they serve as a shorthand for typing print statements.

    Next table lists some common expression statement forms in Python:

    OperationInterpretationspam(eggs, ham)Function callsspam.ham(eggs)Method callsspamInteractive printspam < ham and ham != eggsCompound expressionsspam < ham < eggsRange tests

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Statements and Syntax

    if Statements

    General format:

    if : # if test

    # Associated block

    elif : # Optional elifs

    else:# Optional else

    Example:

    >>> x = 'killer rabbit'

    >>> if x == 'roger':

    ... print "how's jessica?"

    ... elif x == 'bugs':

    ... print "what's up doc?"

    ... else:

    ... print 'Run away! Run away!

    Run away! Run away! the result of if statement

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Statements and Syntax

    while Loops

    General format:

    The while statement consists of a header line with a test expression, a body of one or more indented statements, and an optional else part that is executed if control exits the loop without running into a break statement.

    while : # Loop test

    # Loop body

    else:# Optional else

    # Run if didn't exit loop with break

    Example:

    >>> a = 0; b=10

    >>> while a

  • Learning Python

    Advanced Python

    Part II

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Overview

    FunctionsModulesClassesExceptionsExtending Python

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    PYTHON

    FUNCTIONS

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Functions

    General form:

    def (arg1, arg2,... argN):

    return# from procedure

    return # from function

    Main concepts behind Python functions::

    def- is executable code. def- creates an object and assigns it to a name. return - sends a result object back to the caller; arguments are passed by assignment (object reference). global - declares module-level variables that are to be assigned; arguments, return values, and variables are not declared.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Functions

    Anonymous Functions: lambda

    The lambda's general form:

    lambda arg1, arg2,... argN : expression using arguments

    Like def, lambda expression creates a function to be called later, but returns it instead of assigning it to a name. This is why lambdas are sometimes known as anonymous (i.e., unnamed) functions. Differences between lambda and def:

    lambdais an expression, not a statement.

    lambdabodies are a single expression, not a block of statements.

    E.g.

    >>> f = lambda x, y, z: x + y + z

    >>> f(2, 3, 4)

    9 the result of function call

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Functions

    Example function:

    Definition:

    >>> def times(x, y): # Create and assign function.

    ... return x * y # Body executed when called.

    ...

    Calls:

    >>> times(2, 4) # Arguments in parentheses

    8

    >>> x = times(3.14, 4) # Save the result object.

    >>> x

    12.56

    >>> times('Ni', 4) # Functions are "typeless."

    'NiNiNiNi'

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Functions

    Exercises

    Create a module named Function1.py. In this module, write a function that prints its single argument to the screen and call it, passing a variety of object types: integer, string, list, dictionary.

    Create a module named Function2.py. Write a function called copyDict(dict) that copies its dictionary argument. It should return a new dictionary with all the items in its argument. Use the dictionary keys method to iterate.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    PYTHON

    MODULES

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Modules

    Definition

    A module is a file containing Python definitions and statements. The file should have suffix .py.

    Python program architecture

    multiple text files containing Python statements:

    one main, top-level file the file you run to launch your application;

    zero or more supplemental files known as modules in Python;

    The module files are libraries of tools, used by the top-level file, and possibly elsewhere. In Python, a file imports a module to gain access to the tools it defines that are known as its attributes (variable names attached to objects such as functions)

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Modules

    Module design concept

    You're always in a module in Python. Any .py file is a python module;Minimize module coupling: global variables. Like functions, modules work best if they're written to be closed boxes (minimal external interactions). Modules should rarely change other modules' variables. Modules contain variables, functions, classes, and other modules (if imported):

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Modules

    Module usage examples

    >>> import module1 # Get module as a whole.

    >>> module1.printer('Hello world!') # Qualify to get names.

    Hello world!

    >>> from module1 import printer # Copy out one variable.

    >>> printer('Hello world!') # No need to qualify name.

    Hello world!

    >>> from module1 import * # Copy out all variables.

    >>> printer('Hello world!')

    Hello world!

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Modules

    How import work

    Imports are really runtime operations that perform three distinct steps at the first time when a file (module) is imported by a program:

    Find the module's file (Python uses a standard module search path to locate the module file corresponding to an import statement).

    Compile it to byte-code (if needed).

    Run the module's code to build the objects it defines.

    Imports (both forms, import and from import statements) load and run a module's code only on the first time when the module is imported in a process.Later import statements use the already loaded module object without reloading or rerunning the file's code

    Warning!: after module was modified use reload instead of import! The reload function forces an already loaded module's code to be reloaded and rerun. Assignments in the file's new code change the existing module object in-place.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Modules

    Module Packages

    A package is a collection of modules which reside on a sub-directory structure.

    Example:

    import dir1.dir2.mod # import module mod.py from dir1\dir2

    from dir1.dir2.mod import x# import object x from module dir1\dir2\mod.py

    Each directory named in the path of a package import statement must also contain a file named __init__.py (or else your package imports will fail). In the example we've been using, both dir1 and dir2 must contain a file called __init__.py even if is empty.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Modules

    Package import example

    Start conditions:

    >>> import dir1.dir2.mod # First imports run init files.

    dir1 init the result of import statement

    dir2 init across subdirectories (processing each __init__.py)

    in mod.py mod.py processing result

    >>> import dir1.dir2.mod # Later imports does not.

    >>>

    >>> reload(dir1.dir2)

    dir2 init

    FileInside linesdir1\__init__.pyprint 'dir1 init' x = 1dir1\dir2\__init__.pyprint 'dir2 init' y = 2dir1\dir2\mod.pyprint 'in mod.pyz = 3

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Modules

    Exercises

    Write a program that counts lines and characters in a file. With your text editor, code a Python module called mymod.py, which exports two top-level names:

    A countLines(name) function that reads an input file and counts the number of lines in it (hint: file.readlines( ) does most of the work for you)

    A countChars(name) function that reads an input file and counts the number of characters in it (hint: file.read( ) returns a single string)

    Write a second module, myclient.py, which imports mymod and tests its functions; then, run myclient.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    PYTHON

    CLASSES

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Definition

    A Python class is a device used to implement new kinds of objects in Python. Classes are Python's main object-oriented programming (OOP) tool.

    Why use classes

    Classes are:

    - a Python program unit, just like functions and modules.

    - another way for packaging logic and data (classes also define a new namespace -- much like modules)

    Classes have three critical distinctions that make them more useful when it comes to building new objects:

    Give possibility to have multiple instances;

    Customization via inheritance;

    Operator overloading;

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    General form

    class ([superclass,]...):# Assign to name

    data = value # Shared class data

    def method(self,...): # Methods

    self.member = value # Per-instance data

    Methods

    Methods are just function objects created by def statements nested in a class statement's body. Methods first argument always receives the instance object that is the implied subject of a method call (self).Method calls made through an instance are automatically translated to class method function calls:

    instance.method(args...) class.method(instance, args...)

    (In fact, both call forms are valid in Python)

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Example

    class NextClass: # Define class.

    def printer(self, text): # Define method.

    self.message = text # Change instance.

    print self.message # Access instance.

    >>> x = NextClass( ) # Make instance

    >>> x.printer('instance call') # Call its method

    instance call

    >>> x.message # Instance changed

    'instance call

    >>> NextClass.printer(x, 'class call') # Direct class call

    class call

    >>> x.message # Instance changed again

    'class call'

    >>> NextClass.printer('bad call')

    TypeError: unbound method printer( ) must be called with NextClass instance...

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Multiple instances

    There are two kinds of objects in Python's OOP :

    - Class objects provide default behavior:

    The class statement creates a class object and assigns it a name.

    Assignments inside class statements make class attributes. After running a class statement, class attributes are accessed by name qualification: object.name.

    Class attributes provide object state and behavior.

    - Instance objects are concrete items:

    Calling a class object like a function makes a new instance object.

    Each instance object inherits class attributes and gets its own namespace.

    Assignments to attributes of self in methods make per-instance attributes.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Example

    >>> class FirstClass: # define a class object.

    ... def setdata(self, value): # define class methods.

    ... self.data = value # self is the instance.

    ... def display(self):

    ... print self.data # self.data: per instance

    >>> x = FirstClass( ) # make two instances.

    >>> y = FirstClass( ) # each is a new namespace.

    By calling the class in this way (notice the parenthesis), it generates instance objects, which are just namespaces that have access to their class's attributes. So we have three objectstwo instances and a class. So, we have three linked namespaces, as sketched in figure:

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Inheritance

    In Python, instances inherit from classes, and classes inherit from superclasses. Here are the key ideas behind the machinery of attribute:

    Superclasses are listed in parentheses in a class header. The class that inherits is called a subclass, and the class that is inherited from is its superclass.

    Classes inherit attributes from their superclasses.

    Instances inherit attributes from all accessible classes.

    Each object.attribute reference invokes a new, independent search.

    Python searches a tree of linked objects, for the first appearance of the attribute that it can find, looking in object and all classes above it, from bottom to top and left to right.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Example

    The next example builds on the one before. Let's define a new class called SecondClass, which inherits all of FirstClass's names and provides one of its own:

    >>> class SecondClass(FirstClass): # inherits setdata from FirstClass

    ... def display(self): # changes/replace display

    ... print 'Current value = "%s"' % self.data

    >>> z = SecondClass( )

    >>> z.setdata(42) # setdata found in FirstClass

    >>> z.display( ) # finds overridden method in SecondClass.

    Current value = "42"

    >>> x.display( ) # x is still a FirstClass instance (old message).

    New value

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Operators Overloading

    Here are the key ideas behind overloading:

    Operator overloading lets objects coded with classes to intercept and respond to operations that work on built-in types.

    Classes can overload all Python expression operators.

    Classes can also overload operations: printing, calls, qualification, etc.

    Python operator overloading is implemented by providing specially named class methods to intercept operations. For instance, if an object inherits an __add__ method, it is called when the object appears in a + expression.

    Operators allow classes to integrate with Python's object model.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Common operator overloading methods

    MethodOverloadsCalled for__init__ConstructorObject creation: Class( )__del__DestructorObject reclamation__add__ Operator '+'X + Y, X += Y__or__Operator '|' (bitwise or)X | Y, X |= Y__repr__, __str__Printing, conversionsprint X, `X`, str(X)__call__Function callsX( )__getattr__QualificationX.undefined__setattr__Attribute assignmentX.any = value__getitem__IndexingX[key], for loops, in tests __setitem__Index assignmentX[key] = value__len__Lengthlen(X) __cmp__ComparisonX == Y, X < Y__lt__Specific comparisonX < Y (or else __cmp__)__eq__Specific comparisonX == Y(or else __cmp__)__iter__Iteration contextsfor loops, in tests, others

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Example

    We define a subclass of SecondClass, which implements three specially-named attributes that Python will call automatically:

    __init__ : is called when a new instance object is being constructed

    (self is the new ThirdClass object)

    __add__: is called when a ThirdClass instance appears in + expressions

    __mul__ : is called when a ThirdClass instance appears in * expressions

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    >>> class ThirdClass(SecondClass):# is-a SecondClass

    ...def __init__(self, value): # On "ThirdClass(value)"

    ... self.data = value

    ... def __add__(self, other): # On "self + other"

    ... return ThirdClass(self.data + other)

    ... def __mul__(self, other): # On "self * other"

    ... self.data = self.data * other

    >>> a = ThirdClass("abc")# New __init__ called

    >>> a.display( ) # Inherited method

    Current value = "abc"

    >>> b = a + 'xyz' # New __add__: makes a new instance

    >>> b.display( )

    Current value = "abcxyz

    >>> a * 3 # New __mul__: changes instance in-place

    >>> a.display( )

    Current value = " abcabcabc

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Classes

    Exercises

    Create a module called Inheritance.py. Write a class called Adder that exports a method add(self, x, y) that prints a "Not Implemented" message. Then define two subclasses of Adder that implement the add method:

    ListAdder, with an add method that returns the concatenation of its two list arguments

    DictAdder, with an add method that returns a new dictionary with the items in both its two dictionary arguments

    Experiment by making instances of all three of your classes interactively and calling their add methods.

    Create a module called OperatorOverloading.py. Write a class called Mylist that overload a Python list operation: concatenation.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    PYTHON

    EXCEPTIONS

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Exceptions

    Definition

    Exceptions are events that can modify the flow of control through a program and let us jump out of arbitrarily large chunks of a program. In Python, exceptions are triggered automatically on errors, and can be both triggered and intercepted by your code.

    Exception Roles

    Error handling - Python raises exceptions whenever it detects errors in programs at runtime

    Event notification - exceptions can also signal a valid condition, without having to pass result flags around a program or test them explicitly

    Termination actions - the try/finally statement allows us to guarantee that required closing-time operations will be performed

    Unusual control-flows

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Exceptions

    Exception form variations:try/except

    Catch and recover from exceptions raised by Python, or by you.

    try/finally

    Perform cleanup actions whether exceptions occur or not.

    raise

    Trigger an exception manually in your code.

    assert

    Conditionally trigger an exception in your code.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Exceptions

    The try/else/except statement

    try:

    # Run this action first.

    except :

    # Run if any of these exceptions occur.

    except , :

    # Run if name3 is raised, and get extra data.

    except:

    # Run for all (other) exceptions raised.

    else:

    # Run if no exception was raised by try block.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Exceptions

    The try/finally statement

    try:

    # Run this action first.

    finally:

    # Always run this code on the way out.

    The raise Statement

    raise # Manually trigger an exception.

    raise , # Pass extra data to catcher too.

    raise # Reraise the most recent exception.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Exceptions

    When you write try statements, a variety of clauses can appear after the try statement block:

    except clauses that list no exception name, catch all exceptions not previously listed in the try statement (except:).except clauses that list a set of exceptions in parenthesis catch any of the listed exceptions (except (e1,e2,e3)).Clause formInterpretationexcept:Catch all (other) exception types.except name:Catch a specific exception onlyexcept name, value:Catch exception and its extra data.except (name1, name2, ):Catch any of the listed exceptions.except (name1, name2), value:Catch any, and get the extra data.else:Run block if no exceptions raised.finally:Always perform block.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Exceptions

    Simple Example

    >>> def fetcher(obj, index):# target function

    ...return obj[index]

    >>> x = 'spam'

    >>> fetcher(x, 3) # Like x[3]

    'm'

    - without exception handling-- handling the exception -

    >>> fetcher(x, 4) >>> try:

    Traceback (most recent call last):...fetcher(x, 4)

    File "", line 1, in ?... except IndexError:

    File "", line 2, in fetcher... print 'got exception'

    IndexError: string index out of range

    'got exception the result of exception handling

    >>>

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Exceptions

    Example (Py2.5): Coding Termination Actions with try/finally

    (In this code, we've wrapped a call to a file-processing function in a try with a finally clause, to make sure that the file is always closed, whether the function triggers an exception or not.)

    MyError = "my error

    def stuff(file):

    try:

    x= file.write(SomeInfo)# try to do some illegal action

    except:

    raise MyError# Trigger exception manually.

    file = open('data', 'r') # Open an existing file in READ mode.

    try:

    try:

    stuff(file) # Raises exception

    except MyError:# on exception MyError

    print Error: R/O file!

    finally:

    print Close file anyway

    file.close( ) # Always close file and

    # Continue here, even if no exception.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    PYTHON

    EXTENDING PYTHON

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Extending Python

    Introduction

    This chapter introduce Python's tools for interfacing to the outside world and discuss its interfaces for extending Python scripts with new modules and types implemented in C-compatible languages. Our focus in this chapter is on tight integration where control is transferred between languages by a simple, direct, and fast in-process function call. Extending Python has few main reasons:

    Python code cannot normally access devices at absolute memory addresses, for instance, but can call C functions that do. Extending Python allows programs to do things not directly supported by the language.

    For example, the NumPy package for Python is largely an instance of extending at work: by integrating optimized numeric libraries, it turns Python into a flexible and efficient system that some compare to Matlab.

    speeds up Python routines with C/C++ replacements.

    adds new data types to Python

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Extending Python

    C Extensions Overview

    Regardless of the implementation language, the compiled Python extensions language can take two forms:

    C modules

    These look and feel to their clients like Python module files.

    C types

    These behave like standard built-in types (numbers, lists, and so on).

    To make the interface work, both C modules and C types must provide a layer of code that translates calls and data between those two languages. This layer registers C-coded operations with the Python interpreter as C function pointers. In all cases, the C layer is responsible for converting arguments passed from Python to C form and for converting results from C to Python form. Python scripts simply import C extensions and use them as though they were really coded in Python. Once coded, C modules and C types may be linked to Python either statically (by rebuilding Python) or dynamically (when first imported). Thereafter, the C extension becomes another toolkit available for use in Python scripts.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Extending Python

    A Simple C Extension Module

    #include

    #include

    /* module functions */

    static PyObject * /* returns object */

    message(PyObject *self, PyObject *args) /* self unused in modules */

    { /* args from Python call */

    char *fromPython, result[64];

    if (! PyArg_Parse(args, "(s)", &fromPython)) /* convert Python -> C */

    return NULL; /* null=raise exception */

    else {

    strcpy(result, "Hello, "); /* build up C string */

    strcat(result, fromPython); /* add passed Python string*/

    return Py_BuildValue("s", result); /* convert C -> Python */

    }

    }

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Extending Python

    /* registration table */

    static struct PyMethodDef hello_methods[] = {

    {"message", message, 1}, /* method name, C func ptr, always-tuple */

    {NULL, NULL} /* end of table marker */

    }

    /* module initializer */

    __declspec(dllexport) void inithello( ) /* called on first import */

    { /* name matters if loaded dynamically */

    (void) Py_InitModule("hello", hello_methods); /* mod name, table ptr */

    }

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Extending Python

    Ultimately, Python code will call this C file's message function, passing in a string object and getting back a new string object. First, though, it has to be somehow linked into the Python interpreter. To use this C file in a Python script:

    Add the path of the C module to the Python search path module:

    >>> import sys

    >>> sys.path.append(path\\of\\library)

    Then, to call the C function from a Python program, simply import the module hello and call its hello.message function with a string; you'll get back a normal Python string:

    >>> import hello # import a C module

    >>> hello.message('world') # call a C function

    'Hello, world'

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Extending Python

    Anatomy of a C extension module:

    Python header files: The C file first includes the standard Python.h header file (from the installed Python Include directory). This file defines almost every name exported by the Python API to C, and it serves as a starting point for exploring the API itself.

    Method functions: The file then defines a function to be called from the Python interpreter in response to calls in Python programs. C functions receive two Python objects as input, and send either a Python object back to the interpreter as the result or a NULL to trigger an exception in the script. In C, a PyObject* represents a generic Python object pointer. C module functions can be declared C static (local to the file) because Python calls them by pointer, not by name.

    Registration table: Near the end, the file provides an initialized table (array) that maps function names to function pointers (addresses). This table "registers" attributes of the module. A NULL entry terminates the table.

    Initialization function: Finally, the C file provides an initialization function, which Python calls the first time this module is imported into a Python program. This function calls the API function Py_InitModule to build up the new module's attribute dictionary from the entries in the registration table and create an entry for the C module on the sys.modules table. Once so initialized, calls from Python are routed directly to the C function through the registration table's function pointers. The initialization function name must be a C global and should generally be of the form initX, where X is both the name of the module in Python import statements and the name passed to Py_InitModule.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Extending Python

    A simple DLL in C++for Python

    Instead of writing all that C code in the prior section, write the C function you want to use from Python without any Python integration logic at all, as though it is to be used from C alone.Create a new Dynamic-Link Library project and include the file, compile, and create the DLL.

    #define DLLEXPORT extern "C" __declspec(dllexport)

    DLLEXPORT int sum(int a, int b) {

    return a + b;

    }

    This example represents a simple C library file, with a single function, sum", which is to be made available for use in Python programs. There is nothing about Python here--this C function can be called from a C program, as well as Python.

    The extern "C" construct, tells the compiler that the function is a C function. __declspec(dllexport) adds the export directive to the object file.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Extending Python

    Use the ctypes module to access the DLL:

    >>> import ctypes

    >>> mydll = ctypes.cdll.LoadLibrary("path\\of\\dll")

    >>> mydll.sum(5, 7)

    12

    The ctypes system is a foreign function interface (FFI) module for Python. It allows Python scripts to access and call compiled functions in a binary library file directly and dynamically, by writing dispatch code in Python instead of generating or writing the integration C wrapper code we've studied in this chapter.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    Extending Python

    Exercises

    Create a C extension module for Python which contains a function that returns a string. Then, import this module and call its C function from a Python program.

    Create a C extension module for Python which contains a function with two integer arguments and returns the sum of these integers. Then, import this module and call its C function from a Python program.

    Create a C dll which contains a function called sum and returns the sum of its two integer arguments. Then, import this dll and call its function sum from a Python program.

    Automotive Systems DivisionEngineering Center SibiuBusiness Unit Powertrain & Chassis

    References

    O'Reilly - Learning Python 2nd Edition

    O'Reilly - Programming Python 3rd Edition Aug 2006