Day15

Pramod Ray
3 min readSep 27, 2019

Exception handling : Exception handling is one of the powerful mechanism to handle the run-time error. And maintain the flow of execution.

Handling an exception : In the code is running and exception handling maintain the flow of execution using try block, include except: statement.

Try: try block is used to enclose the code that might throw an exception. It must be used within the method.

If an exception occurs at the particular statement of try block, the rest of the block code will not execute. So, it is recommended not to keeping the code in try block that will not throw an exception.

Python try block must be followed by either except or finally block.

example: -
Syntax:
try:
a = 10
b = 0
c = a/b
print("output",c)
except Exception:
print("something is error")

Argument of an Exception : An exception can have an argument, which is a value that give the reference of exception

try:
a = 10
b = 0
c = a/b
print("output",c)
except Exception as ex:
print("something is error",ex)

The except Clause with No Exception: This kind of a try statement catches all the exception , but does not make the programmer identify the root cause of the problem.

try:
a = 10
b = 0
c = a/b
print("output",c)
except Exception as ex:
print("something is error",ex)
else:
print("there is no exception")

Raising an Exception : You can raise exception in several ways by using the raise statement.

raise [Exception [, args [, trackback]]]

User-Defined Exceptions : Python allows user to create own exception by deriving class from the standard built-in exception.

try:
raise Network-error("Bad host-man")
except Network-error,e:
print(e)

Datetime : date, time and datetime classes provides a number of function to deal with dates, times and time intervals. Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps. Whenever you manipulate dates or time, you need to import datetime function.

There are two kinds of date and time object : naive and aware

Naive : A naive object does not contain enough information to unambiguously locate itself relative. Whether a naive object represent Coordinate Universal Time (UTC), local time, in some timezone is purely up to the program, just like it is up to the program whether a particular number represent meters, miles or mass. Native object are easy to understand and to work with, at the cost of ignoring some aspects of reality.

Aware : An aware object has sufficient knowledge of applicable algorithm and political time adjustment such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object is used is used to represent a specific in time.

JSON : JSON stands for JavaScript object Notation

JSON is an open standard for exchanging data on the web. It supports data structures like object and array. So it is easy to write and read data from JSON.

  • JSON stands for JavaScript Object Notation.
  • JSON is an open standard data-interchange format.
  • JSON is lightweight and self describing.
  • JSON is easy to read and write.
  • JSON is language independent.
  • JSON supports data structures such as array and objects.

Features of JSON

  1. Simplicity
  2. Openness
  3. Self Describing
  4. Internationalization
  5. Extensibility
  6. Interoperability
import json
{
"firstName" : "Yuraj",
"lastName" : "Singh",
"hobbies" : ["running", "playing-cricket", "singing"],
}

Python comes with built-in package called json for encoding and decoding JSON data.

The process of encoding Json is usually called serialization. (Pickling)

Serialization is the reciprocal process of decoding data that has been stored or delivered in the JSON standard. (Depickling)

dump(data, fp): This method is using for writing data to files. Data is object to be serialized. fp is file-like object to which the bytes will be written.

import json
data = {"user" :{"username" : "Sonam","password" : "mickey@1122"}}
write_file = open("data_file.json","w")
json.dump(data, write_file)
write_file.close()

dumps(): This method used for writing to a python string.

json_string = json.dumps(data)

load() : Turning JSON encoded data from a file into python objects.

import json
read_file = open("data_file.json", "r")
data = json.load(read_file)

loads() : Turning JSON encoded data from a string into Python objects.

import json
data = {"user" :{"username" : "Sonam","password" : "mickey@1122"}}
write_file = open("data_file.json","w")
data_object = json.loads(data)

--

--