Uncategorized – Teknonauts https://teknonauts.com Stay Tuned With Technology Thu, 15 Jul 2021 09:01:58 +0000 en-US hourly 1 https://wordpress.org/?v=5.7.5 https://teknonauts.com/wp-content/uploads/2021/01/cropped-teknonauts_favicon_original-1.png Uncategorized – Teknonauts https://teknonauts.com 32 32 #30 Python Programming – Modules, Package and Object-Oriented Programming https://teknonauts.com/python-programming-06/ https://teknonauts.com/python-programming-06/#respond Wed, 14 Jul 2021 08:32:47 +0000 https://teknonauts.com/?p=4298

Python modules

  • Module is a python file which contains python data members i.e., statements and definitions.
  • A file containing Python code, for e.g.: fax.py, is called a module and its module name would be fax.
  • Like functions, modules help us to break down large python programs into small manageable and organized files. Furthermore, modules provide reusability of code.

How to create a module?

  • Module is a python script file, let us create one function definition below fax module
File : fax.py
def  calc(a1,a2):  # function calc will receive two arguments
          total=a1+a2
          print  “Sum of total value:”,total

Note : In the above code, there is no function call. It contains only definition.

How to import modules in Python?

 We can import the definitions present inside a module to another module or the interactive interpreter in Python.

  • We use the import keyword to do this. 
  • To import our previously defined module fax, we type the following in the Python prompt.

>>> import fax

· Using the module name we can access the function using dot (.) operation. for example:

>>> fax.calc(10,20)

30

>>>fax.calc(1.45,3.56)

5.01

· Within a module, the module’s name is available as the value of the global variable __name__. 

>>>fax.__name__

‘fax’

Advantage of modules

  • Code reusability
  • We may want to split single script (or) definition into multiple file for easier maintenance  
  • We can import a module on demand.
  • Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module.
  • Modules can import other modules
  • Open the below link will get list of python standard modules https://docs.python.org/3/py-modindex.html 
  • These files are in the Lib directory inside the location where you installed Python.
  • Standard modules can be imported the same way as we import our user-defined modules.
  •  There are various ways to import modules.

Python import statement

We can import a module using import statement and can access the definitions inside it using the dot operator as described above.

Here is an example.

# import statement example

# to import standard module math

import math

print(“The value of pi is”, math.pi)

When you run the program, the output will be:

The value of pi is 3.141592653589793

Import with renaming

  • We can import a module by renaming it as follows.
  • # import module by renaming it
  • import math as m     # alias (or) symbolic name
  • print(“The value of pi is”, m.pi)
  • We have renamed the math module as m
  • This can save us typing time in some cases.
  • Note that the name math is now not recognized in our scope.
  • Hence, math.pi is invalid, m.pi is the correct implementation.

Python from…import statement

  • We can import specific names from a module without importing the module as a whole. Here is an example.

# import only pi from math module

from math import pi

print(“The value of pi is”, pi)

  • We have imported only the attribute pi from the module.
  • In such case we don’t use the dot operator.
  • We can also import multiple attributes as follows.
>>> from math import pi, e
>>> pi
3.141592653589793
>>> e
2.718281828459045

Import all names

  •  We can import all names (definitions) from a module using the following construct.
  • # import all names from the standard module math
from math import *
print("The value of pi is", pi)
  • Now we have imported all the definitions from the math module.
  • This makes all names except those beginning with an underscore, visible in our scope.
  • However, Importing everything with the asterisk (*) symbol is not a good programming practice.
  • This can lead to duplicate definitions for an identifier. It also hampers the readability of our code.

Python Module Search Path

  • While importing a module, Python looks at several places.
  • Interpreter first looks for a built-in module then (if not found) into a list of directories defined in sys.path.
  • The search is performed in this order.
    •  The current directory.
    • PYTHONPATH (an environment variable with a list of directory).
    • The installation-dependent default directory.

What is sys.path ?

C:\Users\python>python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs', 'C:\\Python27\\Lib\\lib-tk',
'C:\\Users\\python', 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\lib\\plat-win', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']
>>>
>>>

Module path list in linux os

Example :

root@awadheshpdwivedi]#  pwd

/root/python/modules/example

Creating one module file name called report.py

File : report.py

import os
def  display() :
         print “Current vmreport status :-“
        os.system(“vmstat”)
        print “Current CPU Load balance:-“
        os.system(“uptime”)

Importing report.py file into an external file

File : p1.py

import  report

report.display()

root@awadheshpdwivedi]#  ls

p1.py  report.py

root@awadheshpdwivedi]# python p1.py

#  display vmstat and uptime command result to the screen

  •  After completing the execution, again type ls command from linux command line (or) dir command in windows operating system.

root@awadheshpdwivedi]#  ls

p1.py  report.py report.pyc

· The report.pyc file contain a ready-“byte-compiled” version of the module report.

· Test the file type in linux command line

root@awadheshpdwivedi]#  file  report.pyc   

# file is a linux command – it’s determine file type

report.pyc :python 2.7 byte-compiled

· Normally, you don’t need to do anything to create the report.pyc file.

· Whenever report.py is successfully compiled, an attempt is made to write the compiled version to report.pyc.

· Now let us create one sub directory under your current working directory.

root@awadheshpdwivedi]#  mkdir L1

root@awadheshpdwivedi]# ls

L1  p1.py report.py report.pyc

· Now let us move the report.py file into L1 sub directory

root@awadheshpdwivedi]# mv report.py L1

root@awadheshpdwivedi]# ls

L1 p1.py  report.pyc

· Now there is no report.py file , but report.pyc file is available in current working directory. So script (p1.py ) will execute as follows.

root@awadheshpdwivedi]# python p1.py

# Display system commands

# Execution done successfully

 #Deleting report.pyc file from current working directory

root@awadheshpdwivedi]# rm report.pyc

L1 p1.py

· Now there is no report.py and report.pyc files in my current working directory.

· If you run the p1.py file now, we will get an error message.

· We can’t always keep module file and script files in same directory.

· But sometimes we load external modules or in built module such as os or sys.The os.py or sys.py  files are not in our current working directory.

. If we import os module in to script , the file loads successfully. Let’s see them in detail.

Example :

root@awadheshpdwivedi]# ls

L1 p1.py  

· Now let us create one more file name called p2.py

File :p2.py

import os

os.system(“uptime”)

· Before executing this file (p2.py) ,list out all the available files.

root@awadheshpdwivedi]# ls

L1 p1.py  p2.py

· Note down there is no os.py or os.pyc file in our current working directory. But if we run the program p2.py  it will execute successfully, there is no import or attribute errors

root@awadheshpdwivedi]#  python p2.py

# Execute successfully

# display uptime command result to #screen.

  • The reason behind is that, whenever we are interpreting python file, interpreter searches the module file from sys.path variable .
  • The sys.path is list type of variable.
  • This is equivalent to $PATH in shell command line ,@INC in perl script , $LOAD_PATH in ruby.

root@awadheshpdwivedi]# python

import sys

sys.path

  • Look at the result which is list type of data structure. So we can use all list functions and list manipulation.
  • The sys.path variable contains the current directory (. ) , installed python path.
  • We can use append or insert functions to insert our external module file into sys.path.

