CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns


Building Framework In Python


In this article, we will see how to build/create a framework in python.

Framework is a collection of generic functionality in form of code which uses specific part written by developer to provide the solution.

What is Framework?

Framework is a collection of generic functionality in form of code which uses specific part written by developer to provide the solution. It will have methods in classes which will be implemented in subclasses written by developers who want to use the framework for providing solution for their specific requirement. So the Framework controls the developer code by calling these methods. So developer is focused on writing the specific part of application and framework has structure for generic behaviors and functionality and calls the specific parts of application written by developer. It is generally referred to work as "Don't call us, we'll call you.". Let's see how building a framework in python is done.


Enroll Now and Get an Early Bird Offer

Implementation of Framework

Here are the steps for implementing the framework.

Step 1-
Write the classes and methods in a python file.

Step 2-
Raise the exception NotImplementedError in methods which are suppose to be written in subclasses.

Step 3-
Have the statement if __name__ == '__main__' at the end.


Framework Usage

1. Import the framework file which has classes and methods to be used.
2. Use the framework file name to access the classes of the framework.
3. Implement the methods which are suppose to be written in subclasses.
4. Have both the files in same directory or environment variable PYTHONPATH set to framework file path if its in different directory.


Framework Devenv - Requirements

Create a framework of development environment which can be used for developing specific development environment.


Design the Framework

Framework Name - Devenv

Framework File Name - Devenv.py

Class - Project
AddFile() - Abstract method, implemented by user for adding the file.
Parameters - None
Return Value - None

Class - ProjectCreator
NewProject() - This will be called when new project has to be created. It will call the method CreateProject().
Parameters - None
Return Value - Created Project
CreateProject() - Abstract method, implemented by user for creating project.
Parameters - None
Return Value - Created Project

Framework Usage-

This is the way framework will be used-

import Devenv
...................
class LinuxProject(Devenv.Project):
...................


Implementation of framework

We will write code of framework in a python file. All the classes and methods of framework will be in this file. The classes and methods will work together to provide solution.

We will raise the exception NotImplementedError in methods which are suppose to be written in subclass. Actually this is the way we are providing abstract method in framework. Developer will implement these methods in subclasses to provide the functionality of specific part of application.

Building Framework in Python - Abstract Method


At the end of the file we will have if statement if __name__ == '__main__'. This is really good feature of python. The statements under this condition will be executed when this python file is executed standalone, otherwise this file is used as module. So the file can be used as standalone application or framework.

Building Framework in Python - main statement for library


Now the framework Devenv is available which can be used in user file.

Devenv.py

Creating a Framework in Python - Code of framework


We have the classes of framework and we want to use them. The developer will use the framework classes using framework file name for writing the subclasses and will implement the abstract methods to provide the functionality of specific part of application.

LinuxProject.py

How to build a Framework in Python - Using Python framework


Now we can place the framework file and user file in same directory. Then we can run the user file.

If we want to have the framework file in different directory. Then we have to set the framework file path to environment variable PYTHONPATH. Then we can run the user file. In this case any other user file from other directories can also use the framework file.

How to create a Framework in Python - Setting framework path


Output

LinuxProjectCreator::CreateProject()
ProjectCreator::NewProject()
Adding Files



Next Article : Closure In Python




Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his course on Advanced Python.







Yearly Subscription


Advanced Python


Data Structures in Python


Design Patterns Through Python

Explore

Courses

Subscription

Knowledge Hub

Design Patterns

Tech Articles

About Us

Overview

Contact Us

CourseGalaxy

CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy