Welcome! This is my personal blog about Web technologies, software development, open source and other related topics
The ideas and opinions expressed here are solely mine and don't represent those of others, either individuals or companies.The code snippets or references to software products or analogous are to be used without any warranty of any kind. If you enjoy the content, feel free to share it and re-use it as long as you provide a link to the original post.
Python allows to apply multiple decorators to callable object.
The decorator function is called based on the order or the sequence it has been listed.
Decorator can also use there own arguments. In the below example @log decorator a parameter is passed with value ‘log sample’.
Log decorator has 2 wrapper functions. Outer function accepts the reference or pointer to the decorated fucntion and an internal function holds arguments that are passed to the decorated fucntion.
The decorated function is called from the internal wrapper function along with the parameters.
The parameter passed to the decorator is also assessible in the decorator function.
Coming back to Decorator stacking. The below example is used to decorate the fucntion with multiple decorators. Here log and executiontime are decorator function and stacked on the decorated function i.e. sample
Decorators are very useful for refactoring or debugging the code.
Decorators are used to but not limited to do the following –
validate the arguments
modiy the arguments
modify the returned objects
See below code snippet calls the log function to the decorator method which returns the log function.
Other way to perform the same is by decorating the method with @decorator method which widely in Python
The above example can be rewritten with following code snippet. Here the calling function is decorated with log function which returns
Arguments passed to the deocrated function can also be made available to the decorator function using *args and **kwargs
See below example for same. Here the decorator calls the decorated function and prints the same. This way you can log the execution of decorated function. Log the time the parameters, output and time taken to execute the fucntion.
Multiple inheritnace is possible in Python but it comes with the Diamond Problem as seen in the diagram below.
Class A is super class of Class B and Class C
Class D is sub-class of Class B and Class C
When instance of Class D is created which Class method will be called? This is a Diamond Problem
MRO will in this case execute the method of the Class B. i.e. in multiple inheritance the method of first in the super class is executed from left to right. If that method does not exists in first super class then will try and check in the second and finally to the parent class.
Use info() method to know which super class method will be executed. See below code snippet
But there are issues with MRO too. If the Class D inherits Class A and Class C it throws error – Cannot create a consistent MRO. See below code.
How to access the class variables without even creating a instance of class?
Class varaibles are created during class construction hence these variables are accessible even before an instance of class is created.
In below code preview_var variable is accessible even before creating and instance of class.
The class variables (preview_var) can also be accessed with the class instance.
You can also see all the class variables with __dict__ property
A class variable is accessible even before the instance of the class is created. You can use these varaibles to load any metadata information.
But a class varaible is cannot be accessed in class instance __dict__ property. See below code sample for object of class i.e. preview__dict__ property does not show class variable and is empty.
Now if you try to update the class variable with the instance of the class the content of the class variable will be only accesible to that instance and not the other instance.
See below example-
preview instance content is changed and is only accessible to that instance while preview1 instance still has the original content for class_var
While class varaibles as mentioned above holds different content for various instance of same class, they are also called as shared variables only when these class varaibles are changed within the class accessed by the class and not the instance of that class.
Below example shows the instances of the Preview class where the counter incremented when the instance is created but the counter value is shared and can be accessed with the class and instance variable. What is instance variable? see here
see constructor of the class updates the counter accessed by the class. i.e. Preview.counter
I hope this helps to understand class.shared variables and different ways of accessing same.
Create a seperate image for each application. Containers can talk to each other and can form a single large application and helps to scale each application
Don’t sotre data in Images
Images are read-only layer. When a container or image is deleted the data is lost
Use Cache Busting strategy
Install the repositories and packages together. As the docker file ises layered architecture having a seperate command for updating repositories and packages may not gurantee the repsoitories will be upto date and may result in dependency issue.
Use Version Pinning
When installing packages specifiy which version you want to install as everytime the image is build it will install the latest package where a application may not be compatible. Version pinning will always ensure the image will have specific version which your application supports.
Create slim images
This will help pull the images from which ever repository you are using quickly
Install only necessary or required packages
This will make image slim or with minimal packages and no unwanted packages are installed so every time image is build with minimal packages the build is fast and light weight. Also the containers created from such images are light weight and fast.
Maintain different images for different environements
Development images may contain debug tools and temprorary files required for debugging and bulding your application. However there tools are not required for production images. Hence remvoe any temporary files/folder and such tools for prodcution by creating a seperate image for different environments.
Use multi-stage builds to create images
Multi-stage build helps to or uses multiple FROM statements where each FROM instruction can use a different base, and each of them begins with new stage of the build and everything that is required for application is in the final image leaving behind unwanted files. This will avoid executing any script that was done traditionaly.
Exclude any unwanted files and folders using .dockerignore file
Use .dockerignore to exlcude any unwanted files and folders e.g.:- temp folder etc. this make the image lighter.
Use CMD instructions to run software in Image
Use CMD instructions to run the software contained in your image along with arguments. CMD should be used in JSON format that is the command and parameters should be seperated by comma.
Minimize build times
By avoiding to send unwanted files to the build context using .dockerengine