Example

File : p3.py

import  sys

sys.path.append(“/root/python/modules/L1”)

import report

report.display()

root@krosumlabs day1]#  ls

L1  p1.py p2.py p3.py

# note there is no report.py file

  • We added (appended) L1 directory ( which contains module files) into sys.path variable ,so now python interpreter look at (“/root/python/modules/day1/L1) where report.py file is present. So it will load successfully.
  • If you want to make permanent update use PYTHONPATH variable.

What is PYTHONPATH ?

  • The PYTHONPATH is an environment variable, consisting of a list of directories.
  • The syntax of PYTHONPATH is the same as that of the shell variable PATH.
  • Here is a typical PYTHONPATH from a Windows system ?

set PYTHONPATH = c:\python20\lib;

· In linux operating system

open a  login profile file (.profile or .bash_rc )

export PYTHONPATH=”/root/python/modules/day1/L1”

Now open new shell , test the belowscript

File :p4.py

import report

report.display()

  • Run the p4.py file , the script will execute successfully.
  • Compare p3.py and p4.py file, understand the sys.path and PYTHONPATH variable usages.

Reloading a module

  • The Python interpreter imports a module only once during a session.
  • This makes things more efficient.
  • Suppose we have the following code in a module named my_module.
# This module shows the effect of
#  multiple imports and reload
  print("This code got executed")

Now we see the effect of multiple imports.

>>> import my_module
This code got executed
>>> import my_module
>>> import my_module
  • We can see that our code got executed only once. This goes to say that our module was imported only once.
  • Now if our module changed during the course of the program, we would have to reload it.
  • One way to do this is to restart the interpreter. But this does not help much.
  • Python provides a neat way of doing this. We can use the reload() function inside the imp module to reload a module. This is how its done.
>>> import imp
>>> import my_module
This code got executed
>>> import my_module
>>> imp.reload(my_module)
This code got executed

dir() built-in function

  • We can use the dir() function to find out names that are defined inside a module.
  • For example, we have defined a function add() in the module example that we had in the beginning.
>>> dir(example)
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__initializing__',
'__loader__',
'__name__',
'__package__',
'add']
  • Here, we can see a sorted list of names (along with add).
  • All other names that begin with an underscore are default Python attributes associated with the module (we did not define them our self).
  • For example, the_name_attribute contains the name of the module.
>>> import example
>>> example.__name__
'example'
  • All the names defined in our current namespace can be found out using the dir() function without any arguments.
>>> a = 1
>>> b = "hello"
>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']

Python Package

  • Python package is a hierarchical file directory structure.
  • We don’t usually store all of our files in our computer in the same location.
  • We use a well-organized hierarchy of directories for easier access.
  • Similar files are kept in the same directory, for example, we may keep all the songs in the “music” directory.
  • Analogous to this, Python has packages for directories and modules for files.
  • As our application program grows larger in size with a lot of modules, we place similar modules in one package and different modules in different packages.
  • This makes a project (program) easy to manage and conceptually clear.
  • Similar, as a directory can contain sub-directories and files, a Python package can have sub-packages and modules.
  • A directory must contain a file named init.py in order for Python to consider it as a package.
  • This file can be left empty but we generally place the initialization code for that package in this file.

Package structure

  • Create a directory Demo and under the Demo directory let us create a package structure that holds the below format.
root@awadhesh ~]#mkdir  Demo
root@awadhesh  ~] # cd Demo
root@awadhesh  Demo]#mkdir   Fax
  • Under Fax directory there are three python scripts such as p1.py ,p2.py and p3.py.
  • Each file contains several functions. Instead of loading these individual files and functions, we are creating Fax as a package which will initialize all the functions under init.py file.
  • We can import modules from a package using the dot (.) operator. To import particular names from a module directory , use following syntax :-
from module import functions

from module import functions

Go to Fax directory

root@awadhesh Demo]#   cd   Fax
 
· Create an initialization file __init__.py.
root@awadhesh  Fax]#  vi __init__.py
from p1.py import f1,f2,f3
from p2.py import calc,bc
from p3.py import view,report,result
# save and exit
  • Now move to the parent directory Demo and create one new python script file,say demo_package.py and import the package Fax inside it.
root@awadhesh Demo]#   vi  demo_package.py
import  Fax  #  Now Fax is a package which encapsulates a collection of modules and methods
Fax.f1()  # to invoke f1() method
Fax.calc()   # to invoke calc() method
  • While importing packages, Python looks in to the list of directories defined in sys.path, similar to module search path.

Object Oriented Programming in Python

  • Python is an object oriented programming language.
  • It is a way of organizing the program, which is to combine the data and functionality and wrap them inside something called an object.
  • Classes and objects are the two main aspects of object oriented programming.
  • The class becomes a new type where objects are their instances.
  • The class defines a data type, which contains variables, properties and methods.
  • A class describes the abstract characteristics of a real-life thing.
  • Object is simply a collection of data (variables) and methods (functions) that act on those data whereas class is a blueprint for the object.

Defining a Class in Python

  • In Python, we define a class using the keyword class.
  • Class which contains collection of variables and methods preferably called as data members.
  • A class creates a new local namespace where all its data members are defined.
>>> class Box:
...            pass
>>> Box
>>> type(Box)
<__main__.Box instance at 0x0247D440>

Creating an Object in Python

  • A class is used to create new object instances (instantiation).
  • These class objects allow us to access the different attributes of the class.
  • The procedure to create an object looks very similar to a function call.

Syntax:-

Example :-

>>> class Box:
...        name="Krosum"
>>> obj=Box()
>>> type(obj)
>>> obj.name
'Krosum'
obj.name is interpreted as Box.name
 >>> class Box:
...         name="Krosum"
...         code=34
...         ips=['10.20.30.40','10.20.30.50']
...         books={'subject1':'programming python','subject2':'Python Essentials'}
 

>>> obj=Box()
>>> type(obj.name)
>>> type(obj.code)
>>> type(obj.ips)
>>> type(obj.books)
>>> print "Hello",obj.name
Hello Krosum
>>> print "Total no.of IPs",len(obj.ips)
Total no.of IPs 2
>>> # adding new items to the existing list
>>> obj.ips.append("127.0.0.1")
>>> # List of updated IPs
>>> print obj.ips
['10.20.30.40', '10.20.30.50', '127.0.0.1']
>>> #accessing dictionary values
>>> print obj.books['subject1']
Programming python
>>> # modifying existing dictionary value
>>> obj.books['subject1']="PYTHON PROGRAMMING GUIDE"
>>> # adding new subject to the existing dictionary
>>> obj.books['subject3']="Network Python Program"
>>> # List of all the keys(subjects) from dictionary (books)
>>> obj.books.keys()
['subject1', 'subject2', 'subject3']
# List of all the values (subjects) from dictionary (books)
>>> obj.books.values()
['PYTHON PROGRAMMING GUIDE', 'Python Essentials', 'Network Python Program']

From the above examples, we have accessed the class data members through the class instance (object).

We can create more than one object from a single class.

Each such object contains an individual reference (memory).

>>> class Box:
...        name=""
...        fname=""
 
