User Tools

Site Tools


script:python

Table of Contents

Python

The Python language has many similarities to Perl, C and Java. However, there are some definite differences between the languages. This chapter is designed to quickly get you up to speed on the syntax that is expected in Python.

Install Python

  • Install ActivePython.
  • on windows, you should install Microsoft Visual C++ Compiler for Python 2.7 at link https://www.microsoft.com/en-us/download/details.aspx?id=44266
    1. Step1: Install setup tools version 6.0 or later:
      python https://bootstrap.pypa.io/ez_setup.py
    2. Step2: Install Microsoft Visual C++ Compiler
    3. Step3: Update pip version
      pip install -U pip

      And check new version:

      pip -V

      output:

      pip 7.1.2 from D:\tools\python27\lib\site-packages (python 2.7)

Convert Python Script to execute file on windows

Install PyInstaller

  1. Step1: Install PyInstaller
    easy_install.exe dis3
    easy_install.exe pyinstaller
    easy_install.exe --upgrade pyinstaller
  2. Step2: Check PyInstaller was installed:
    pyinstaller --version

Create execute file exe to run on windows

Steps to create exe file from python script:

  1. Step1: Create Hello.py
    print "Hello, Python!";
  2. Step2: convert Hello.py to exe file:
    pyinstaller --onefile Hello.py

    or

    pyinstaller --onedir Hello.py

    ⇒ The exe file will startup more fastly

Error can't run exe python file:

Traceback (most recent call last):
  File "site-packages\PyInstaller\loader\rthooks\pyi_rth__tkinter.py", line 30,
in <module>
IOError: Tcl data directory "C:\Users\anh\AppData\Local\Temp\_MEI83~1\tcl" not found.
[6720] Failed to execute script pyi_rth__tkinter

Fix

easy_install --upgrade pyinstaller
pyinstaller --add-data d:\tools\python27\tcl\tcl8.5;tcl\tcl8.5  --add-data d:\tools\python27\tcl\tk8.5;tk\tk8.5 --onefile tktonkho.py

Package Manager for Python

Pip

Install Pip

  1. Step1: Install
    wget https://bootstrap.pypa.io/get-pip.py
    python2.7 get-pip.py
  2. Step2: Update pip
    python -m pip install --upgrade pip

Using pip

  • Show all available commands which pip support
    pip

    output

    Usage:
      pip <command> [options]
    
    Commands:
      install                     Install packages.
      uninstall                   Uninstall packages.
      freeze                      Output installed packages in requirements format.
      list                        List installed packages.
      show                        Show information about installed packages.
      search                      Search PyPI for packages.
      wheel                       Build wheels from your requirements.
      zip                         DEPRECATED. Zip individual packages.
      unzip                       DEPRECATED. Unzip individual packages.
      bundle                      DEPRECATED. Create pybundles.
      help                        Show help for commands.
    
    General Options:
      -h, --help                  Show help.
      -v, --verbose               Give more output. Option is additive, and can be
                                  used up to 3 times.
      -V, --version               Show version and exit.
      -q, --quiet                 Give less output.
      --log-file <path>           Path to a verbose non-appending log, that only
                                  logs failures. This log is active by default at
                                  C:\Users\user1\pip\pip.log.
      --log <path>                Path to a verbose appending log. This log is
                                  inactive by default.
      --proxy <proxy>             Specify a proxy in the form
                                  [user:passwd@]proxy.server:port.
      --timeout <sec>             Set the socket timeout (default 15 seconds).
      --exists-action <action>    Default action when a path already exists:
                                  (s)witch, (i)gnore, (w)ipe, (b)ackup.
      --cert <path>               Path to alternate CA bundle.
  • search all packages which contain the keyword “search”:
    pip search search

    output

    ...........................
    Fozzy                     - Full-Text search REST Service
    easyshop.search           - Search replacement for EasyShop
    sphinxapi                 - Client for the Sphinx search engine.
    cubicweb-searchui         - set of ui components to ease data browsing
    deepsearch                - A Haystack extension used to index deep and nested
                                model relationships.
    ...........................                            

conda

refer: http://conda.pydata.org/docs/intro.html

Conda is an open source package management system and environment management system for installing multiple versions of software packages and their dependencies and switching easily between them. It works on Linux, OS X and Windows, and was created for Python programs but can package and distribute any software.

If you’ve used pip and virtualenv in the past, you can use conda to perform all of the same operations. Pip is a package manager, and Virtualenv is an environment manager. Conda is both.

TaskConda package and environment manager commandPip package manager commandVirtualenv environment manager command
Install a packageconda install $PACKAGE_NAMEpip install $PACKAGE_NAMEX
Update a packageconda update –name $ENVIRONMENT_NAME $PACKAGE_NAMEpip install –upgrade $PACKAGE_NAMEX
Update package managerconda update condaLinux/OSX: pip install -U pip Win: python -m pip install -U pipX
Uninstall a packageconda remove –name $ENVIRONMENT_NAME $PACKAGE_NAMEpip uninstall $PACKAGE_NAMEX
Create an environmentconda create –name $ENVIRONMENT_NAME pythonXcd $ENV_BASE_DIR; virtualenv $ENVIRONMENT_NAME
Activate an environmentsource activate $ENVIRONMENT_NAMEXsource $ENV_BASE_DIR/$ENVIRONMENT_NAME/bin/activate
Deactivate an environmentsource deactivateXdeactivate
Search available packagesconda search $SEARCH_TERMpip search $SEARCH_TERMX
Install package from specific sourceconda install –channel $URL $PACKAGE_NAMEpip install –index-url $URL $PACKAGE_NAMEX
List installed packagesconda list –name $ENVIRONMENT_NAMEpip listX
Create requirements fileconda list –exportpip freezeX
List all environmentsconda info –envsXInstall virtualenv wrapper, then lsvirtualenv
Install other package managerconda install pippip install condaX
Install Pythonconda install python=x.xXX
Update Pythonconda update python *XX

