Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Profound Python Libraries
Profound Python Libraries
Profound Python Libraries
Ebook315 pages3 hours

Profound Python Libraries

Rating: 0 out of 5 stars

()

Read preview

About this ebook

The book contains Python libraries used in many applications. Internet, Downloads, JSON, REST are covered. Utilities such as time, random, regular expressions are included. The operating systems & process are explained in detail. File system operations and Pathlib are covered. Some introductions to Big Data & Artificial Intelligence are added. CSV, Samples are explained as a preperation for data science. Visual libraries such as PIL & Matplotlib are included. Speech Recognition is covered. Finally Tk is is explained & a full sample application is supplied. Önder Teker, the author of the book, develops projects since the end of the 1990s, gives courses and lectures since the beginning of the 2000s, and produces printed and electronic books and visual courses since the beginning of the 2010s.

LanguageEnglish
PublisherGodoro
Release dateJul 8, 2022
ISBN9786057172501
Profound Python Libraries

Read more from Onder Teker

Related to Profound Python Libraries

Related ebooks

Programming For You

View More

Related articles

Reviews for Profound Python Libraries

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Profound Python Libraries - Onder Teker

    Profound

    Python

    Libraries

    Önder Teker

    Godoro Yayıncılık

    GODORO PUBLISHING

    Publisher Certificate No: 40946

    The Name Of The Book:

    Profound Python Libraries

    Copyright © 2022 Godoro Publishing

    The Author Of The Book:

    Önder Teker

    First Edition, July 2022, Istanbul

    ISBN:

    978-605-71725-0-1

    Cover Design & Makeup:

    Önder Teker

    Printing & Binding:

    Godoro

    Special Printing Solutions

    Atikali Mah. Fatih Cad. No: 81 D: 2

    Fatih / İstanbul

    Telefon : (533) 561-2435

    http://www.godoro.com

    GODORO PUBLISHING

    Atikali Mah. Fatih Cad. No: 81 D: 2

    Fatih / Istanbul

    Phone : (533) 561-2435

    http://www.godoro.com

    Some Important Libraries

    There are lots of modules for the Python language. Some of them need to be installed. Some are built-in. The modules coming together with Python are huge. Every developer must use them when it is necessary. However, there may be better alternatives. But still, the better ones work in a similar way to the basic ones. Some basic libraries must be learned. In this section, a few modules which are used very frequently are explained. The main goal is to introduce how to use a library.

    Time

    The word time has some ambiguity in programming languages. In some cases it means anything related to time and in some others it means time in a day. In some languages, time is called datetime. It means date and / or time. The confusion comes from computers. They don't care about how human beings are divided time into units which are based on whether the earth rotates around the sun or moon around the earth. A clock shows time as a number. The rest is converted to units according to human beings.

    There is a module named time. It contains several functions and other things related to time such as dates. The module is used by import keyword as below:

    import time

    Ticks

    Computers have a device named clock. It keeps time for computer operations. Clocks of most modern computers hold numbers passed since the beginning of the year 1970. This date is called the epoch since it is accepted as the beginning of personal computers. The seconds or milliseconds passed since the epoch time are sometimes called ticks. It may be an integer number. However, computers can keep time in more precision than milliseconds. They can keep nanoseconds. So, ticks value may be a large float or double number.

    The code below prints the time passed after 1970 as milliseconds:

    ticks = time.time()

    print('Ticks: ', ticks )

    The result may be a number like this:

    Ticks:  1620140012.30621

    Years

    Some date values can be derived from seconds obtained from the clock. However, dates have some anomalies. For example, there are leap years. Because a year is not exactly 365 days. Yet, an approximation can be done using some calculation. For example, the code below prints current year approximately:

    year = 1970 + int( ticks / ( 365 * 24 * 60 * 60))

    print('Years Approximately:',year)

    The calculation is simple multiplication of 365 days, 24 hours, 60 minutes, 60 seconds.

    Local Time

    The time as years, months, days, hours, minutes, seconds etc. can be obtained from the localtime() function. It returns an object of the type structure_time. It contains fields for time units. The local time can be obtained as below:

    local = time.localtime()

    print('Local Time: ',local)

    The code above produces this output:

    time.struct_time(tm_year=2021, tm_mon=5, tm_mday=4, tm_hour=20, tm_min=25, tm_sec=11, tm_wday=1, tm_yday=124, tm_isdst=0)

    Structure of Time

    The struct_time (structure of time) object contains all units of times. If output is written in a better way as below:

    time.struct_time(

       tm_year=2021,

       tm_mon=5,

       tm_mday=4,

       tm_hour=20,

       tm_min=25,

       tm_sec=11,

       tm_wday=1,

       tm_yday=124,

       tm_isdst=0)

    These fields and their meanings:

    year : year

    mon : month (0-12)

    mday    : day of month (0-31)

    hour : hour

    min       : minute

    sec       : second

    wday : day of week (0-6) (0 is Monday)

    yday   : day of year (0-366)

    isdst : is  daylight saving (-1, 0, 1)

    Fields of time structure can be accessed independently. For example, the code below prints day of year:

    print('Day Of Year: ', local.tm_yday )

    The output is as below:

    124

    It is the 124th day of the year, which is May 5th.

    Time To String

    The fields of time structure can be used to show time formatted. For example, the code below prints date in the international format:

    print('Date: ',

       local.tm_year,'-',

       local.tm_mon,'-',

       local.tm_mday)

    The result will be as below:

    2021 - 5 - 4

    Similarly, the time of day can be printed as below:

    print('Time: ',local.tm_hour,':',local.tm_min)

    The result:

    Time:  20 : 52

    Date Time

    In Python, another module named datetime exists for time operations. While the time module is in low level and related to hardware and operating system, the datetime module is in high level and supports some useful features such as time zone. To import the module a code as below can be written:

    import datetime

    There may be a confusion about the name datetime because both the module and the class named as datetime. In most cases something like this is used:

    datetime.datetime

    Current Time

    In order to get current time, the now() method can be used. Example:

    now = datetime.datetime.now()

    print( 'Şimdiki Zaman: ', now)

    The output is as below:

    Current Time:  2021-05-04

    Format

    A datetime object can be formatted using format() function as string type. Example:

    formatted1='{0:%d.%m.%Y %H:%M}'.format(now)

    The format() function recognizes some letters as fields of time. Here d is a day, m is month, Y is year, H is hour, M is month. The letters are case-sensitive. For example, m and M have different meanings.

    The datetime object has a method named strftime() (string format time). It converts object to string as below:

    formatted2 = now.strftime( '%d.%m.%Y %H:%M' )

    print('Formatted Time 2: ',formatted2)

    The letters used for formatting are the same used for format() function. The biggest difference is the use of curly brackets ({}) and numbers. The format() function may be used when there are multiple types of variables, such as integers or floats. On the other hand, strftime() is a method of datetime object and converts its own object to string.

    Sleep

    The time module has a function named sleep() for waiting for a certain time. It takes time as seconds. It stops executing the Python script. The use of the function is as below:

    time.sleep(1)

    This function can be used to simulate an actual code which takes some time.

    In the example below, for 10 times a string written on console:

    print('Sleeping started.')

    for _ in range(10):

      time.sleep(1)

      print('Sleeping continues..')

    print('Sleeping finished.')

    The result is as below:

    Sleeping started.

    Sleeping continues..

    Sleeping continues..

    Sleeping continues..

    Sleeping continues..

    Sleeping continues..

    Sleeping finished.

    Example

    An example using several features about time can be written as below:

    import time

    ticks=time.time()

    print('Ticks: ',ticks)

    year = 1970+int(ticks/(365*24*60*60))

    print('Years Approximately:',year)

    local=time.localtime()

    print('Local Time: ',local)

    print('Day Of Year: ',local.tm_yday)

    print('Date: ',

       local.tm_year,'-',

       local.tm_mon,'-',

       local.tm_mday)

    print('Time: ',local.tm_hour,':',local.tm_min)

    import datetime

    now = datetime.datetime.now()

    print('Current Time: ',now)

    formatted1='{0:%d.%m.%Y %H:%M}'.format(now)

    print('Formatted Time 1: ',formatted1)

    formatted2=now.strftime( '%d.%m.%Y %H:%M')

    print('Formatted Time 2: ',formatted2)

    print('Sleeping started.')

    for _ in range(5):

     time.sleep(1)

     print('Sleeping continues..')

    print('Sleeping finished.')

    In the first part, the time module is imported. Using time() function, seconds after the epoch time of year 1970 is printed. The time which can be understood by human beings is obtained by localtime() function. By using the returned structure_time object, the parts of time such as year, month, day, hour, minute are printed.

    The second part shows the use of the datetime module. In this case, current time is learned by using the now() function in the datetime object. Firstly, the time is converted to string by using format() function. Secondly, the same thing is done using strftime() (string format time) function.

    In the last section, use of the sleep() function in the time module is shown. For 5 times, execution of the script is stopped for 1 seconds. A for loop is used to do this. When the script is executed, output will show slowly, step by step. The action will continue for approximately 5 seconds.

    The console output of the example is as blow:

    Ticks:  1620207983.6095982

    Years Approximately: 2021

    Local Time:  time.struct_time(tm_year=2021, tm_mon=5, tm_mday=5, tm_hour=12, tm_min=46, tm_sec=23, tm_wday=2, tm_yday=125, tm_isdst=0)

    Day Of Year:  125

    Date:  2021 - 5 - 5

    Time:  12 : 46

    Current Time:  2021-05-05 12:46:23.609598

    Formatted Time 1:  05.05.2021 12:46

    Formatted Time 2:  05.05.2021 12:46

    Sleeping started.

    Sleeping continues..

    Sleeping continues..

    Sleeping continues..

    Sleeping continues..

    Sleeping continues..

    Sleeping finished.

    Random

    In order to generate random values, a module named random can be used. There are a few functions to generate random numbers. An import statement as below can be written to use random functions:

    import random

    Generation

    The module random has a method named random().  It can be used to generate a value between 0 and 1. Here, 0 is inclusive, 1 is exclusive. The generated numbers are called normalized because normal means a value between 0 and 1.

    An example may be given as below:

    normalized = random.random()

    print('Normalized: ',normalized)

    The result will be:

    Normalized:  0.05859373150583902

    Seed

    Although the numbers are generated by a random function they can be predictable. It can produce the same number even if they are random. This is because random functions use a specific value called seed. If it is not changed, results may be the same. This can be required to see the same value so that an application has always the same numbers. If numbers wanted to be truly random, a method named seed() can be used. Example:

    random.seed(7919)

    A certain value can be used as a seed . It is prefered to be a prime number.  

    If the same number is used for each time the application runs, the results may be the same again, even if they are different from the ones for which a seed is not used. In order to produce unpredictable values; a variable value can be used. For this, the functions about current time in time or datetime modules can be used. Because the current time always changes.

    The one using the time module and time() function which uses ticks:

    import time

    random.seed(time.time())

    Alternatively, the method now() in the datetime object in datetime module can be used. In other words, current date and time can be used as seed. Example:

    import datetime

    random.seed( datetime.datetime.now() )

    Since the time values used as a seed are changed every second, each time they are called a different value is produced.

    Denormalization

    Since numbers other than between 0 and 1 may be needed, an operation called denormalization should be performed to generate numbers for other intervals. The formula for this is as below:

    denormalized = min + range * random.random()

    Firstly, the random number is multiplied by a range or interval value. Then a minimum is added.

    If the number is required to be a whole number, int() (integer) function can be called. For example, the code below generates a number between 150 and 200.

    denormalized = 150 + int( 50 * random.random() )

    Random Integer

    Instead of denormalizing the generated value, some functions doing this can be used. For example, randint() (random integer) generates an integer number. The code below does the same thing as the above:

    whole = random.randint( 150, 199 )

    print( 'Whole: ', whole )

    The second parameter is inclusive in this method.

    The output will be as below:

    Whole:  194

    Random Range

    Another method named randrange() (random range) can generate values in an interval. Example:

    ranged=random.randrange(150,200)

    print('Ranged: ',ranged)

    This second parameter (last value) is exclusive. The biggest number the example produces is 199, not 200. The output:

    Ranged:  190

    This method can generate numbers by an interval called step. For example, the code below generate numbers with 5 between each:

    stepped = random.randrange( 150, 200, 5 )

    print('Stepped: ',stepped)

    The output:

    Stepped:  155

    The result will always be the fold of the step, 5 in the example.

    Random List

    If more random values than one are needed, the random functions can be used together with the for loop. Example:

    array=[

       random.randrange(150,200,5)

           for element in range(10)]

    print('Array:')

    print(array)

    The output is as blow:

    Array:

    [ 170, 155, 190, 180, 175, 185, 155, 190, 190, 180 ]

    Choose

    If it is needed to randomly choose a number from an array, the choose() function can be used. Example:

    values = [ 5, 1, 2, 7, 4, 1, 8, 1, 6, 3]

    chosen = random.choice( values )

    print('Chosen: ',chosen)

    The output is as below:

    Chosen:  4

    For this function, the randomly generated value is not an element in the list. It is the index randomly generated. The result will be one of the values in the list.

    Shuffle

    The order of elements in a list can be changed randomly. It is called to shuffle. For this shuffle() is used. Example:

    random.shuffle(values)

    print('Shuffled:')

    print(values)

    The output:

    Shuffled:

    [ 7, 6, 4, 1, 1, 2, 1, 3, 8, 5 ]

    The result does not contain random values. The values are the same as before. The only thing randomized is the positions of the elements.

    Example

    The code below contains basic operations about generation of random values:

    import random

    import time

    import datetime

    #random.seed(7919)

    #random.seed(time.time())

    #random.seed(datetime.datetime.now())

    normalized=random.random()

    print('Normalized: ',normalized)

    denormalized = 150 + int( 50 * random.random() )

    print('Denormalized: ',denormalized)

    whole=random.randint(150,199)

    print('Whole: ',whole)

    ranged=random.randrange(150,200)

    print('Ranged: ',ranged)

    stepped=random.randrange(150,200,5)

    print('Stepped: ',stepped)

    array=[

       random.randrange(150,200,5)

           for element in range(10)]

    print('Array:')

    print(array)

    values=[ 5, 1, 2, 7, 4, 1, 8, 1, 6, 3]

    chosen=random.choice(values)

    print('Chosen: ',chosen)

    random.shuffle(values)

    print('Shuffled:')

    print(values)

    The first part of the example uses the seed() function. It is left as a comment. One of them can be activated. The first function is random(). It generates normalized numbers between 0, inclusive, and 1, exclusive. After that, it is shown how to denormalize random numbers to fit in a certain interval. For producing a random integer in a certain interval is done by randint(). The example contains the use of the randrange() function which has an option of generating numbers in steps.

    The last part contains examples for random operations about multiple values. Firstly a random array is generated using the randrange() function in a for loop. Secondly, an element selected randomly from a list by using choose() function. In the third one, the elements in a list changed positions randomly by the shuffle() function.

    Output of the example as below:

    Normalized:  0.44282076943192494

    Denormalized:

    Enjoying the preview?
    Page 1 of 1