>>> obj1=Box()    # object1 is created
>>> obj2=Box()    # object2 is created
>>> obj3=Box()    # object3 is created
>>> obj1.name="User A"
>>> obj1.fname="/etc/passwd"
>>> obj2.name="User B"
>>> obj2.fname="/var/log/auth.log"
>>> obj3.name="User C"
>>> obj3.fname="/home/userC/temp.txt"
>>> print "User Name:",obj1.name,"\tFile name:",obj1.fname
User Name: User A       File name: /etc/passwd
>>> print "User Name:",obj2.name,"\tFile name:",obj2.fname
User Name: User B       File name: /var/log/auth.log
>>> print "User Name:",obj3.name,"\tFile name:",obj3.fname
User Name: User C       File name: /home/userC/temp.txt
Now let us see how to access class function (methods) using class instance.
>>> class Box:
...        name="Krosum"
...        DBs=['oracle','sql','mysql']
 

...     def    hello():
...             print "Im hello function from Box class"
>>>
>>> obj=Box()
>>> obj.name
'Krosum'
>>> obj.DBs
['oracle', 'sql', 'mysql']
>>> obj.DBs[-1]
'mysql'
>>> obj.hello()
Traceback (most recent call last):
  File "", line 1, in
TypeError: hello() takes no arguments (1 given)
>>>
Here, we didn’t pass any argument but it displays ‘ TypeError: hello () takes no arguments (1 given)’
Whenever an object calls its method, the object itself is passed as the first argument, i.e) the form object.method() is internally translated into ClassName.method(object).
>>> obj.hello()    #  Box.hello(obj)   # like function call with single argument
In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list with method's object as its first argument.
For these reasons, the first argument of the function inside class (method definition) must be the object itself.
This is conventionally called self.
 
>>> class Box:
...     def hello(self):
...             print "Im hello method from Box class"
>>> obj=Box()
>>> obj.hello()
Im hello function from Box class
Note that an the argument (self) is passed in the method definition inside the class but still, we called the method simply as object.hello() without any arguments.
Here, obj.hello()  is interpreted as  hello(obj)
>>> class Box:
...            def hello(self):
...                   print "Im hello function",self
>>>
>>> obj=Box()
>>> obj.hello()
Im hello function <__main__.Box instance at 0x023DD620>
>>> obj
<__main__.Box instance at 0x023DD620>
Note the above result. Both self and obj refers to same location.
So self is just an object placeholder.
obj.name is same as  self.name
>>> class Box:
...           name="Vikas"
...           def hello(self):
...                     print "From hello function",self.name
>>>
>>> obj=Box()     # creating object
>>> obj.hello()  # object.method(  )
From hello function Vikas
We have already discussed that the class variables can be accessed only through class name or class instance.
>>> class Box:
...             name="Krosum"
...             def hello(self):
...                       print "Hello function",name  # Display Name Error
>>> obj=Box()
>>> obj.hello()
Hello function
Traceback (most recent call last):
  File "", line 1, in
  File "", line 4, in hello
NameError: global name 'name' is not defined
 
Here the class variable name has to be accessed as self.name
Variables in python are generally classified as Class variables, Instance variables and Private variables.
 
Class variables 
Class variables are defined only within a class definition.
We can access and use a class variable directly inside the class.
Example:
>>> class Box:
...        username="Krosum"    
...        Filename="/etc/passwd"
...     print "From Box Class username:",username
...     print "From Box Class Filename:",Filename
From Box Class username: Krosum
From Box Class Filename: /etc/passwd
From the above example username and Filename are class variables which are placed inside a class directly.
We can access class variables from outside the class definition, using class name or class instance (object)
 
Example :
>>> class Box:
...        username="Krosum"    
...        Filename="/etc/passwd"
>>> obj=Box()
>>> print “username:”,obj.username
Krosum
>>> print “Filename:”,obj.Filename
/etc/passwd
>>> print “username:”,Box.username
Krosum
>>>print “Filename:”,Box.Filename
/etc/passwd

Instance variable

  • Instance variables are the variables placed inside the class member function.
  • We can use instance variable only within the class member function.
  • We can’t use instance variable either out of the class or out of the class member functions.

Examples

class Box:
     book="Core Python Programming" # class variable
     def getdata(self,author,price,vol):   
             print "Book name:",self.book
             print "Author name:",author
             print "Price:",price
             print "Volume:",vol
#  author , price, vol are instance variables
# we can’t use instance variables outside the class function
 
 obj=Box()
 obj.getdata ("Mr.Guido van rossum",467.67,1.0)
 
Book name: Core Python Programming
Author name: Mr.Guido van rossum
Price: 467.67
Volume: 1.0
 
=> We can initialize instance variable to class variable
class Box:
        name="Mr.Krosum" # class varaible
        price=""     # class variable
        volume=""   # class variable
        def getdata(self,p,v):
                self.price=p    # instance --> class variable
                self.volume=v  # instance --> class variable
       def display(self):
             print "User name:",self.name # class variable
             print "Price name:",self.price # class variable
             print "Voulme:",self.volume # class variable
 

obj=Box()
obj.getdata(1000.34,1.0)
obj.display()
# Run the above script in command line
C:\Users\KrosumLabs>python p1.py
User name: Mr.Krosum
Price name: 1000.34
Volume: 1.0

Private variable

Class variable that begins with double underscore (__) are called special variable as they have special meaning

>>> class Emp:
...     name="Mr.Xerox"
...     __password="abc"       #  private variable  can’t be accessed from    outside the class
...     def display(self):
...             print self.name
...             print self.__password
 
>>> obj=Emp()
>>> obj.display()
Mr.Xerox
abc
>>> obj.name
'Mr.Xerox'
>>> obj.__password
Traceback (most recent call last):
  File "", line 1, in
AttributeError: Emp instance has no attribute '__password'

Constructors in Python

  • A class functions that begins with double underscore (__) are called special functions as they have special meaning.
  • Of one particular interest is the __init__()function. This special function gets called whenever a new object of that class is instantiated.
  • This type of function is also called constructors in Object Oriented Programming (OOP). We normally use it to initialize all the variables.
Example  1:-
>>> class Box:
                    def display(self):
                    print "Im display function"
 
>>> obj=Box()
>>> obj.display() # object.method() call
Im display function
 
Example 2:-
>>> class Box:
                  def __init__(self):
                          print   "Im Initialization function block"
>>> obj=Box()
Im Initialization function block  ## didn't used object.method(), __init__() automatically initialized
 

Example 3:-
>>> class Emp:
                name=" "
                dept=" "
                def   __init__(self):   # automatically initialized constructor block
                          self.name="Mr.John Paul"
                         self.dept="Sales"
                def   display(self):    # non  constructor block
                           print  "Emp Details:"
                          print "Name:",self.name
                          print "Dept:",self.dept
 
>>> obj=Emp()   # object  is initialized
>>> obj.display()   # object.method() call
Emp Details:
Name: Mr.John Paul
Dept: Sales

Destructors in Python

  • Destructors are called when an object gets destroyed.
  • The destructor is defined using __del__(self).
  • __del__(self)  method is only called on destruction of the object.
>>> class Box:
                 def __init__(self):
                        print "Im Constructor" 
                def __del__(self):
                        print   "Im Destructor"