conda update python updates to the most recent in the series, so Python 2 to latest 2.x, Python 3 to latest 3.x, and so on.

Install Conda

Linux

pip install conda

Windows: download and install it from http://conda.pydata.org/miniconda.html(Win32, not 64bits)

After install, change the PATH of python to PATH of conda

Basic Create virtual enviornment with conda

  • Help options in Creating environment:
    conda create -h

    output:

    usage: conda create [-h] [-y] [--dry-run] [-f] [--file FILE] [--unknown]
                        [--no-deps] [-m] [--use-index-cache] [--use-local]
                        [--offline] [--no-pin] [-c CHANNEL] [--override-channels]
                        [-n ENVIRONMENT | -p PATH] [-q] [--copy] [--alt-hint]
                        [--json] [--clone ENV] [--no-default-packages]
                        [package_spec [package_spec ...]]
    
    Create a new conda environment from a list of specified packages. To use the created environment, use 'source activate envname' look in that directory first.  This command requires either the -n NAME or -p PREFIX option.
    
    Options:
    
    positional arguments:
      package_spec          Package versions to install into the conda
                            environment.
    
    optional arguments:
      -h, --help            Show this help message and exit.
      -y, --yes             Do not ask for confirmation.
      --dry-run             Only display what would have been done.
      -f, --force           Force install (even when package already installed),
                            implies --no-deps.
      --file FILE           Read package versions from FILE.
      --unknown             Use index metadata from the local package cache, which
                            are from unknown channels (installing local packages
                            directly implies this option).
      --no-deps             Do not install dependencies.
      -m, --mkdir           Create the environment directory if necessary.
      --use-index-cache     Use cache of channel index files.
      --use-local           Use locally built packages.
      --offline             Offline mode, don't connect to the Internet.
      --no-pin              Ignore pinned file.
      -c CHANNEL, --channel CHANNEL
                            Additional channel to search for packages. These are
                            URLs searched in the order they are given (including
                            file:// for local directories). Then, the defaults or
                            channels from .condarc are searched (unless
                            --override-channels is given). You can use 'defaults'
                            to get the default packages for conda, and 'system' to
                            get the system packages, which also takes .condarc
                            into account. You can also use any name and the
                            .condarc channel_alias value will be prepended. The
                            default channel_alias is http://conda.anaconda.org/.
      --override-channels   Do not search default or .condarc channels. Requires
                            --channel.
      -n ENVIRONMENT, --name ENVIRONMENT
                            Name of environment (in /usr/local/envs).
      -p PATH, --prefix PATH
                            Full path to environment prefix (default:
                            /usr/local/envs/zipline).
      -q, --quiet           Do not display progress bar.
      --copy                Install all packages using copies instead of hard- or
                            soft-linking.
      --alt-hint            Use an alternate algorithm to generate an
                            unsatisfiability hint.
      --json                Report all output as json. Suitable for using conda
                            programmatically.
      --clone ENV           Path to (or name of) existing local environment.
      --no-default-packages
                            Ignore create_default_packages in the .condarc file.
  • Examples:
    conda create -n myenv sqlite
  • Create new virtual environment with name py3k which contain package anaconda with python3x:
    conda create -n py3k anaconda python=3

Create virtual environment for zipline app and using it

We will install zipline which requirement package numpy (Because we can found numpy in packages of conda on https://anaconda.org/anaconda/repo):

  1. Step1: Create new virtual environment with name zipline which contain package numpy in conda
    conda create -n zipline numpy

    ⇒ (option -n → –name of environment)You click yes to install these packages for new virtual environment. You can check new environment created in directory

    D:\tools\miniconda2\envs\
  2. Step2: Go to virtual environment zipline:
    source activate zipline

    in Windows, go to directory d:\tools\Miniconda2\condabin\ and run command below

    conda.bat activate zipline
  3. Step3: Install zipline in environment:
    conda install -c Quantopian zipline

    ⇒install package zipline from channel Quantopian on https://anaconda.org/anaconda/repo

Other commands of conda

Anaconda

install anaconda:

conda install anaconda

Build python modules from source

  1. Step1: Download source and go to source directory and run
    pip install -r requirements.txt
    python setup.py build
    python setup.py install
  2. Step2: Check modules installed using python -c, for example:
    python -c "from PIL import Image, ImageFont, ImageDraw"

First Python Program

Interative mode programming

Invoking the interpreter without passing a script file as a parameter brings up the following prompt:

$ python
Python 2.4.3 (#1, Nov 11 2010, 13:34:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Type the following text to the right of the Python prompt and press the Enter key:

>>> print "Hello, Python!";

If you are running new version of Python, then you would need to use print statement with parenthesis like

print ("Hello, Python!"); 

. However at Python version 2.4.3, this will produce following result:

Hello, Python!

Script mode programming

Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished. When the script is finished, the interpreter is no longer active.

Let us write a simple Python program in a script. All python files will have extension .py.

  • Create hello.py
    print "Hello, Python!";
  • Run
    python hello.py

    ⇒ result

    Hello, Python!

Let's try another way to execute a Python script.

  • Modified hello.py file:
    #!/usr/bin/python
    print "Hello, Python!";
  • Run:
    $ chmod +x hello.py     # This is to make file executable
    $./hello.py

    output:

    Hello, Python!

Python Command line and environment variables

Python Command line

Python Environment Variables

PYTHONHOME

Change the location of the standard Python libraries. By default, the libraries are searched in prefix/lib/pythonversion and exec_prefix/lib/pythonversion, where prefix and exec_prefix are installation-dependent directories, both defaulting to /usr/local.

When PYTHONHOME is set to a single directory, its value replaces both prefix and exec_prefix. To specify different values for these, set PYTHONHOME to prefix:exec_prefix.

PYTHONPATH

Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.

  • In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles containing pure Python modules (in either source or compiled form). Extension modules cannot be imported from zipfiles.
  • The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.
  • An additional directory will be inserted in the search path in front of PYTHONPATH as described above under Interface options. The search path can be manipulated from within a Python program as the variable sys.path.

Basic Syntax

Python Identifiers

A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $ and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.

Here are following identifier naming convention for Python:

  • Class names start with an uppercase letter and all other identifiers with a lowercase letter.
  • Starting an identifier with a single leading underscore indicates by convention that the identifier is meant to be private.
  • Starting an identifier with two leading underscores indicates a strongly private identifier.
  • If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.

Reserved Words

The following list shows the reserved words in Python. These reserved words may not be used as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.

andexecnot
assertfinallyor
breakforpass
classfromprint
continueglobalraise
defifreturn
delimporttry
elifinwhile
elseiswith
exceptlambdayield

Lines and Indentation

One of the first caveats programmers encounter when learning Python is the fact that there are no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. Both blocks in this example are fine:

if True:
    print "True"
else:
  print "False"

However, the second block in this example will generate an error:

if True:
    print "Answer"
    print "True"
else:
    print "Answer"
  print "False"

Thus, in Python all the continous lines indented with similar number of spaces would form a block. Following is the example having various statement blocks:

Note: Don't try to understand logic or different functions used. Just make sure you undertood various blocks even if they are without braces.

#!/usr/bin/python
 
import sys
 
try:
  # open file stream
  file = open(file_name, "w")
except IOError:
  print "There was an error writing to", file_name
  sys.exit()
print "Enter '", file_finish,
print "' When finished"
while file_text != file_finish:
  file_text = raw_input("Enter text: ")
  if file_text == file_finish:
    # close the file
    file.close
    break
  file.write(file_text)
  file.write("\n")
file.close()
file_name = raw_input("Enter filename: ")
if len(file_name) == 0:
  print "Next time please enter something"
  sys.exit()
try:
  file = open(file_name, "r")
except IOError:
  print "There was an error reading file"
  sys.exit()
file_text = file.read()
file.close()
print file_text

Multi-Line Statements

Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example:

total = item_one + \
        item_two + \
        item_three

Statements contained within the [], {} or () brackets do not need to use the line continuation character. For example:

days = ['Monday', 'Tuesday', 'Wednesday',
        'Thursday', 'Friday']

Quotation in Python

Python accepts single ('), double (“) and triple (''' or ”“”) quotes to denote string literals, as long as the same type of quote starts and ends the string.

The triple quotes can be used to span the string across multiple lines. For example, all the following are legal:

word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""

Comments in Python

A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the physical line end are part of the comment and the Python interpreter ignores them.

#!/usr/bin/python
 
# First comment
print "Hello, Python!";  # second comment

This will produce the following result:

Hello, Python!

A comment may be on the same line after a statement or expression:

name = "Madisetti" # This is again comment

You can comment multiple lines as follows:

# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

Using Blank Lines

A line containing only whitespace, possibly with a comment, is known as a blank line and Python totally ignores it.

In an interactive interpreter session, you must enter an empty physical line to terminate a multiline statement.

Waiting for the User

The following line of the program displays the prompt, Press the enter key to exit and waits for the user to press the Enter key:

#!/usr/bin/python
 
raw_input("\n\nPress the enter key to exit.")

Here, “\n\n” are being used to create two new lines before displaying the actual line. Once the user presses the key, the program ends. This is a nice trick to keep a console window open until the user is done with an application.

Multiple Statements on a Single Line

The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block. Here is a sample snip using the semicolon:

import sys; x = 'foo'; sys.stdout.write(x + '\n')

Multiple Statement Groups as Suites

A group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class, are those which require a header line and a suite.

Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are followed by one or more lines which make up the suite. For example:

if expression : 
   suite
elif expression : 
   suite 
else : 
   suite

Command Line Arguments

You may have seen, for instance, that many programs can be run so that they provide you with some basic information about how they should be run. Python enables you to do this with -h:

python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser (also PYTHONDEBUG=x)
-E     : ignore environment variables (such as PYTHONPATH)
-h     : print this help message and exit
[ etc. ]

You can also program your script in such a way that it should accept various options. Command Line Arguments is an advanced topic and should be studied a bit later once you have gone through rest of the Python concepts.

Python Data Types

Python has five standard data types:

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Assigning Values to Variables

Python variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. For example:

#!/usr/bin/python
counter = 100          # An integer assignment
miles   = 1000.0       # A floating point
name    = "John"       # A string
 
print counter
print miles
print name

Output:

100
1000.0
John

Python Numbers

Python has five standard data types:

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Python Strings

Strings in Python are identified as a contiguous set of characters in between quotation marks. Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

The plus ( +) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator. For example:

#!/usr/bin/python
 
str = 'Hello World!'
 
print str          # Prints complete string
print str[0]       # Prints first character of the string
print str[2:5]     # Prints characters starting from 3rd to 5th
print str[2:]      # Prints string starting from 3rd character
print str * 2      # Prints string two times
print str + "TEST" # Prints concatenated string

This will produce the following result:

Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

Python Lists

refer: http://www.tutorialspoint.com/python/python_lists.htm
A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.

The values stored in a list can be accessed using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator. For example:

#!/usr/bin/python
 
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
 
print list          # Prints complete list
print list[0]       # Prints first element of the list
print list[1:3]     # Prints elements starting from 2nd till 3rd 
print list[2:]      # Prints elements starting from 3rd element
print tinylist * 2  # Prints list two times
print list + tinylist # Prints concatenated lists

This will produce the following result:

['abcd', 786, 2.23, 'john', 70.200000000000003]
abcd
[786, 2.23]
[2.23, 'john', 70.200000000000003]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']

Python Tuples

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and size cannot be updated. Tuples can be thought of as read-only lists. For example:

#!/usr/bin/python
 
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
tinytuple = (123, 'john')
 
print tuple           # Prints complete list
print tuple[0]        # Prints first element of the list
print tuple[1:3]      # Prints elements starting from 2nd till 3rd 
print tuple[2:]       # Prints elements starting from 3rd element
print tinytuple * 2   # Prints list two times
print tuple + tinytuple # Prints concatenated lists

This will produce the following result:

('abcd', 786, 2.23, 'john', 70.200000000000003)
abcd
(786, 2.23)
(2.23, 'john', 70.200000000000003)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')

Following is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar case is possible with lists:

#!/usr/bin/python
 
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
list = [ 'abcd', 786 , 2.23, 'john', 70.2  ]
tuple[2] = 1000    # Invalid syntax with tuple
list[2] = 1000     # Valid syntax with list

Python Dictionary

Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ( [] ).

  • For example:
    #!/usr/bin/python
     
    dict = {}
    dict['one'] = "This is one"
    dict[2]     = "This is two"
     
    tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
     
     
    print dict['one']       # Prints value for 'one' key
    print dict[2]           # Prints value for 2 key
    print tinydict          # Prints complete dictionary
    print tinydict.keys()   # Prints all the keys
    print tinydict.values() # Prints all the values

    This will produce the following result:

    This is one
    This is two
    {'dept': 'sales', 'code': 6734, 'name': 'john'}
    ['dept', 'code', 'name']
    ['sales', 6734, 'john']
  • Dictionaries have no concept of order among elements. It is incorrect to say that the elements are “out of order”; they are simply unordered. Loop in Dictionary:
    mydict = {'dept': 'sales', 'code': 6734, 'name': 'john'}
    for i, v in mydict.iteritems():
       print i, v
  • Update dictionary:
    #!/usr/bin/python
     
    dict = {'Name': 'Zara', 'Age': 7}
    dict2 = {'Sex': 'female' }
     
    dict.update(dict2)
    print "Value : %s" %  dict

    When we run above program, it produces following result

    Value : {'Age': 7, 'Name': 'Zara', 'Sex': 'female'}

Data Type Conversion

FunctionDescription
int(x [,base])Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )Converts x to a long integer. base specifies the base if x is a string.
float(x)Converts x to a floating-point number.
complex(real [,imag])Creates a complex number.
str(x)Converts object x to a string representation.
repr(x)Converts object x to an expression string.
eval(str)Evaluates a string and returns an object.
tuple(s)Converts s to a tuple.
list(s)Converts s to a list.
set(s)Converts s to a set.
dict(d)Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s)Converts s to a frozen set.
chr(x)Converts an integer to a character.
unichr(x)Converts an integer to a Unicode character.
ord(x)Converts a single character to its integer value.
hex(x)Converts an integer to a hexadecimal string.
oct(x)Converts an integer to an octal string.

Python Operators

Arithmetic Operators

Assume variable a holds 10 and variable b holds 20, then:

Operator Description Example
+Addition - Adds values on either side of the operatora + b will give 30
-Subtraction - Subtracts right hand operand from left hand operanda - b will give -10
*Multiplication - Multiplies values on either side of the operatora * b will give 200
/Division - Divides left hand operand by right hand operandb / a will give 2
%Modulus - Divides left hand operand by right hand operand and returns remainderb % a will give 0
**Exponent - Performs exponential (power) calculation on operatorsa**b will give 10 to the power 20
//Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.9//2 is equal to 4 and 9.0//2.0 is equal to 4.0

Compare Operators

Assume variable a holds 10 and variable b holds 20, then:

Operator Description Example
==Checks if the value of two operands are equal or not, if yes then condition becomes true.(a == b) is not true.
!=Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.(a != b) is true.
<>Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.(a <> b) is true. This is similar to != operator.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(a > b) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(a < b) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(a >= b) is not true.
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(a ⇐ b) is true.

Python Assignment Operators

Assume variable a holds 10 and variable b holds 20, then:

Operator Description Example
=Simple assignment operator, Assigns values from right side operands to left side operand c = a + b will assigne value of a + b into c
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / a
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a
**=Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a<nowiki>| |<nowiki>//=<nowiki>|Floor Dividion and assigns a value, Performs floor division on operators and assign value to the left operand|<nowiki> c //= a is equivalent to c = c // a

Python Bitwise Operators

Bitwise operator works on bits and perform bit by bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows:

a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a  = 1100 0011

There are following Bitwise operators supported by Python language

Operator Description Example
&Binary AND Operator copies a bit to the result if it exists in both operands.(a & b) will give 12 which is 0000 1100
|Binary OR Operator copies a bit if it exists in eather operand.(a | b) will give 61 which is 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both.(a ^ b) will give 49 which is 0011 0001
~Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.(~a ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
«Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.a « 2 will give 240 which is 1111 0000
»Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.a » 2 will give 15 which is 0000 1111

Python Logical Operators

There are following logical operators supported by Python language. Assume variable a holds 10 and variable b holds 20 then:

Operator Description Example
andCalled Logical AND operator. If both the operands are true then then condition becomes true.(a and b) is true.
orCalled Logical OR Operator. If any of the two operands are non zero then then condition becomes true.(a or b) is true.
notCalled Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.not(a and b) is false.

Python Membership Operators

In addition to the operators discussed previously, Python has membership operators, which test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators explained below:

Operator Description Example
inEvaluates to true if it finds a variable in the specified sequence and false otherwise. x in y, here in results in a 1 if x is a member of sequence y.
not inEvaluates to true if it does not finds a variable in the specified sequence and false otherwise.x not in y, here not in results in a 1 if x is not a member of sequence y.

Try following example to understand all the membership operators available in Python programming language:

#!/usr/bin/python
 
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
 
if ( a in list ):
   print "Line 1 - a is available in the given list"
else:
   print "Line 1 - a is not available in the given list"
 
if ( b not in list ):
   print "Line 2 - b is not available in the given list"
else:
   print "Line 2 - b is available in the given list"
 
a = 2
if ( a in list ):
   print "Line 3 - a is available in the given list"
else:
   print "Line 3 - a is not available in the given list"

When you execute the above program it produces the following result:

Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list

Python Identity Operators

Identity operators compare the memory locations of two objects. There are two Identity operators explained below:

Operator Description Example
isEvaluates to true if the variables on either side of the operator point to the same object and false otherwise. x is y, here is results in 1 if id(x) equals id(y).
is notEvaluates to false if the variables on either side of the operator point to the same object and true otherwise. x is not y, here is not results in 1 if id(x) is not equal to id(y).

Python Decision Making

Statement Description
if expression:
  statement(s)
An if statement consists of a boolean expression followed by one or more statements.
if expression:
   statement(s)
else:
   statement(s)
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
if expression1:
   statement(s)
   if expression2:
      statement(s)
   elif expression3:
      statement(s)
   else
      statement(s)
elif expression4:
   statement(s)
else:
   statement(s)
You can use one if or else if statement inside another if or else if statement(s).

Python loops

Loop TypeDescription
while expression:
   statement(s)
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for iterating_var in sequence:
   statements(s)
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
nested loops: with for:
for iterating_var in sequence:
   for iterating_var in sequence:
      statements(s)
   statements(s)

with while:

while expression:
   while expression:
      statement(s)
   statement(s)
You can use one or more loop inside any another while, for or do..while loop.

With Loop Control Statements:

Control Statement Description
break statementTerminates the loop statement and transfers execution to the statement immediately following the loop.
continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
pass statementThe pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

Examples:

  • For from 0 to 3
    for x in range(0, 3):
        print "We're on time %d" % (x)
  • Lists as an iterable
    collection = ['hey', 5, 'd']
    for x in collection:
        print x
  • Loop over Lists of lists
    list_of_lists = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
    for list in list_of_lists:
        for x in list:
            print x

Python Functions

Defining a Function

You can define functions to provide the required functionality. Here are simple rules to define a function in Python.

  • Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
  • Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
  • The first statement of a function can be an optional statement - the documentation string of the function or docstring.
  • The code block within every function starts with a colon (:) and is indented.
  • The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Syntax and examples

  • syntax:
    def functionname( parameters ):
       "function_docstring"
       function_suite
       return [expression]
  • simple example:
    def printme( str ):
       "This prints a passed string into this function"
       print str
       return
  • calling a function:
    #!/usr/bin/python
     
    # Function definition is here
    def printme( str ):
       "This prints a passed string into this function"
       print str;
       return;
     
    # Now you can call printme function
    printme("I'm first call to user defined function!");
    printme("Again second call to the same function");

    Output:

    I'm first call to user defined function!
    Again second call to the same function
  • Return value in function:
    #!/usr/bin/python
    # Function definition is here
    def sum( arg1, arg2 ):
       # Add both the parameters and return them."
       total = arg1 + arg2
       print "Inside the function : ", total
       return total;
     
    # Now you can call sum function
    total = sum( 10, 20 );
    print "Outside the function : ", total 

    Output:

    Inside the function :  30
    Outside the function :  30

Pass by reference vs value

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example:

  • Example1:
    #!/usr/bin/python
     
    # Function definition is here
    def changeme( mylist ):
       "This changes a passed list into this function"
       mylist.append([1,2,3,4]);
       print "Values inside the function: ", mylist
       return
     
    # Now you can call changeme function
    mylist = [10,20,30];
    changeme( mylist );
    print "Values outside the function: ", mylist

    output:

    Values inside the function:  [10, 20, 30, [1, 2, 3, 4]]
    Values outside the function:  [10, 20, 30, [1, 2, 3, 4]]
  • Example2:
    #!/usr/bin/python
     
    # Function definition is here
    def changeme(text):
       "This changes a passed list into this function"
       text = text + 'world'
       return
     
    # Now you can call changeme function
    str = 'hello'
    changeme(str);
    print str

    output:

    hello

Global vs. Local variables

#!/usr/bin/python
 
total = 0; # This is global variable.
# Function definition is here
def sumlocal( arg1, arg2 ):
   # Add both the parameters and return them."
   total = arg1 + arg2; # Here total is local variable.
   print "Inside the function local total : ", total
   return total;
def sumglobal( arg1, arg2 ):
   # Add both the parameters and return them."
   global total
   total = arg1 + arg2; # Here total is local variable.
   return total; 
# Now you can call sum function
sumlocal( 10, 20 );
print "Outside total after run sumlocal : ", total
sumglobal( 10, 20 );
print "Outside total after run sumglobal : ", total

Output:

Inside the function local total :  30
Outside total after run sumlocal :  0
Outside total after run sumglobal :  30

Built-in Functions

Define Function inside Function

Define functions inside other functions:

def greet(name):
    def get_message():
        return "Hello "
 
    result = get_message()+name
    return result
 
print greet("John")
 
# Outputs: Hello John

Functions can be passed as parameters to other functions

def greet(name):
   return "Hello " + name 
 
def call_func(func):
    other_name = "John"
    return func(other_name)  
 
print call_func(greet)
 
# Outputs: Hello John

Functions can return other functions

In other words, functions generating other functions.

def compose_greet_func():
    def get_message():
        return "Hello there!"
 
    return get_message
 
greet = compose_greet_func()
print greet()
 
# Outputs: Hello there!

Python Modules

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.

Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.

Create simple module

Create module support.py

def print_func( par ):
   print "Hello : ", par
   return

import and using module

Basic syntax:

from file1 import *

⇒ import all objects and methods in file1

  • Simple import:
    #!/usr/bin/python
     
    # Import module support
    import support
     
    # Now you can call defined function that module as follows
    support.print_func("Zara")
  • import with from:
    #!/usr/bin/python
     
    # Import module support
    from support import print_func
     
    # Now you can call defined function that module as follows
    print_func("Zara")

    Or

    #!/usr/bin/python
     
    # Import module support
    from support import *
     
    # Now you can call defined function that module as follows
    print_func("Zara")

Excuting modules as script

To run modules as script, you add below code at the end your module for running functions in module:

if __name__ == "__main__":
    import sys
    print_func(sys.argv[1])

⇒ This code only run when your module is main. That mean It only implement when you run module as script Therefore simple module support.py will update to:

def print_func( par ):
   print "Hello : ", par
   return
if __name__ == "__main__":
    import sys
    print_func(sys.argv[1])

And you can test the module with command below:

python support.py "zara"

Output:

Hello :  zara

Packages in Python

Packages are a way of structuring Python’s module namespace by using “dotted module names”.

  • Below example design a collection of modules (a “package”) for the uniform handling of sound files and sound data:
    sound/                          Top-level package
          __init__.py               Initialize the sound package
          formats/                  Subpackage for file format conversions
                  __init__.py
                  wavread.py
                  wavwrite.py
                  aiffread.py
                  aiffwrite.py
                  auread.py
                  auwrite.py
                  ...
          effects/                  Subpackage for sound effects
                  __init__.py
                  echo.py
                  surround.py
                  reverse.py
                  ...
          filters/                  Subpackage for filters
                  __init__.py
                  equalizer.py
                  vocoder.py
                  karaoke.py
                  ...

    The __init__.py files are required to make Python treat the directories as containing packages. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. For example, the file sound/effects/__init__.py could contain the following code:

    __all__ = ["echo", "surround", "reverse"]

    ⇒ This would mean that from sound.effects import * would import the three named submodules of the sound package.

  • Using package:
    import sound.effects.echo
    sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)

    Or

    from sound.effects import echo
    echo.echofilter(input, output, delay=0.7, atten=4)

Python Exceptions

Used to replace “Try Catch” with “Try Except”

Syntax

try:
   You do your operations here;
   ......................
except ExceptionI:
   If there is ExceptionI, then execute this block.
except ExceptionII:
   If there is ExceptionII, then execute this block.
   ......................
else:
   If there is no exception then execute this block. 

Simple code:

#!/usr/bin/python
 
try:
   fh = open("testfile", "w")
   fh.write("This is my test file for exception handling!!")
except IOError:
   print "Error: can\'t find file or read data"
else:
   print "Written content in the file successfully"
   fh.close()

The try-except clause

  • The except clause with no exceptions
    try:
       You do your operations here;
       ......................
    except:
       If there is any exception, then execute this block.
       ......................
    else:
       If there is no exception then execute this block. 
    try:
       You do your operations here;
       ......................
    except:
       If there is any exception, then execute this block.
       ......................
  • The except clause with multiple exceptions
    try:
       You do your operations here;
       ......................
    except(Exception1[, Exception2[,...ExceptionN]]]):
       If there is any exception from the given exception list, 
       then execute this block.
       ......................
    else:
       If there is no exception then execute this block. 

