Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Python core/Day 4.ipynb
3074 views
Kernel: Python 3

Exceptions (3hr)

  • About Exceptions

  • Python's Default Exception Handler

  • Using Try/Except/Else/Finally Exceptions

  • Generating User Defined Exceptions

  • More on Exceptions

  • Using Asserts

  • Exception Classes

  • Raise Exceptions

  • Log Generation

  • Debug

Process management and process automation (3hrs)

  • Using os.system, os.popen, os.fork, os.exec functions

  • Using the commands module

  • Using the subprocess module

  • Managing processes using various functions in os module

Python with Databases (2hr)

  • Connection to a database server MySQL and other databases,

  • Connection Create Close

  • Run query towards DB

  • Deal with Query Output

Hands-on

  1. Script to handle multiple exceptions

  2. Script that selects latitudes and longitudes from an SQLite database stored in a file called

  3. Script to generate Address of given location using google API Integration

  4. Script to treat MYSQL data

Exceptions

Exception Classes classes

The following exceptions are used mostly as base classes for other exceptions.

exception BaseException The base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no arguments.

args The tuple of arguments given to the exception constructor. Some built-in exceptions (like OSError) expect a certain number of arguments and assign a special meaning to the elements of this tuple, while others are usually called only with a single string giving an error message.

with_traceback(tb) This method sets tb as the new traceback for the exception and returns the exception object. It is usually used in exception handling code.

  • exception Exception

All built-in, non-system-exiting exceptions are derived from this class. All user-defined exceptions should also be derived from this class.

  • exception ArithmeticError

The base class for those built-in exceptions that are raised for various arithmetic errors: OverflowError, ZeroDivisionError, FloatingPointError.

  • exception BufferError

Raised when a buffer related operation cannot be performed.

  • exception LookupError

The base class for the exceptions that are raised when a key or index used on a mapping or sequence is invalid: IndexError, KeyError. This can be raised directly by codecs.lookup().

with open ("c.txt","r") as f: f.read(0)
try: x=int(input("NUMBER = ")) except ValueError: print("Entered wrong data type") x=int(input("NUMBER = ")) finally: print("The 'try except' is finished")
try: x=int(input("NUMBER=")) y=int(input("Number")) print(x/y) raise ValueError('Represents a hidden bug, do not catch this') raise Exception('This is the exception you expect to handle') except Exception as error: print('Caught this error: ' + repr(error))
NUMBER=2 Number0 Caught this error: ZeroDivisionError('division by zero')
ZeroDivisionError #Syntax Error(EOF Error,Indentation Error) #IOError #ImportError #ValueError #NameError #wrong operation :Type ERROR print("*****ZeroDivisionError*******") try: x=int(input("Enter First Number")) y=int(input("Enter Second Number")) print("sum is:", x+y) print("div is:",x/y) print("product is:",x*y) except (ZeroDivisionError,ValueError) as e: print("oops!!Error either wrong division or wrong data type entered") print("ReEnter:") x=int(input("Enter First Number")) y=int(input("Enter Second Number")) print("sum is:", x+y) print("answer is:",x/y) print("product is:",x*y) print("****Module Error*****") try: import time t=time.strftime("%c") print(t) except ImportError as e: print("OOps you have imported wrong module") print("****IOE Error*****") try: a=input("Enter file name you want to read") f=open(a,"r") x=f.read() print(x) except IOError as e: print("you have entered wrong file name") a=input("ReEnter file name") f=open(a,"r") x=f.read() print(x) print("Name Error") x=6 try: print("the square of x is",(k*k)) except NameError as e: print("youn have entered unknow variable")

Concrete exceptions¶

The following exceptions are the exceptions that are usually raised.

  • exception AssertionError Raised when an assert statement fails.

  • exception AttributeError Raised when an attribute reference (see Attribute references) or assignment fails. (When an object does not support attribute references or attribute assignments at all, TypeError is raised.)

  • exception EOFError Raised when the input() function hits an end-of-file condition (EOF) without reading any data. (N.B.: the io.IOBase.read() and io.IOBase.readline() methods return an empty string when they hit EOF.)

  • exception FloatingPointError Not currently used.

  • exception GeneratorExit Raised when a generator or coroutine is closed; see generator.close() and coroutine.close(). It directly inherits from BaseException instead of Exception since it is technically not an error.

  • exception ImportError Raised when the import statement has troubles trying to load a module. Also raised when the “from list” in from ... import has a name that cannot be found.

What is Assertion?

Assertions are statements that assert or state a fact confidently in your program. For example, while writing a division function, you're confident the divisor shouldn't be zero, you assert divisor is not equal to zero.

  • Assertions are simply boolean expressions that checks if the conditions return true or not. If it is true, the program does nothing and move to the next line of code. However, if it's false, the program stops and throws an error

  • Python's assert statement is a debugging aid that tests a condition. If the condition is true, it does nothing and your program just continues to execute. But if the assert condition evaluates to false, it raises an AssertionError exception with an optional error message.

Python assert Statement

Python has built-in assert statement to use assertion condition in the program. assert statement has a condition or expression which is supposed to be always true. If the condition is false assert halts the program and gives an AssertionError.