>>> obj=Box()
Im Constructor 
>>> del(obj)
  Im Destructor
  • In the example, the obj is created and manually deleted; therefore, both messages will be displayed.
  • However, if you comment out the last line  del (obj), the destructor will not be called immediately.
  • Instead, the Garbage Collector (GC) will take care of the deletion automatically.

Python Inheritance

What is inheritance?

  • Inheritance is one of the OOPs feature.
  • The Reusability of existing class means that it refers to the concept of defining a new class with little or no modification to an existing class.
  • The new class is called child class or derived class.
  • Existing class is called parent class or base class.

Python Inheritance Syntax

class BaseClass :

           Body of base class
class DerivedClass ( BaseClass ):

          Body of derived class
  • Derived class inherits features from the base class, adding new features to it.
  • This results into re-usability of code.

Example :

import  os
class  Parent:
          def   display(self):
                   print    “Display mounted file system details :”
                   os.system(“df –Th”)
class Child(Parent) :   # Inheritance – Extending Parent class data members
          def  view(self):
                   print  “Current process details :-“
                   os.system(“ps –f”)
obj=Child()
obj.view()
obj.display()  # using child class object, calling parent data member
 
Example :
class  Parent :
def   display(self):
                    print “This is display block from Parent class”
 
class Child(Parent):
          def   display(self):
                   print “This is display block from Child class”
 
obj=Child()
obj.display()  #  This is display block from Child class

Method Overriding in Python

  • In the above example, notice that display( )method was defined in both classes, Parent as well Child.
  • When this happens, the method in the derived class overrides that in the base class.
  • Generally when overriding a base method, we tend to extend the definition.
  • The same is being done by calling the method in base class from the one in derived class.
class Parent():
          def display(self):
                      print “This is display block from Parent class”
class Child(P):
          def display(self):
                     print “This is display block from Child class”
                   Parent.display (self)   # calling Parent class display method
obj=Child()
obj.display() 

Python Multiple Inheritance

  • A class can be derived from more than one base classes, this is called multiple inheritance
  • In multiple inheritances, the features of all the base classes are inherited into the derived class.

Syntax

class Parent1:
           pass
class Parent2:
          pass
class Child(Parent1, Parent2):
           pass
  • The class Child inherits from both Parent1 and Parent2.

Multilevel Inheritance in Python

  • We can also inherit the features from a derived class. This is called multilevel inheritance. It can be of any depth in Python.
  • In multilevel inheritance, features of the base class and the derived class are inherited into the new derived class.

Syntax

class Parent1:
          pass
class Parent2(Parent1):
          pass
class Child(Parent2):
          pass

Here, Parent2 is derived from Parent1, and Child is derived from Parent2.

Using super() method  

class GrandParent(object):                                                      
         def act(self):                                                              
              print 'grandpa act'                                                     
class Parent(GrandParent):                                                      
         def act(self):                                                              
              print 'parent act'                                                      
class Child(Parent):                                                            
        def act(self):                                                              
              super (Child.__bases__[0], self) . act()                                   
              print 'child act'                                                       
 
instance = Child()                                                              
instance.act()

Built-In Class Attributes

  • Every Python class keeps the following built-in attributes and  they can be accessed using dot operator like any other attribute.
  •  __dict__         Dictionary containing the class’s namespace.
  •  __doc__         Class documentation string or none, if undefined.
  • __name__      Class name.
  • __module__   Module name in which the class is defined. ( This attribute is “__main__” in interactive mode.)
  • __bases__     A possibly empty tuple containing the base classes,in the order of their occurrence in the base class list.

Example 1 :

>>> class Box:
...     ' sample class document, this is Box class'
>>> Box.__doc__
' sample class document, this is Box class'
 
>>> Box.__dict__
{'__module__': '__main__', '__doc__': ' sample class document, this is Box class'}

Example 2 :