The try-finally clause

  • Syntax:
    try:
       You do your operations here;
       ......................
       Due to any exception, this may be skipped.
    finally:
       This would always be executed.
       ......................

    Note that you can provide except clause(s), or a finally clause, but not both. You can not use else clause as well along with a finally clause.

  • Example:
    #!/usr/bin/python
     
    try:
       fh = open("testfile", "w")
       try:
          fh.write("This is my test file for exception handling!!")
       finally:
          print "Going to close the file"
          fh.close()
    except IOError:
       print "Error: can\'t find file or read data"

    And to display error detail, you add command raise:

    except IOError:
       print "Error: can\'t find file or read data"
       raise

Python with Statement

With the “With” statement, you get better syntax and exceptions handling:

  • The with statement simplifies exception handling by encapsulating common preparation and cleanup tasks.
  • In addition, it will automatically close the file. The with statement provides a way for ensuring that a clean-up is always used.

Without with statement, we use:

import sys
 
try:
  # open file stream
  file = open("welcome.txt")
  data = file.read()
  print data
  file.close()
  # It's important to close the file when you're done with it  
except IOError:
  print "There was an error reading welcome.txt"
  sys.exit()

With Statement Usage

  • Opening a file using with is as simple as: with open(filename) as file:
    import sys
     
    with open("welcome.txt") as file: # Use file to refer to the file object
       data = file.read()
       do something with data
  • Opens output.txt in write mode
    import sys
     
    with open('output.txt', 'w') as file:  # Use file to refer to the file object
       file.write('Hi there!')

