Quick Contact

    Python Packages

    A package is a hierarchical levelled document catalogue structure which characterizes the individual python software condition. This package comprises of a module, sub-packages, and sub-sub-packages, etc. Generally, packages are the method of organizing the python module namespace utilizing spotted module names. This python package permits the uniform treatment of records and information.

    For instance, P.Q demonstrates, Package P includes a sub-module Q. In request to regard the python indexes as bundles, __init__.py documents are required. What’s more, there is no limitation for the __init__.py record w.r.t to the quantity of code line. (i.e. it tends to be unfilled records and a document that contains some code). Other than this python translator permits the client to implement the single structure of the bundle.

    Python Regular expressions

    A regular expression is a feature grouping that frames a pursuit design. In an assortment of ways, these articulations grant you to coordinate different string esteems. These are a little exceptionally particular programming language accessible within the python and made accessible by re-module. With this language, you can indicate the standards for the arrangement of potential strings to coordinate. What’s more, this conceivable string might be English sentence, email address and some other Tex orders.

    Also, this module re offers full help for Perl like the ordinary articulation in python. At the gathering time, if a blunder happens, the re-module raises re.error exemption.

    Without summoning the unique meaning, regular expressions utilize the oblique punctuation line (\) to show the unique structures and uncommon significance.

    Regular expression syntax

    It is a collection of strings that connect the necessary expression. Also, this regular expression permits the concatenation to structure new strings. If P and Q are the regular expressions, then PQ is additionally a regular expression.

    Example

    If the string M connects a, and the other string q connects N, thus the string pq will link MN. So, let us have a view over the python regular expression features.

    Regular Expression Characters
    Symbol Meaning
    . It can except newline character connect any feature.
    ^ It connects any string starting.
    $ It connects any string ending.
    * In providing regular expression, it searches zero and higher repetition.
    \ It can either a unique character and a regular expression character and special regular expression series.
    ? It connects one or greater previous regular expressions.
    + In the One or higher occurrences.
    | It is utilized in the cases of Either and
    ( ) Image and gr
    {} It can determine the actual number of series.
    [ ] It is used for the character set.
    […] It can connect any individual line character in brackets.
    [^…] It can connect any individual character but not in brackets.

    Python interpreter also provides the connect expressions.

    Connect expressions
    Pattern Descriptions
    re* It connects for the zero and more occurrences of the previous expression.
    re+ It connects one and more occurrence of the previous expression.
    re? It connects for the one and more occurrence of the previous expression.
    re{n} It connects for the truly ‘n’occurances of the previous expression.
    re{n,} It connects for n and more occurrences of the previous expression.
    re{n,m} It connects for a minimum ‘n’ of the’ occurrences of the previous expression.
    a|b It connects for either a and b
    (re) It classes the normal expressions and gets connects text.

    Like connect expression, it provides a particular series. So, let us have a view over those specific sequences.

    Special Sequences
    Character Description
    \A It restores the connection if the particular characters are the string starts.
    \b It restores the connection where the particular characters are at the starting and end of the text.
    \B It restores a connection where the special characters are present. But not at the starting and end of the text.
    \d It restores the string that includes numbers 0-9.
    \D It restores the string that does not include numeric.
    \s It restores the string that includes the special character.
    \S It restores the string that does not include the particular character.
    \w It restores a connection, where the string includes any words.
    \W It restores a connection, but the string does not include any word characters.
    \Z It restores the connection if the specified characters are the string end.
    What is a Map Function?

    map() used a particular function to each component in an iterable:

    names = [‘Fred’, ‘Wilan’, ‘Lisa’]

    Python 3.x Version ≥ 3.0

    map(len, names) # map in Python 3.x is a class; its instances are iterable

    A Python 3-compatible map is contained in the future_builtins structure:

    Python 2.x Version ≥ 2.6

    from future_builtins import map # include a Python 3.x compatible map()

    map(len, names)

    In Python 2 one can utilize imap from itertools to receive a generator

    Python 2.x Version ≥ 2.3

    map(len, names)

    # map() restore a list

    # Out: [4, 5, 6]

    from itertools import

    imap imap(len, names) # itertools.imap() restore a generator

    The outcome can be specifically modified to a list to delete the differences between Python 2 and 3:

    list(map(len, names))

    map() can be restored by an similar list comprehension or generator expression:

    [len(item) for element in names] # similar to Python 2.x map()

    (len(item) for element in names) # similar to Python 3.x map()

    Mapping each value in an iterable

    For example, we can hold the absolute value of each component:

    list(map(abs, (1, -1, 2, -2, 3, -3))) # the call to `list` is undoubtedly in 2.x

    Output:

    [1, 1, 2, 2, 3, 3]

    Anonymous function additionally provides for mapping a record:

    map(lambda x:x*2, [1, 2, 3, 4, 5])

    Output:

    [2, 4, 6, 8, 10]

    or modifying decimal values to percentages:

    def to_percent(num):

    return num * 100

    list(map(to_percent, [0.95, 0.75, 1.01, 0.1]))

    Output:

    [95.0, 75.0, 101.0, 10.0]

    or modifying dollars to euros (provided an exchange rate):

    from functools import partial

    from operator import mul

    rate = 0.9 # fictitious exchange rate, 1 dollar = 0.9 euros

    dollars = {‘under_my_bed’: 1000,

    ‘jeans’: 45,

    ‘bank’: 5000}

    sum(map(partial(mul, rate), dollars.values()))

    Output:

    5440.5

    functools.partial is an appropriate method to fix arguments of functions so that they can be utilized with map rather thanf using lambda or generating customized functions.

    Mapping values of different iterables

    For example evaluating the average of each i-th component of several iterables:

    def average(*args):

    return float(sum(args)) / len(args) # cast to float – only compulsory for python 2.x measurement1 = [100, 111, 99, 97]

    measurement2 = [102, 117, 91, 102]

    measurement3 = [104, 102, 95, 101]

    list(map(average, measurement1, measurement2, measurement3))

    Output:

    [102.0, 110.0, 95.0, 100.0]

    There are several requirements if higher than one iterable is transfered to map depending on the version of python:

    The function should take as many arguments as there are iterables:

    def median_of_three(a, b, c):

    return sorted((a, b, c))[1]

    list(map(median_of_three, measurement1, measurement2))

    TypeError: median_of_three() missing 1 required positional argument: ‘c’

    list(map(median_of_three, measurement1, measurement2, measurement3, measurement3))

    TypeError: median_of_three() takes 3 positional parameters but 4 were provided

    Python 2.x Version ≥ 2.0.1

    map: The mapping iterates as long as one iterable is still not completely consumed but consider None from the completely consumed iterables:

    import operator

    measurement1 = [100, 111, 99, 97]

    measurement2 = [102, 117]

    # Calculate difference between elements

    list(map(operator.sub, measurement1, measurement2))

    TypeError: unsupported operand type(s) for -: ‘int’ and ‘NoneType’

    itertools.imap and future_builtins.map: The mapping stops as soon as one iterable stops:

    import operator

    from itertools import imap

    measurement1 = [100, 111, 99, 97]

    measurement2 = [102, 117]

    # Calculate difference between elements

    list(imap(operator.sub, measurement1, measurement2))

    Output:

    [-2, -6]

    list(imap(operator.sub, measurement2, measurement1))

    Output:

    [2, 6]

    Python 3.x Version ≥ 3.0.0

    The mapping stops as soon as one iterable stops:

    import operator

    measurement1 = [100, 111, 99, 97]

    measurement2 = [102, 117]

    # Calculate difference between elements

    list(map(operator.sub, measurement1, measurement2))

    Output:

    [-2, -6]

    list(map(operator.sub, measurement2, measurement1))

    Output:

    [2, 6]

    Transposing with Map: Using “None” as function argument (python 2.x only)

    from itertools import imap

    from future_builtins import map as fmap # Different name to highlight differences image = [[1, 2, 3],

    [4, 5, 6],

    [7, 8, 9]]

    list(map(None, *image))

    Output:

    [(1, 4, 7),

    (2, 5, 8),

    (3, 6, 9)]

    list(fmap(None, *image))

    Output:

    [(1, 4, 7),

    (2, 5, 8),

    (3, 6, 9)]

    list(imap(None, *image))

    Output:

    [(1, 4, 7),

    (2, 5, 8),

    (3, 6, 9)]

    image2 = [[1, 2, 3],

    [4, 5],

    [7, 8, 9]]

    list(map(None, *image2))

    Output:

    [(1, 4, 7),

    (2, 5, 8),

    (3, None, 9)]

    # Fill missing values with None list(fmap(None, *image2))

    Output:

    [(1, 4, 7),

    (2, 5, 8)]

    # ignore columns with missing values list(imap(None, *image2))

    Output:

    [(1, 4, 7),

    (2, 5, 8)]

    # dito

    Python 3.x Version ≥ 3.0.0

    list(map(None, *image))

    TypeError: ‘NoneType’ object is not callable

    But there is a workaround to have similar results:

    def conv_to_list(*args):

    return list(args)

    list(map(conv_to_list, *image))

    Output:

    [[1, 4, 7],

    [2, 5, 8],

    [3, 6, 9]]

     

    Enroll Yourself In Live Training: Python Training

    Copyright 1999- Ducat Creative, All rights reserved.