> Articles > 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.
Enroll in Python Programming In Depth Course Save 50%
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.
Here are the steps for implementing the framework in python.
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.
Enroll in Python Programming In Depth Course Save 50%
1. Import the python framework file which has classes and methods to be used.
2. Use the python framework file name to access the classes of the python 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 python framework file path if its in different
directory.
Create a framework in python for development environment which can be used for developing specific development environment.
This is the way python framework will be used-
import Devenv
...................
class LinuxProject(Devenv.Project):
...................
Enroll in Python Programming In Depth Course Save 50%
We will write code of python framework in a python file. All the classes and methods of python 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.
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.
Now the python framework Devenv is available which can be used in user file.
Devenv.pyWe have the classes of python framework and we want to use them. The developer will use the python framework classes using python framework file name for writing the subclasses and will implement the abstract methods to provide the functionality of specific part of application.
LinuxProject.pyNow we can place the python framework file and user file in same directory. Then we can run the user file.
If we want to have the python framework file in different directory. Then we have to set the python 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 python framework file.