Notice, that we didn't have to write “file.close()”. That will automatically be called.

Python Classes/Objects

Creating Classes, Instant Objects and Using

  • Syntax:
    class ClassName:
        <statement-1>
        .
        .
        .
        <statement-N>
  • Simple Python Class:
    class Employee:
       'Common base class for all employees'
       empCount = 0
     
       def __init__(self, name, salary):
          self.name = name
          self.salary = salary
          Employee.empCount += 1
     
       def displayCount(self):
         print "Total Employee %d" % Employee.empCount
     
       def displayEmployee(self):
          print "Name : ", self.name,  ", Salary: ", self.salary
  • Instant object and using:
    #This would create first object of Employee class
    emp1 = Employee("Zara", 2000)
    #This would create second object of Employee class
    emp2 = Employee("Manni", 5000)
    emp1.displayEmployee()
    emp2.displayEmployee()
    print "Total Employee %d" % Employee.empCount

Class Inheritance and Overriding methods

  • Syntax:
    class SubClassName (ParentClass1[, ParentClass2, ...]):
        <statement-1>
        .
        .
        .
        <statement-N>
  • Example for Class Inheritance:
    #!/usr/bin/python
     
    class Parent:        # define parent class
       parentAttr = 100
       def __init__(self):
          print "Calling parent constructor"
     
       def parentMethod(self):
          print 'Calling parent method'
     
       def setAttr(self, attr):
          Parent.parentAttr = attr
     
       def getAttr(self):
          print "Parent attribute :", Parent.parentAttr
     
    class Child(Parent): # define child class
       def __init__(self):
          print "Calling child constructor"
     
       def childMethod(self):
          print 'Calling child method'
     
    c = Child()          # instance of child
    c.childMethod()      # child calls its method
    c.parentMethod()     # calls parent's method
    c.setAttr(200)       # again call parent's method
    c.getAttr()          # again call parent's method

    Output:

    Calling child constructor
    Calling child method
    Calling parent method
    Parent attribute : 200
  • Example for Overriding methods:
    #!/usr/bin/python
     
    class Parent:        # define parent class
       def myMethod(self):
          print 'Calling parent method'
     
    class Child(Parent): # define child class
       def myMethod(self):
          print 'Calling child method'
     
    c = Child()          # instance of child
    c.myMethod()         # child calls overridden method

    Output:

    Calling child method

