Using Python Libraries
Using Python Libraries
com
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
Introduction
As our program become larger and more complex the
need to organize our code becomes greater. We have
already learnt in Function chapter that large and
complex program should be divided into functions that
perform a specific task. As we write more and more
functions in a program, we should consider organizing of
functions by storing them in modules
A module is simply a file that contains Python code.
When we break a program into modules, each modules
should contain functions that perform related tasks.
Commonly used modules that contains source code for
generic needs are called Libraries.
Introduction
When we speak about working with libraries in
Python, we are, in fact, working with modules that
are created inside Library or Packages. Thus a
Python program comprises three main components:
Library or Package
Module
Function/Sub-routine
What is module?
Act of partitioning a program into individual
components(modules) is called modularity. A module
is a separate unit in itself.
It reduces its complexity to some degree
It creates numbers of well-defined, documented
boundaries within program.
Its contents can be reused in other program, without
having to rewrite or recreate them.
MODULES
VARIABLES OTHER
PYTHON
MODULES
FUNCTIONS
VARIABLES
IMPORT
CLASSES
MEMBERS OTHER
PYTHON
METHODS MODULES
help() function
Is used to get detailed information about any
module like : name of module, functions inside
module, variables inside module and name of file
etc.
Namespace
Is a space that holds a bunch of names. Consider an
example:
In a CCA competition of vidyalaya, there are students from
different classes having similar names, say there are three
POOJA GUPTA, one from class X, one from XI and one from
XII
As long as they are in their class there is no confusion, since
in X there is only one POOJA GUPTA, and same with XI and
XII
But problem arises when the students from X, XI, XII are
sitting together, now calling just POOJA GUPTA would
create confusion-which class‟s POOJA GUPTA. So one need
to qualify the name as class X‟s POOJA GUPTA, or XI‟s or
XII‟s and so on.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Namespace
From the previous example, we can say that class X has its
own namespace where there no two names as POOJA
GUPTA; same holds for XI and XII
In Python terms, namespace can be thought of as a named
environment holding logical group of related objects.
For every python module(.py), Python creates a namespace
having its name similar to that of module‟s name. That is,
namespace of module AREA is also AREA.
When 2 namespace comes together, to resolve any kind
of object name dispute, Python asks you to qualify the
name of object as <modulename>.<objectname>
Creating Package
Step 1
Create a new folder which you want to act as package. The
name of folder will be the name of your package
IN THE C:\USERS\VIN
A new Folder “mypackage” is
created.
Note: you can create folder in
any desired location
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Creating Package
Step 2: Create modules (.py) and save it in
“mypackage” folder numcheck.py
area.py
Creating Package
Step 2: importing package and modules in python
program
Functions of URLLIB
FUNCTION NAME PURPOSE
urllib.request.urlopen(<url>) Opens a website or network object
denoted by URL for reading and return
file like object using which other functions
are often used
urlopen_object.read() Return HTML or the source code of given
url
urlopen_object..getcode() Returns HTTP status code where 200
means „all okay‟ 404 means url not found
etc. 301, 302 means some kind of
redirections happened
urlopen_object.headers Stores metadata about the open URL
Example:
WEBBROWSER MODULE
Provides functionality to open a website in a
window or tab or web browser on your computer,
from within your program.
To use webbrowser module we must import the
module as:
import webbrowser