Showing posts with label example code. Show all posts
Showing posts with label example code. Show all posts

Saturday, 25 April 2020

Sample code for Authorization in Python

Achieving Authorization using JWT in Python


Here we are going to access an get API using python authorization by checking the username, password and generating JWT token.

kindly install below packages to proceed with the code.
install flask_jwt 
install flask_restful 
install flask 

once the above steps has been completed, we will be created three files 
app.py - main application file
security.py - security file
user.py - user

Code: app.py


Code: security.py


Code: user.py



If you need further explanation, kindly mention in the comments so that i can give a brief on code flow.
   



Sunday, 5 April 2020

Try Except in python with example

Error handling in Python using Try.. Except..

Consider the below example for your reference, you can see the TRY block followed by the EXCEPT and below that there are two additional blocks ELSE and FINALLY.

Try:
The try block will get executed first and will perform the operations given under it. If there is any exception  during the operation then the try block will be stopped else the try block will completed its execution and by pass the EXCEPT block. Compulsory block 

Except:
This block will get executed only if there is any exception in the try block. This Except block will handle the exception and provide a useful message to user regarding the error that has been occurred. Compulsory block 

Else:
This block will be performed if there are no exception and over successful completion of the try block. This is an optional block.

Finally:
This is a final block and this block will definitely get executed even though if there is any exception and optional block in try except.

Example:

students =[
    {"name":"Ram""marks":[50,40,80]},
    {"name":"Raju""marks":[]},
    {"name":"Babu""marks":[90,80,70]}
]

try:
    for student in students:
        name = student["name"]
        marks = student["marks"]
        total = sum(marks)
        average = sum(marks)/len(marks)

        print(f"{name} has scored: Total={total} & Average={average}")
except Exception as e:
    print("There is error in student")
    print(e)
else:
    print("Student list completed")
finally:
    print("done")

Output:
Ram has scored: Total=170 & Average=56.666666666666664
There is error in student
division by zero
done

Dunder or Magic methods in python with example

Magic methods are usually represented by two underscores at front and two at the back of method names. The name Dunder is because of these underscores. Also mainly used for operator overloading.

In the below example you can see two methods "str" and "repr" with two underscore at the front and two at the back. These two methods are used to give user a output if there are no proper return methods in a class. These can also be used to give a message on the function that has been performed in a class.

Example:

class Cars:
    def __init__(selfnamemodel):
        self.name = name
        self.model = model

    def __str__(self):
        return (f"{self.name} is manufactured in the year {self.model}")

    def __repr__(self):
        return (f"<{self.name} is manufactured in the year {self.model}/>")


car = Cars("Ferrari""2019")
print(car)

Output:
Ferrari is manufactured in the year 2019

Expense Handler Application with advance technologies

Budget Planner  One of the application developed with Ionic 4 and Python-Flask.  Ionic 4 code:  https://github.com/logeshbuiltin/Expense...