>>> class Box:
...     ' sample document message from Box class'
...     count=10
...     LIST=['data1','data2']
...
>>> Box.__doc__
' sample document message from Box class'
>>>
>>> Box.__dict__
{'count': 10, '__module__': '__main__', 'LIST': ['data1', 'data2'], '__doc__': '
 sample document message from Box class'}
>>> Box.__name__
'Box'
>>>
>>> Box.__module__
'__main__'
>>> Box.__bases__
()
>>>

 

Example 3:

>>> class Parent:
... pass
>>> class Child1(Parent):
... pass
>>> class Child2(Child1):
... pass
>>> obj=Child2()
>>>
>>> Child2.__bases__
>>> Child1.__bases__
>>> Parent.__bases__
()

Example 4:

File : Fax.py

class C1:
          def f1(self):
                   print "f1 func"
Import Fax.py file into external python file ( p2.py )
File: p2.py
import Fax
obj=Fax.C1()
obj.f1()
print Fax.C1.__name__  
print Fax.C1.__module__
Run the above (p2.py) file in command line.
C:\Users\awadheshpdwivedi>python   p2.py
f1 func
C1
Fax

Example 5:

  • __name__ is a variable automatically set in an executing python program.
>>> print __name__
__main__
>>>
  • If you import your module from another program, __name__ will be set to the name of the module.
  • If you run your program directly, __name__ will be set to __main__.
if __name__ == "__main__name___" :
  # will run only if module directly runs
             print  "I am being run directly"
else:
  # will run only if module imported
             print  "I am being imported" 

Python Iterators

  • Iterator in Python is simply an object that can be iterated upon.
  • An object which will return data, one element at a time.
  • Python iterator object must implement two special methods,

      __iter__()and __next__(), collectively called the iterator protocol.

  • An object is called iterable if we can get an iterator from it.

Most of built-in containers in Python like: list, tuple, string etc. are iterables.

  • The iter() function (which in turn calls the __iter__() method) returns an iterator from them.
>>> dbs=['oracle','sql','db2','server2000']
>>> obj=iter(dbs)
>>> obj    
>>> for v in obj:
               v
'oracle'
'sql'
'db2'
'server2000'
=>   We use the next() function to manually iterate through all the items of an iterator.
=>   When we reach the end and there is no more data to be returned,
       it will raise StopIteration
 
>>> dbs=['oracle','sql','db2','server2000']
>>> obj=iter(dbs)
>>> next(obj)
'oracle'
>>> next(obj)
'sql'
>>> next(obj)
'db2'
>>> next(obj)
'server2000'
>>> next(obj)
Traceback (most recent call last):
  File "", line 1, in
StopIteration 

Python Generator

  • A generator is a function that returns an object (iterator) which we can iterate over (one value at a time).

How to create a generator in Python?

  • It is as easy as defining a normal function with yield statement instead of a return statement.
  • If a function contains at least one yield statement (it may contain other yield or returnstatements),it becomes a generator function.
  • Both yield and return will return some value from a function.
  • The difference is that, while a return statement terminates a function entirely, yield statement pauses the function saving all its states and later continues from there on successive calls.

Differences between Generator function and a Normal function

  • Here is how a generator function differs from a normal function.
  • Generator function contains one or more yield statement.
  • When called, it returns an object (iterator) but does not start execution immediately.
  • Methods like __iter__() and __next__() are implemented automatically.

So we can iterate through the items using next().

  • Once the function yields, the function is paused and the control is transferred to the caller.
  • Local variables and their states are remembered between successive calls.
  • Finally, when the function terminates, StopIteration is raised automatically on further calls.

Example

>>> def   f1():
...          n=1
...          yield n
...          n+=1
...          print "2nd n"
...          yield n
>>>      f1()
 
>>> r1=f1()
>>> r1
>>>
>>> next(r1)
1
>>> next(r1)
2nd n
2
>>> next(r1)

Traceback (most recent call last):
  File "", line 1, in
StopIteration
  • Unlike normal functions, the local variables are not destroyed when the function yields.
  • Furthermore, the generator object can be iterated only once.

Python Generators with a Loop

  • Normally, generator functions are implemented with a loop having a suitable terminating condition.
  • Let’s take an example of a generator that reverses a string.
def    rev_str(my_str):
           length = len(my_str)
           for i in range(length - 1,-1,-1):
                      yield  my_str[i]
 
         for char in rev_str("hello"):
                         print(char)

# For loop to reverse the string

# Output:

# o

# l

# l

# e

# h

Why generators in Python?

There are several reasons which make generators an attractive implementation to go for.

1.  Easy to Implement

  •  Generators can be implemented in a clear and concise way as compared to their iterator class counterpart.
  • Following is an example to implement a sequence of power of 2’s using iterator class.
class   PowTwo:
    def    __init__ self, max = 0 :
             self.max = max
 
    def     __iter__(self):
             self.n = 0
            return self
 
    def   __next__(self):
          if   self.n > self.max:
                 raise StopIteration
 
        result = 2 ** self.n
        self.n += 1
        return result
  • This was lengthy. Now let’s do the same using a generator function.
def    PowTwoGen(max = 0):
         n = 0
        while n < max>
               yield 2 ** n
               n += 1
  • Since, generators keep track of details automatically; it was concise and much cleaner in implementation.

2.  Memory Efficient

  • A normal function to return a sequence will create the entire sequence in memory before returning the result. This is overkill if the number of items in the sequence is very large.
  • Generator implementation of such sequence is memory friendly and is preferred since it only produces one item at a time.

3. Represent Infinite Stream

  • Generators are excellent medium to represent an infinite stream of data.
  • Infinite streams cannot be stored in memory and since generators produce only one item at a time, it can represent infinite stream of data.
  • The following example can generate all the even numbers (at least in theory).
def    all_even():
           n = 0
           while True:
                 yield n
                 n += 2

4.  Pipelining Generators

  • Generators can be used to pipeline a series of operations. This is best illustrated using an example.
  • Suppose we have a log file from a famous fast food chain. The log file has a column (4th column) that keeps track of the number of pizza being sold every hour and we want to sum it to find the total pizzas sold in 5 years.
  • Assume everything is in string and numbers that are not available are

marked as ‘N/A’.

A generator implementation of this could be as follows.

with  open('sells.log') as file:
         pizza_col  =  (line[3] for line in file)
         per_hour  =  (int(x) for x in pizza_col if x != 'N/A')
         print("Total pizzas sold  =  ",sum(per_hour))

Python Generator Expression

  • Simple generators can be easily created on the fly using generator expressions. It makes building generators easy.
  • Same as lambda function creates an anonymous function, generator expression creates an anonymous generator function.
  • The syntax for generator expression is similar to that of a list comprehension in Python. But the square brackets are replaced with round parentheses.
  • The major difference between a list comprehension and a generator expression is that while list comprehension        produces the entire list, generator expression produces one item at a time.
# Initialize the list
my_list = [1, 3, 6, 10]

# square each term using list comprehension

[x**2 for x in my_list]

# Output: [1, 9, 36, 100]
# same thing can be done using generator expression
(x**2 for x in my_list)

# Output:

generator object genexpr at 0x0000000002EBDAF8

Reference

https://www.python.org/

https://www.anaconda.com/

Continue exploring at Teknonauts.com

]]>
https://teknonauts.com/python-programming-06/feed/ 0
#29 Python Programming – Python Function, File Dictionary and Exception Handing https://teknonauts.com/python-programming-05/ https://teknonauts.com/python-programming-05/#respond Wed, 14 Jul 2021 07:45:49 +0000 https://teknonauts.com/?p=4295

Python Function

  Python functions are classified into two types

  • Built-in functions (or) library functions ( example: print(),sort(),list() etc., )
  •  User defined functions – which are created by user.

When you creating user-defined functions don’t use python keywords.

Definition

  • A function is a block of code (or) script .
  • A python function is defined as a group of statements (or) script placed inside another statement or scripts.
  • The main advantage of using function is its reusability in our working script.
  • Whenever we want to repeat a particular operation again and again, functions allow us to write the definition once and call function more than one time.

Function advantages

  • Provide reusable code /script, delivering enhanced modularity for your application.
  • Easy way to debug your script
  • Improve script scalability
  • Allows to import more than one external program
  • The Function operation block,  previously mentioned as definition block ( action block ) will use def keyword
  • Function call is nothing but to invoke a function. In python function call has bottom-up approach.

Function definition syntax

def   function_name() :

           Function definition block

Syntax rules

  • Function name must begin with keyword def
  • Function name is case sensitive .Also avoid the usage of python keywords as a function name.
  • The function definition must end with colon :
  • All other lines inside a function, placed after the function definition are indented.

How to call a function ?

Let’s start with an example function hello().

def   hello() :
         print “This is hello function block”

Calling a function definition (or) invoking a function is nothing but writing a function name, followed by open and close parentheses ( ).

hello()  # This is simple function call
print  “Exit From script”

Whenever python interprets the line hello (), the hello function block will execute and after completing the execution, the control will return back to the script section

Example :

def   hello ( ) :
              print “This is hello function block”
print “B4 function call”
hello() # simple function call
print “After function call”

Output:-

B4 function call

This is hello function block

After function call

Function definitions can be placed in any order. But its execution is based on the order of the function calls

File : p1.py

import os    #  import  python os module file into local script
def    view( ):
          print “This is view function block”
          print “display list of mounted file system details :”
          os.system(“df –Th”)  # df –Th is a unix like operating system command
          print  “Exit from view function”
 
def   display() :
         print “This is display block”
         print “Current process details”
         os.system(“ps –f”)
         print “Exit from display function”
 
print   “Im Script Section “
display()  # 1st call  
view()  # 2nd call
print “Exit From script”

Nested function calls

  • Nested function calls are similar to nested structures where the function calls are placed inside another function.
  • These nested structures are applied in many situations which includes dependency-based actions and stack applications.
  • Let’s have an example
def  functionA() :
           print  “Im functionA”
           functionB() # nested call
 
def functionB():
          print “Im functionB”
          functionC()  # nested call
 
def functionC():
          print “Im functionC”
 
functionA()  # This is main script call
print “Exit from function block”

Output :-

Im functionA

Im functionB

Im functionC

Exit From function block

Function call with arguments

While passing arguments to a function, those arguments should be placed in the correct positional order.

def  hello(a1,a2) :  
          print “1st argument :”,a1
          print “2nd argument:”,a2
          print “Arguments types are :”
          print type(a1)
          print type(a2)