# Without Error message def avg(marks): assert len(marks) != 0 return sum(marks)/len(marks) mark1 = [] print("Average of mark1:",avg(mark1))
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-5-bde8efdebeac> in <module>() 5 6 mark1 = [] ----> 7 print("Average of mark1:",avg(mark1)) <ipython-input-5-bde8efdebeac> in avg(marks) 1 # Without Error message 2 def avg(marks): ----> 3 assert len(marks) != 0 4 return sum(marks)/len(marks) 5 AssertionError:
# With error message def avg(marks): assert len(marks) != 0,"List is empty." return sum(marks)/len(marks) mark2 = [] print("Average of mark2:",avg(mark2)) mark1 = [12,12,1,2] print("Average of mark1:",avg(mark1))
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-7-64fe5245f8a9> in <module>() 5 6 mark2 = [] ----> 7 print("Average of mark2:",avg(mark2)) 8 9 mark1 = [12,12,1,2] <ipython-input-7-64fe5245f8a9> in avg(marks) 1 # With error message 2 def avg(marks): ----> 3 assert len(marks) != 0,"List is empty." 4 return sum(marks)/len(marks) 5 AssertionError: List is empty.

Log Generation

Logging is a means of tracking events that happen when some software runs. Logging is important for software developing, debugging and running. If you don’t have any logging record and your program crashes, there are very little chances that you detect the cause of the problem.

There are two built-in levels of the log message.

  • Debug : These are used to give Detailed information, typically of interest only when diagnosing problems.

  • Info : These are used to Confirm that things are working as expected

  • Warning : These are used an indication that something unexpected happened, or indicative of some problem in the near future

  • Error : This tells that due to a more serious problem, the software has not been able to perform some function

  • Critical : This tells serious error, indicating that the program itself may be unable to continue running

Logging module is packed with several features. It has several constants, classes, and methods. The items with all caps are constant, the capitalize items are classes and the items which start with lowercase letters are methods. There are several logger objects offered by the module itself.

  • Logger.info(msg) : This will log a message with level INFO on this logger.

  • Logger.warning(msg) : This will log a message with level WARNING on this logger.

  • Logger.error(msg) : This will log a message with level ERROR on this logger.

  • Logger.critical(msg) : This will log a message with level CRITICAL on this logger.

  • Logger.log(lvl,msg) : This will Logs a message with integer level lvl on this logger.

  • Logger.exception(msg) : This will log a message with level ERROR on this logger.

  • Logger.setLevel(lvl) : This function sets the threshold of this logger to lvl. This means that all the messages below this level will be ignored.

  • Logger.addFilter(filt) : This adds a specific filter filt to the to this logger.

  • Logger.removeFilter(filt) : This removes a specific filter filt to the to this logger.

#importing module #importing module import logging #Create and configure logger logging.basicConfig(filename="1.log", format='%(asctime)s %(message)s', filemode='w') #Creating an object logger=logging.getLogger() #Setting the threshold of logger to DEBUG logger.setLevel(logging.DEBUG) #Test messages logger.debug("Harmless debug Message") logger.info("Just an information") logger.warning("Its a Warning") logger.error("Did you try to divide by zero") logger.critical("Internet is down")

Python has a debugger, which is available as a module called pdb (for “Python DeBugger”, naturally)

# epdb1.py -- experiment with the Python debugger, pdb import pdb x=input("Your Name\t") pdb.set_trace() b = "Auto" c = "Desk" final = a + b + c print (final)

Process management and process automation (3hrs)¶

  • Python subprocess.check_output() function

Using subprocess.check_output() function we can store the output in a variable.

  • Using os.system, os.popen, os.fork, os.exec functions

  • Using the commands module

  • Using the subprocess module

  • Managing processes using various functions in os module

import os cmd = "git --version" returned_value = os.system(cmd) # returns the exit code in unix print('returned value:', returned_value)
returned value: 0
# process path to current working directory import os print ('Starting:', os.getcwd()) print ('Moving up one:', os.pardir) os.chdir(os.pardir) print ('After move:', os.getcwd())
Starting: C:\Users\HP\Downloads\Python(POC) Moving up one: .. After move: C:\Users\HP\Downloads
  • popen():It creates a new process running the command given and attaches a single stream to the input or output of that process, depending on the mode argument. While popen() functions work on Windows, some of these examples assume a Unix-like shell.

  • stdin - The “standard input” stream for a process (file descriptor 0) is readable by the process. This is usually where terminal input goes.

  • stdout - The “standard output” stream for a process (file descriptor 1) is writable by the process, and is used for displaying regular output to the user.

  • stderr - The “standard error” stream for a process (file descriptor 2) is writable by the process, and is used for conveying error messages.

import os print ('popen, read:') pipe_stdout = os.popen('echo "to stdout"', 'r') try: stdout_value = pipe_stdout.read() finally: pipe_stdout.close() print ('\tstdout:', repr(stdout_value)) print ('\npopen, write:') pipe_stdin = os.popen('cat -', 'w') try: pipe_stdin.write('\tstdin: to stdin\n') finally: pipe_stdin.close()
  • The subprocess module present in Python(both 2.x and 3.x) is used to run new applications or programs through Python code by creating new processes.

  • The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions

import subprocess #cmd = "git --version" returned_value = subprocess.call(cmd, shell=True) # returns the exit code in unix print('returned value:', returned_value)
returned value: 0