Private and public variables

Default variable in python are public. For private variable, you can name attributes with a double underscore prefix, and those attributes will not be directly visible to outsiders Example:

#!/usr/bin/python
 
class JustCounter:
   __secretCount = 0
 
   def count(self):
      self.__secretCount += 1
      print self.__secretCount
 
counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount

Output:

1
2
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    print counter.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'

Python static variable

refer: http://radek.io/2011/07/21/static-variables-and-methods-in-python/

All variables defined on the class level in Python are considered static. See this example:

class Example:
    staticVariable = 5 # Access through class
 
print Example.staticVariable # prints 5
 
# Access through an instance
instance = Example()
print instance.staticVariable # still 5
 
# Change within an instance
instance.staticVariable = 6
print instance.staticVariable # 6
print Example.staticVariable # 5
 
# Change through
class Example.staticVariable = 7
print instance.staticVariable # still 6
print Example.staticVariable # now 7

If variable:

  • used by class → static variable
  • And used by object → property variable of class(public or private variable)

Python static functions

With static methods it gets a little more complex. In Python, there are two ways of defining static methods within a class.

  • @staticmethod: Method decorated with this decorator shares with the class only the namespace. Note that, no arguments are mandatory in the method definition. Static method can access classes static variables. See in the following example:
    class Example:
        name = "Example"
     
        @staticmethod
        def static():
            print "%s static() called" % Example.name
     
    class Offspring1(Example):
        name = "Offspring1"
     
    class Offspring2(Example):
        name = "Offspring2"
     
        @staticmethod
        def static():
            print "%s static() called" % Offspring2.name
     
    Example.static() # prints Example
    Offspring1.static() # prints Example
    Offspring2.static() # prints Offspring2
  • @classmethod:The difference between class method and static method in Python is, that class method recieves one mandatory argument – a class name it was called from. Let’s have a look:
    class Example:
        name = "Example"
     
        @classmethod
        def static(cls):
            print "%s static() called" % cls.name
     
    class Offspring1(Example):
        name = "Offspring1"
        pass
     
    class Offspring2(Example):
        name = "Offspring2"
     
        @classmethod
        def static(cls):
            print "%s static() called" % cls.name
     
    Example.static()    # prints Example
    Offspring1.static() # prints Offspring1
    Offspring2.static() # prints Offspring2