hello(10,20)                                       # arguments are numbers
hello(“/etc/passwd”, “/var/log/auth.log”)   # arguments are string
hello(“data1”,”data2”)                             # arguments are string
hello([‘one’,’two’,’three’],(“three”,”four”))         # arguments are list and tuple
hello({‘key1’:’Value1’,’key2’:’Value2’}, [10,20,30]) # arguments are dict and list

In python, the arguments passed inside a function are classified as

  1. Required arguments
  2. Keyword arguments
  3. Default arguments
  4. Variable-length arguments

Required arguments

  •  These are the function arguments that have to be passed to a function in correct positional order.
  • The order of the arguments should match in both function call and function definition.
  • In case of any mismatch, the python return an error (Type Error) message and exits from the script without continuing to the next statement.

File : p1.py

print "I am python"
def a():  #  No arguments are given
          print "This is a block"
a(10,20)  #  call with two arguments
for v in [1,2,3,4,5]:
          print v
print "Exit from block"

Output :

I am python

Traceback (most recent call last):

  File “a.py”, line 5, in

    a(10,20)

TypeError: a() takes no arguments (2 given)

  • In the above example, the control stops executing the next looping statements placed after the function call.
  • Using exception handling technique we can handle this exception and can make the control move forward to the next statement of execution.
  • Here, the number of arguments (count) in the function call should also match exactly with the number of arguments in function definition.
def   display(a1,a2):

          print “This is display block”

          print “Received 1st argument:”,type(a1)

          print “Received 2nd argument:”,type(a2)

# The above code display block contains two arguments

display(“data1”,”data2”)  # valid call
display() # empty argument – invalid call
display(10,20,30) #more than two arguments - invalid call

Keyword  arguments

  •  Keyword arguments are related to the function calls.
  • When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.
  • This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters.
def  display( name,age) :
          print  “Name:”, name
          print “Age:”,age
 
display(name=”Mr.Xerox”,age=55) 

In python, the function will take the default value if the value is not passed in the function call as an argument.

def display(name=”Mr.Xerox”,dept=”Sales”) :
                   print “Name:”,name
                   print “Dept:”,dept
display()
display(name=”Mr.John”,dept=”Account”)
display(name=”Mr.Paul”)
display(dept=”Production”)
  • A default argument is an argument that assumes a default value if the value is not provided in the function call as argument.

Variable-length arguments

  • You may need to process a function for more arguments than you specify while defining the function.
  • These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.
def  functionname(*variable) :
                   function definition operation
 
functionname(arg1,arg2,arg3……argN)

Example

def  display(a1,*a2):

          print type(a1)  #  type ‘str’

          print type(a2)  #  type `tuple`

display(“data”,10,20,30,40,50,60)

  • An asterisk (*) is placed before the variable name that holds the values of all non-keyword variable arguments.
  • These collection of non-keyword arguments form a tuple and it remains empty if no additional arguments are specified during the function call.

The *args vs **kwargs

  •  We studied  *args allows you to do is take in more arguments than the number of formal arguments that you previously defined.
  •   With *args, any number of extra arguments can be treated as tuple type in formal parameters 
  •  The special syntax**kwargs in function definitions in python is used to pass a keyword variable-length argument list.
  • We use the name kwargs with the double star.
  •  The reason is because the double star allows us to pass through       keyword arguments (and any number of them).
  •  A keyword argument is where you provide a name to the variable as you pass it into the function.
  •  The kwargs as being a dictionary that maps each keyword to the value that we pass along it.
  • That’s the reason when we iterate over the kwargs there doesn’t seem to be ordered result.
  •  kwargsreturn type will be ‘dict’ type.
>>> def    display(**kwargs):
...             print type(kwargs)

...

>>> display(name="Karthik",place="Bangalore")

 type 'dict'

>>> def    display(**kwargs):

 

...     for v in kwargs.keys():

...             print v,"\t",kwargs[v]

>>> display(name="Karthik",place="Bangalore")

#output
place   Bangalore

name    Karthik

Anonymous function

  •  Anonymous function (or) lambda function i.e., functions unnamed or without name.
  • The general syntax of a lambda function:-

lambda argument_list expression

  • The argument_ list consists of list of arguments separated by comma and the expression is
  • an arithmetic expression using these arguments.
  • You can assign the function to a variable to give it a name.

>>> f = lambda x, y : x + y

>>> f(10,20)

30

Scope of variables

  • All variables within a program may not be accessible at all locations in that program.
  • This depends on where you have declared that variable.
  • The scope of a variable determines the portion of the program where you can access a particular identifier.
  • There are two basic scopes of variables in Python ?
  • Global variables
  • Local variables
  • Variables that are defined inside a function is called local variables
  • Local variables can be accessed only inside the function, we can’t access outside the function

Example:

def    hello():
         Fname=”/var/log/result.log”   # local variable
         print “File name:”,Fname  
hello() # function call
print “Outside function  File name:”, Fname  ## NameError
  • We can use a global variable in other functions by declaring it as global in each function that assigns to it.
  • Global variables are accessible from anywhere in the script  i.e., inside a function and outside a function.

Example :

def  hello():
         global Fname #  variable Fname is a global variable
         Fname=”/var/log/result.log”   
 
def  display():
       print “Result log file name is: “,Fname   # Result log file name is:/var/log/result.log
 
hello()
display()
print  “Outside function  File name:”  # display Outside function File name:/var/log/result.log

File Handling categories

  • Read a data from Keyboard(STDIN) to script write a data to FILE ( Not using Monitor(STDOUT) )
  • Read a data from FILE to Script display to Monitor ( Not using STDIN )
  • Read a data from one FILE to Script write a data to another FILE (Not using STDIN/STDOUT. (like: cp old new))
  • When we want to read from or write to a file we need to open it first.
  • When we are done, it needs to be closed, so that resources that are tied with the file are freed.
  • Hence, in Python, a file operation takes place in the following order.
    • Open a file
    • Read or write (perform operation)
    • Close the file

How to open a file?

  • Python has a built-in function open() to open a file.
  • This function returns a file object, also called a handle, as it is used to read or modify the file accordingly.

Syntax :

Object=open(“Inputfile”)
>>> f = open("test.txt")    # open file in current directory
>>> f = open("/var/log/temp.log")  # specifying full path
  • We can specify the mode while opening a file.
  • In mode, we specify whether we want to read ‘r’, write ‘w’ or append ‘a’ to the file.
  • We also specify if we want to open the file in text mode or binary mode.
  • The default is reading in text mode. In this mode, we get strings when reading from the file.
  • On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files.

Python File Modes

Python File Modes
ModeDescription
‘r’Open a file for reading. (default)
‘w’Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
‘x’Open a file for exclusive creation. If the file already exists, the operation fails.
‘a’Open for appending at the end of the file without truncating it. Creates a new file if it does not exist.
‘t’Open in text mode. (default)
‘b’Open in binary mode.
‘+’Open a file for updating (reading and writing)

Example:-

f = open("test.txt")      # equivalent to 'r' or 'rt'
f = open("test.txt",'w')  # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode

How to close a file Using Python?

  • When we are done with operations to the file, we need to properly close the file.
  • Closing a file will free up the resources that were tied with the file and is done using Python close() method.
  • Python has a garbage collector to clean up unreferenced objects but, we must not rely on it to close the file.
