Urgenthomework logo
UrgentHomeWork
Live chat

Loading..

Modules in Python Programming Tutorials

Modules in Python

In this module we shall be looking on modules in Python. What are modules, how do we use them, and even look at creating one such module.

Let us get started by knowing about a module.

A module is a file which consists a Python code. In a module we can define functions, classes variables etc. As I have told you earlier, there are a lot of functions in Python. For using the functions, we need an import statement which tells the interpreter that we are going to use a functionality that are mentioned in that module.

If we want to take a particular functionality from a module, then we need to use the “from” that is, from Module_name import functionality.

If you want to use all the functionalities of a module, then you need to write an asterisk (*) like from module_name import *.

Let us look at a program to get a better understanding.

 {`from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
import os
chatbot = ChatBot('Kazami')
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train('C:..\chatterbot-corpus-master\chatterbot-corpus-master
\chatterbot_corpus\data\english')
for files in os.listdir
('C:..\chatterbot-corpus-master\chatterbot-corpus-master
\chatterbot_corpus\data\english'):
 print(files)
 print('C:..chatterbot-corpus-master\chatterbot-corpus-master
\chatterbot_corpus\data\english/'+ files)
 data ='C:..chatterbot-corpus-master\chatterbot-corpus-master
\chatterbot_corpus\data\english/'+ files trainer.train(data)
while True:
 message = input('You:')
 if message.strip()!= 'Bye':
 reply = chatbot.get_response(message)
 print("Kazami:",reply)
 if message.strip()== 'Bye':
 print('Kazami: Bye')
 break
`}

The above code is just for the understanding about the modules. Like here we are using the from and the import statements. From the module chatterbot we are importing a function ChatBot, we are importing the os module, and from chatterbot.trainers we are importing ChatterBotCorpusTrainer . there are a lot of functions and modules.

You can also create your own module and use its functions.

It is quite simple. Suppose I have made a program which calculates sum of numbers, then in the other file I can import that program as a module. All you need to do is:

Import operation from name_of_file/project.

Let us look at the time module and its functionality.

The time module is one of the most popular modules available in Python which provides functionalities with allows us to do things like time conversions, reading time from the system etc. we shall be looking at some of the functions and will play around time.

Let us look at a program used to get the local time.

{`import time
t = time.localtime(time.time())
print(t,'is the local time')
`}

we have imported the time module and used the localtime() function and the time function and then printed the local time.

Output:

time.struct_time(tm_year=2019, tm_mon=7, tm_mday=1, tm_hour=16, tm_min=49, tm_sec=1, tm_wday=0, tm_yday=182, tm_isdst=0) is the local time

But this output is not eye pleasing. Let us see how we can get an output that is easy to read and correct.

Let us look at another function.

{`import time
e = time.asctime(time.localtime(time.time()))
print(e)
`}

Output:

Mon Jul 1 17:04:18 2019

This is much more readable than the previous one. Here we are getting the day date month year and the current time.

Mentioned below are some of the functions that are used in the module time along with their uses.

2

time.asctime([tupletime])

Takes a time-tuple and returns 24-character string such as 'Thurs Jan 14 21:25:17 2015'.

3

time.clock( )

Gives CPU time in decimal number. time.clock is better than time.time() for measuring computational costs of different approaches

4

time.ctime([secs])

Like asctime(localtime(secs)) and without arguments is like asctime( )

5

time.gmtime([secs])

It accepts instant which is expressed in seconds as the epoch and returns a time-tuple with the UTC time. Note : t.tm_isdst is always 0

6

time.localtime([secs])

It accepts instant that is expressed in seconds as the epoch and returns a time-tuple with the local time

7

time.mktime(tupletime)

It accepts instant that is expressed as a time-tuple in the local time and returns a decimal value with the instant which expressed in seconds.

8

time.sleep(s)

It will suspend the calling thread for s seconds.

9

time.strftime(fmt[,tupletime])

It accepts instant that is expressed as a time-tuple in local time and returns a string representing the instant as specified by string fmt.

10

time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')

It parses str according to the format that is given in fmt and returns instant in time-tuple format.

11

time.time( )

Returns the current time, a decimal number of seconds.

12

time.tzset()

Resets the time conversion rules used by the library routines. The variable TZ will specify that how it is to be done.

Let us look at another module that is, “calendar”.

The calendar module gives functions that are related to calendar like printing a text calendar for a month and year of your choice.

Let us look at a program to get a better understanding.

import calendar

t = calendar.month(2019,5)

print(‘calendar for the fifth month of 2019 is:’)

print(t)

Output:

May 2019

Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Let us look at some of the functions that are included in the calendar module.

2

calendar.firstweekday( )

Returns the current setting for the weekday that starts each week. By default, when calendar is first imported, this is 0, meaning Monday.

3

calendar.isleap(year)

Returns True if the year is a leap year; otherwise, False.

4

calendar.leapdays(y1,y2)

Returns the total number of leap days in the years within range(y1,y2).

5

calendar.month(year,month,w=2,l=1)

Returns a multiline string with a calendar for month of year, one line per week plus two header lines. w is the width in characters of each date; each line has length 7*w+6. l is the number of lines for each week.

6

calendar.monthcalendar(year,month)

Returns a list of lists of ints. Each sublist denotes a week. Days outside month of year are set to 0; days within the month are set to their day-of-month, 1 and up.

7

calendar.monthrange(year,month)

Returns two integers. The first one is the code of the weekday for the first day of the month in year; the second one is the number of days in the month. Weekday codes are 0 (Monday) to 6 (Sunday); month numbers are 1 to 12.

8

calendar.prcal(year,w=2,l=1,c=6)

Like print calendar.calendar(year,w,l,c).

9

calendar.prmonth(year,month,w=2,l=1)

Like print calendar.month(year,month,w,l).

10

calendar.setfirstweekday(weekday)

Sets the first day of each week to weekday code weekday. Weekday codes are 0 (Monday) to 6 (Sunday).

11

calendar.timegm(tupletime)

The inverse of time.gmtime: accepts a time instant in time-tuple form and returns the same instant as a floating-point number of seconds since the epoch.

12

calendar.weekday(year,month,day)

Returns the weekday code for the given date. Weekday codes are 0 (Monday) to 6 (Sunday); month numbers are 1 (January) to 12 (December).

This is it for this tutorial. I hope that you learnt something from this tutorial. You may try all the above-mentioned functions from the header files and see the results yourself. See you in the next one!

Copyright © 2009-2023 UrgentHomework.com, All right reserved.