Python Interface and Multiple Inheritance

Interfaces is not necessary in Python. This is because Python has proper multiple inheritance, and also ducktyping, which means that the places where you must have interfaces in Java, you don't have to have them in Python.For example:

class SomeAbstraction( object ):
    pass # lots of stuff - but missing something
 
class Mixin1( object ):
    def something( self ):
        pass # one implementation
 
class Mixin2( object ):
    def something( self ):
        pass # another
 
class Concrete1( SomeAbstraction, Mixin1 ):
    pass
 
class Concrete2( SomeAbstraction, Mixin2 ):
    pass

Decorators for Functions and Methods

A decorator is the name used for a software design pattern. Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated.
refer: http://thecodeship.com/patterns/guide-to-python-function-decorators/
https://wiki.python.org/moin/PythonDecoratorLibrary

Basic Syntax

The current syntax for function decorators as implemented in Python 2.4a2 is:

@dec2
@dec1
def func(arg1, arg2, ...):
    pass

⇒ function func will automatically call func = dec2(dec1(func)) after declare This is equivalent to:

def func(arg1, arg2, ...):
    pass
func = dec2(dec1(func))

Composition of Decorators

refer: Python Functions
Function decorators are simply wrappers to existing functions. Putting the ideas mentioned above together, we can build a decorator. In this example let's consider a function that wraps the string output of another function by p tags.