f = open("test.txt)
# perform file operations
f.close()

How to read a file using python ?

Syntax :-

Object=Open(“Inputfile”,”mode”)
Example:-
>>> obj=open(“/var/log/result.log”,”r”)  # r – read mode by default.
>>> type(obj)

Read data from file in python we are using different functions.

1.read() – read the entire file contents and returns them as a single string
Example :-

cat IP1.txt
Data1
Data2
Data3
Data4
obj=open(“IP1.txt”)
obj.read()
‘Data1\nData2\nData3\nData4\n’
type(obj.read())

2. readline() – read the file contents line by line return as a single string

>>> obj=open(“IP1.txt”)
>>> obj.readline()
‘Data1’
>>> obj.readline()
‘Data2’
>>> obj.readline()
‘Data3’
>>> obj.readline()
‘Data4’
>>> obj.readline()
“ ”

3. readlines() – read the entire file contents and return as a single list type of data.

>>>obj=open(“IP1.txt”)
>>>type(obj.readlines())
>>>obj.readlines()
[‘Data1\n’,’Data2\n’,’Data3\n’,’Data4\n’]

4. next() – read line by line like readline() and return the next item from the iterator.

  • If default is given and the iterator is exhausted , control returns instead of raising StopIteration
>>>obj=open(“IP1.txt”)
>>>type(next(obj))
>>> next(obj)
‘Data1’
>>> next(obj)
 ‘Data2’
>>> next(obj)
 ‘Data3’
>>> next(obj)
 ‘Data4’
>>> next(obj)
StopIteration 
  •  Note that above object (obj) is iterable object. So will use for loop will read data from file.
>>> obj=open(“IP.txt”) # open a file , bydefault read mode
>>> for var in obj:  # iterable object
              print var.strip() # strip() function is used to remove \n char
>>> obj.close()  # close the file object
     Data1
     Data2
     Data3
     Data4
     Data5
  • This method is not entirely safe. If an exception occurs when we are performing some operation with the file, the code exits without closing the file.
  •  A safer way is to use exception handling techniques which we will be discussing later. 
try:
       f = open(“test.txt”)
except Exception as e:
     print e
else:
      for var in f:
               print var.strip
finally:
       f.close()

How to write to a File in Python?

  • In order to write into a file in Python, we need to open it in write ‘w’, append ‘a.

Syntax:-

open(“Filename”,” w”)

Example:-

f=open("test.txt",'w’):
f.write("my first file\n")
f.write("This file\n\n")
f.write("contains three lines\n")
f.close()
  • This program will create a new file named ‘test.txt’ if it does not exist. If it does exist, it is overwritten.
  • We must include the newline characters ourselves to distinguish different lines.

Using with keyword

with open("test.txt",'w') as f:
        f.write("my first file\n")
        f.write("This file\n\n")
        f.write("contains three lines\n")
  •   This program will create a new file named ‘test.txt’ if it does not exist.
  •   If it does exist, it is overwritten.
  •   When using with keyword, file handler will be closed automatically.
  •  No need to explicitly use f.close().  

Using seek() method

We can change our current file cursor (position) using the seek() method.

>>> obj.seek(0)   # brings file cursor to initial position
0
>>>obj=open(“IP.txt”)
>>>obj.read()
‘Data1\nData2\nData3\nData4\nData5\n’
>>>obj.read()
“ “ # empty string
>>>obj.seek(0)  # brings file cursor to initial position
>>>obj.read()
‘Data1\nData2\nData3\nData4\nData5\n’

Python Directory Handling

  •  A directory or folder is a collection of files and sub directories.
  •  Using python os module we can handle the directories

Get Current Directory

  •   We can get the present working directory using the getcwd()method.
  •   This method returns the current working directory in the form of a string
>>> import os
>>> os.getcwd()
'C:\\Users\\Krosumlabs\\Desktop'

Changing Directory

  • We can change the current working directory using the chdir() method.
>>> os.chdir('C:\\Python33')
>>> print(os.getcwd())
C:\Python33
  •   The new path that we want to change must be supplied as a string argument to this method.
  •   We can use both forward slash (/) and the backward slash (\) to separate the path elements.

List Directories and Files

All files and sub directories inside a directory can be known using the listdir() method.

>>> os.listdir()
['DLLs',
'Doc',
'include',
'Lib',
'libs',
'LICENSE.txt',
'NEWS.txt',
'python.exe',
'pythonw.exe',
'README.txt',
'Scripts',
'tcl',
'Tools']

Making a New Directory

  •  We can make a new directory using the mkdir() method.
  •  This method takes in the path of the new directory. If the full path is not specified the new directory is created in the current working directory.
>>> os.mkdir('test')
>>> os.listdir()
['test']

Renaming a Directory or a File

  • The rename() method can rename a directory or a file.
  • The first argument is the old name and the new name must be supplied as the second argument.
>>> os.listdir()
['test']
>>> os.rename('test','new_one')
>>> os.listdir()
['new_one']

Removing Directory or File

  • A file can be removed (deleted) using the remove() method.
  • Similarly, the rmdir() method removes an empty directory.
>>> os.listdir()
['new_one', 'old.txt']
 
>>> os.remove('old.txt')
>>> os.listdir()
['new_one']
 
>>> os.rmdir('new_one')
>>> os.listdir()
[]
  •  However, note that rmdir() method can only removes empty directories.
  •  In order to remove a non-empty directory we can use the rmtree() method inside the shutil module.
>>> os.listdir()
['test']
>>> os.rmdir('test')
Traceback (most recent call last):
...
OSError: [WinError 145] The directory is not empty: 'test'
 
>>> import shutil
>>> shutil.rmtree('test')
>>> os.listdir()
[]

Multidimensional data structures

Combination of List,tuple and dictionary structures

List  of List

>>> confs=[["p1.log","p2.log","p3.log"],["host01","host02"],
                          ["/etc",  ["passwd",”gpasswd"] ]  ]
>>> type(confs)
>>> confs[0]  # display 0th index
['p1.log', 'p2.log', 'p3.log']
>>> confs[1]  # display 1st index
['host01', 'host02']
>>> confs[2] # display 2nd index
['/etc', ['passwd', 'gpasswd']]
>>> confs[2][1] # list of list 2nd index of 1st index
['passwd', 'gpasswd']
>>>
>>> confs[1] # 1st index
['host01', 'host02']
>>> confs[2][1]
['passwd', 'gpasswd']
>>> confs[2][1][0] # list of list
'passwd'

List of tuple

# List inside tuples
>>> IPs=[('host01','host02','host03'),('host04',('host05'))]
>>> type(IPs)
>>> IPs[0]
('host01', 'host02', 'host03')
>>> type(IPs[0])
>>> IPs[-1]
('host04', 'host05')
>>> IPs[-1][0]
'host04'
>>> IPs[-1][1]
'host05'

Tuple of Tuple

# Read only mode
>>> files=("/etc/passwd",("p1.log","p2.log"),("p3.log"),("p4.log"))
>>>
>>> type(files)
>>>
>>> type(files[1])
>>> files[1]
('p1.log', 'p2.log')
>>>
>>> files[1][0]
'p1.log'

Tuple of List

>>> IPs=(['10.20.30.40','10.20.30.50','10.20.30.65'],['/etc/passwd','/etc/hosts','/etc/gpasswd'])  # tuple of list
>>> type(IPs)
>>> IPs[0]  # 0th index is List
['10.20.30.40', '10.20.30.50', '10.20.30.65']
>>> type(IPs[0]) # List type
>>> IPs[0][0] # tuple of list
'10.20.30.40'
>>> IPs[0][1]
'10.20.30.50'
>>> IPs[1]
['/etc/passwd', '/etc/hosts', '/etc/gpasswd']
>>> IPs[1][-1]
'/etc/gpasswd'
>>> IPs[-1]
['/etc/passwd', '/etc/hosts', '/etc/gpasswd']

Dictionary of List

# Single key points to multiple values ( list )
 Confs={'hosts':['/etc/hosts','/etc/hostname','/etc/sysconfig/network-scripts’]}
# ‘hosts’ is a key – value is list
>>> type(Confs)
>>> Confs['hosts']
['/etc/hosts', '/etc/hostname', '/etc/sysconfig/network-scripts']
>>> type(Confs['hosts'])
>>> Confs['hosts'][0]  # from dict key is pointing to 0th index list
'/etc/hosts'
>>> Confs['hosts'][-1] # from dict key is pointing to last index
'/etc/sysconfig/network-scripts'

Dictionary of tuple

# Dictionary key  value is tuple
 Confs={'hosts':('/etc/hosts','/etc/hostname','/etc/sysconfig/network-scripts’)}
>>> type(Confs)
 
>>> Confs['hosts'] # dictionary key is  tuple value
('/etc/hosts', '/etc/hostname', '/etc/sysconfig/network-scripts')
 
>>> type(Confs['hosts'])
>>> Confs['hosts'][0]  #  0th index
'/etc/hosts'
>>> Confs['hosts'][1]
'/etc/hostname'
>>> Confs['hosts'][2]
'/etc/sysconfig/network-scripts'

Dictionary of Dictionary

# Dictionary is holding another dictionary
>>> Confs={'host1':{'file1':'/etc/hosts'},'host2':{'file2':'/etc/hostname'},
                      'host3':{'file3':'/etc/sysconfig/network-scripts'}}
>>> type(Confs)
>>> type(Confs['host1'])  
>>> Confs['host1'] # host1 key value is dictionary
{'file1': '/etc/hosts'}
>>> Confs['host1']['file1']  # dictionary of another dictionary
'/etc/hosts'

Exception Handling in Python

Exceptions

  • An exception is a signal that an error or other unusual condition has occurred.
  • When that error occurs, Python generate an exception that can be handled, which avoids your program to crash.
  • Exceptions are convenient in many ways for handling errors
  • Below is some examples explaining some common exceptions errors in Python:

Examples

>>> var=10
>>> print Var  # Invalid variable –python case sensitive
Traceback (most recent call last):
  File "", line 1, in
NameError: name 'Var' is not defined
>>> a=['one','two','three']
>>> a[3] # invalid index
Traceback (most recent call last):
File "", line 1, in
IndexError: list index out of range
 
>>> import Http  # module is not loaded
Traceback (most recent call last):
  File "", line 1, in
ImportError: No module named Http
# Temp.log file is not exists
>>> open("Temp.log")
Traceback (most recent call last):
  File "", line 1, in
IOError: [Errno 2] No such file or directory: 'Temp.log'

Handling an exception

  • In basic terminology we are aware of try/except clause.
  • The code which can cause an exception to occur is put in the try block and the handling of the exception is implemented in the except block.

Syntax

try:

    operation

except Exception :

    Handle the exception

Example


NameError: name 'Var' is not defined
>>> try:
...     Var # Exception is occured here
... except NameError:
...     print "Invalid variable"
...
Invalid variable
>>>try:
…     print 1/0
…except ZeroDivisionError:
       print "You can't divide by zero."
  •  The error handling is done through the use of exceptions that are caught in try blocks and handled in except blocks.
  •  If an error is encountered, a try block code execution is stopped and transferred down to the except block.

Handling multiple exceptions

Syntax:-

try:
          operation
 except Exception1:
           Handle the exception
 except Exception2:
           Handle the exception
 except Exception3:
           Handle the exception
 .......
except Exception'N':
           Handle the exception
 
Example:-
>>> try:
...     open("Temp.log") # File is not available
... except NameError:
...     print "Invalid variable"
... except IOError:
...     print "File is not exists"
... except ZeroDivisonError:
...     print "Divide by zero"
...
File is not exists

Try … except … else clause

  • The else clause in a try/ except statement must follow all except clauses, and is useful for the code that must be executed if the try clause does not raise an exception.
try:
        operation
except Exception:
        handle_the_exception_error
else:
         # follows the code that there is no exception    
Example :-
>>> try :
...     var=10
... except NameError:
...     print "Name error"
... else:
...     print "No Exception"
...     print "var value is:",var
No Exception
var value is: 10

Try … finally clause

  • The finally clause is optional.
  • It is intended to define clean-up actions that must be executed under all circumstances.
  • Whether the exception occurs or not, the finally block will always execute.
  • Make sure that the else clause is run before the finally block.

Example:

File : e1.py

try:
           obj=open("Temp.log")
except IOError:
           print "Invalid file"
else:
           print "Read operation"
            for var in obj:
                     print var
                     obj.close
finally:
            print "-----Thank you----"
  •  Let Temp.log file does not exist. So the exception occurs and the control moves to the except clause.
  •   It displays ‘invalid file’ message and then will go to finally block displaying a ‘—-Thankyou —-‘.

C:\Users\Krosumlabs\Desktop>python e1.py

Invalid file

—–Thank you—-

Now create a Temp.log file and then run the Script discussed above.

Now, there is no exception. So the control goes to else clause, followed by the finally block.

C:\Users\Krosumlabs\Desktop>python e1.py

Read operation

dat1

data2

—–Thank you—-

A finally clause is always executed before leaving the try statement, irrespective of whether an exception has occurred or not.

Raising an Exception

  • In Python programming, exceptions are raised when corresponding errors occur at run time, but we can forcefully raise it using the keyword raise.
  • We can also optionally pass values to the exception to clarify why that exception was raised.
>>> raise KeyboardInterrupt
Traceback (most recent call last):
KeyboardInterrupt
>>> raise MemoryError("This is an argument")
Traceback (most recent call last):
MemoryError: This is an argument
>>> try:
...     a = int(input("Enter a positive integer: "))
...     if a <= 0:
...         raise ValueError("That is not a positive number!")
... except ValueError as ve:
...     print(ve)
...    
Enter a positive integer: -2
That is not a positive number!

Python Built-in Exceptions

  •   Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised when corresponding errors occur.
  •    We can view all the built-in exceptions using the local() built-in functions as follows.

>>> locals()[‘__builtins__’]

  •   This will return us a dictionary of built-in exceptions, functions and attributes.

Reference

https://www.python.org/

https://www.anaconda.com/

Continue exploring at Teknonauts.com

]]>
https://teknonauts.com/python-programming-05/feed/ 0