def get_text(name):
   return "lorem ipsum, {0} dolor sit amet".format(name)
 
def p_decorate(func):
   def func_wrapper(name):
       return "<p>{0}</p>".format(func(name))
   return func_wrapper
 
my_get_text = p_decorate(get_text)
 
print my_get_text("John")
 
# <p>Outputs lorem ipsum, John dolor sit amet</p>

⇒ my_get_text = p_decorate(get_text) generate a new function with name func_wrapper

   def func_wrapper(name):
       return "<p>{0}</p>".format(get_text(name))

Example with Python's Decorator Syntax

Python makes creating and using decorators a bit cleaner and nicer for the programmer through some syntactic sugar To decorate get_text we don't have to get_text = p_decorator(get_text) There is a neat shortcut for that, which is to mention the name of the decorating function before the function to be decorated. The name of the decorator should be perpended with an @ symbol.

def p_decorate(func):
   def func_wrapper(name):
       return "<p>{0}</p>".format(func(name))
   return func_wrapper
 
@p_decorate
def get_text(name):
   return "lorem ipsum, {0} dolor sit amet".format(name)
 
print get_text("John")
 
# Outputs <p>lorem ipsum, John dolor sit amet</p>

Generators functions and Yield

Generators functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop. Refer:

Simple code generate iterator

  • simple example of building a list and returning it
    # Build and return a list
    def firstn(n):
        num, nums = 0, []
        while num < n:
            nums.append(num)
            num += 1
        return nums
     
    sum_of_first_n = sum(firstn(1000000))

    ⇒ The code is quite simple and straightforward, but its builds the full list in memory

  • The following implements generator as an iterable object(using iterator pattern). The Generator don't store the list in memory
    # Using the generator pattern (an iterable)
    class firstn(object):
        def __init__(self, n):
            self.n = n
            self.num, self.nums = 0, []
     
        def __iter__(self):
            return self
     
        # Python 3 compatibility
        def __next__(self):
            return self.next()
     
        def next(self):
            if self.num < self.n:
                cur, self.num = self.num, self.num+1
                return cur
            else:
                raise StopIteration()
     
    sum_of_first_n = sum(firstn(1000000))

Generate iterator with python syntax

Python provides generator functions as a convenient shortcut to building iterators. Lets us rewrite the above iterator(simple example) as a generator function:

  • Generate iterator with yield
    # a generator that yields items instead of returning a list
    def firstn(n):
        num = 0
        while num < n:
            yield num
            num += 1
     
    sum_of_first_n = sum(firstn(1000000))
  • Generate iterator with generator expression by replacing the square brackets (“[ ]”) with parentheses Or list function:
    # list comprehension
    doubles = [2 * n for n in range(50)]
     
    # same as the list comprehension above
    doubles = list(2 * n for n in range(50))
  • Generate iterator with xrange:
    sum_of_first_n = sum(xrange(1000000))
  • Generators can be composed. Here we create a generator on the squares of consecutive integers.
    Toggle line numbers
    #square is a generator
    square = (i*i for i in irange(1000000))
    #add the squares
    total = 0
    for i in square:
       total += i
  • Here, we compose a square generator with the takewhile generator, to generate squares less than 100
    Toggle line numbers
    #add squares less than 100
    square = (i*i for i in count())
    bounded_squares = takewhile(lambda x : x< 100, square)
    total = 0
    for i in bounded_squares:
       total += i

Python Extending and Embedding

Python's C API

Build Simple C API module:

  • Create Hellomodule.c
    #include <Python.h>
     
    static PyObject*
    say_hello(PyObject* self, PyObject* args)
    {
        const char* name;
     
        if (!PyArg_ParseTuple(args, "s", &name))
            return NULL;
     
        printf("Hello %s!\n", name);
     
        Py_RETURN_NONE;
    }
     
    static PyMethodDef HelloMethods[] =
    {
         {"say_hello", say_hello, METH_VARARGS, "Greet somebody."},
         {NULL, NULL, 0, NULL}
    };
     
    PyMODINIT_FUNC
    inithello(void)
    {
         (void) Py_InitModule("hello", HelloMethods);
    }
  • Create setup.py for building
    from distutils.core import setup, Extension
     
    module1 = Extension('hello', sources = ['hellomodule.c'])
     
    setup (name = 'PackageName',
            version = '1.0',
            description = 'This is a demo package',
            ext_modules = [module1])
  • Build
    python setup.py build
  • Building the extension module using Microsoft Visual C++
    cl /LD hellomodule.c /Ic:\Python24\include c:\Python24\libs\python24.lib /link/out:hello.dll
  • Using the extension module
    import hello
    hello.say_hello("World")

    ⇒ output:

    Hello World!

Cython

refer: http://en.wikipedia.org/wiki/Cython
Cython is a compiled language that generates CPython extension modules. These extension modules can then be loaded and used by regular Python code using the import statement. It works by producing a standard Python module. The difference from standard Python behavior however, is that the original code of the module is actually written in Python but is then translated into C

Cython has an unusually involved hello world program because it interfaces with the Python C API and the distutils extension building facility. At least three files are required for a basic project:

  • A setup.py file to invoke the distutils build process that generates the extension module
  • A main python program to load the extension module
  • Cython source file(s)

The following code listings demonstrate the build and launch process:

  • # hello.pyx - Python Module, this code will be translated to C by Cython
    def say_hello():
        print "Hello World!"
  • # launch.py - Python stub loader, loads the module that was made by Cython
    # This code is always interpreted, like normal Python.
    # It is not compiled to C.
    import hello
    hello.say_hello()
  • # setup.py - unnecessary if not redistributing the code, see below
    from distutils.core import setup
    from Cython.Build import cythonize
     
    setup(name = 'Hello world app',
          ext_modules = cythonize("*.pyx"))
  • These commands build and launch the program:
    $ python setup.py build_ext --inplace
    $ python launch.py

Python Unit Test

Below are steps to write unittest:

  1. You define your own class derived from unittest Testcase
  2. Write your test functions start with test_
  3. You run the tests by placing the code unittest.main() in your file, usually at the bottom of file

Below are example code:

  • Example code:
    import unittest
    class SimpleTest(unittest.TestCase):
     
        def setUp(self):
            pass
     
        def test_numbers_3_4(self):
            self.assertEqual(3*4, 12)
     
        def test_strings_aa_2(self):
            self.assertEqual( 'aa' * 2, 'aaa')
     
    if __name__ == '__main__':
        unittest.main()
  • Run:
    python simpletest.py
  • Output:
    .F
    ======================================================================
    FAIL: test_strings_aa_2 (__main__.SimpleTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "simpletest.py", line 11, in test_strings_aa_2
        self.assertEqual( 'aa' * 2, 'aaa')
    AssertionError: 'aaaa' != 'aaa'
    
    ----------------------------------------------------------------------
    Ran 2 tests in 0.001s
    
    FAILED (failures=1)
script/python.txt · Last modified: 2023/11/28 09:35 by admin