Month: May 2022

Python – Method Resolution Order

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.

Python – Magic methods

Did you know Python internally uses magic method for operators e.g. for + operator uses __add__() method

number = 10
print(number + 10)

#internally calls __add__() method


The “+” operator depends on the type of the values been added i.e. int, float,string etc.

Now adding 2 integer or float values will add the values while for string it will concatenate the string.

But what about the objects? See below example when 2 objects are added it throws error.

This can be resolved with the magic method i.e. using __add__()

See below example. Here the “+” operator when used on objects calls the __add__() method.

How to know such magic methods for various types? Use help() method

help() method displays documentation for the


Here is a list of operators to magic method mapping-

Comparision methods

OperatorMagic methodImplementation meaning or purpose
==__eq__(self, other)equality operator
!=__ne__(self, other)inequality operator
<__lt__(self, other)less-than operator
>__gt__(self, other)greater-than operator
<=__le__(self, other)less-than-or-equal-to operator
>=__ge__(self, other)greater-than-or-equal-to operato

Unary Operators

OperatorMagic methodImplementation meaning or purpose
+__pos__(self)unary positive, like a = +b
__neg__(self)unary negative, like a = -b
abs()__abs__(self)behavior for abs() function
round(a, b)__round__(self, b)behavior for round() function

Binary Operators

OperatorMagic methodImplementation meaning or purpose
+__add__(self, other)addition operator
__sub__(self, other)subtraction operator
*__mul__(self, other)multiplication operator
//__floordiv__(self, other)integer division operator
/__div__(self, other)division operator
%__mod__(self, other)modulo operator
**__pow__(self, other)exponential (power) operator

Unary Operators and assignments

OperatorMagic methodImplementation meaning or purpose
+=__iadd__(self, other)addition and assignment operator
-=__isub__(self, other)subtraction and assignment operator
*=__imul__(self, other)multiplication and assignment operator
//=__ifloordiv__(self, other)integer division and assignment operator
/=__idiv__(self, other)division and assignment operator
%=__imod__(self, other)modulo and assignment operator
**=__ipow__(self, other)exponential (power) and assignment operator

Type conversion methods

FunctionMagic methodImplementation meaning or purpose
int()__int__(self)conversion to integer type
float()__float__(self)conversion to float type
oct()__oct__(self)conversion to string, containing an octal representation
hex()__hex__(self)conversion to string, containing a hexadecimal representation

Object introspection

FunctionMagic methodImplementation meaning or purpose
str()__str__(self)responsible for handling str() function calls
repr()__repr__(self)responsible for handling repr() function calls
format()__format__(self, formatstr)called when new-style string formatting is applied to an object
hash()__hash__(self)responsible for handling hash() function calls
dir()__dir__(self)responsible for handling dir() function calls
bool()__nonzero__(self)responsible for handling bool() function calls

Object retrospection

FunctionMagic methodImplementation meaning or purpose
isinstance(object, class)__instancecheck__(self, object)responsible for handling isinstance() function calls
issubclass(subclass, class)__subclasscheck__(self, subclass)responsible for handling issubclass() function calls

Object attribute access

Expression exampleMagic methodImplementation meaning or purpose
object.attribute__getattr__(self, attribute)responsible for handling access to a non-existing attribute
object.attribute__getattribute__(self, attribute)responsible for handling access to an existing attribute
object.attribute = value__setattr__(self, attribute, value)responsible for setting an attribute value
del object.attribute__delattr__(self, attribute)responsible for deleting an attribute

Methods allowing access to containers

Expression exampleMagic methodImplementation meaning or purpose
len(container)__len__(self)returns the length (number of elements) of the container
container[key]__getitem__(self, key)responsible for accessing (fetching) an element identified by the key argument
container[key] = value__setitem__(self, key, value)responsible for setting a value to an element identified by the key argument
del container[key]__delitem__(self, key)responsible for deleting an element identified by the key argument
for element in container__iter__(self)returns an iterator for the container
item in container__contains__(self, item)responds to the question: does the container contain the selected item?

References –

Python – Creating and Accessing Class Variables

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.

Python – Creating and Accessing Instance Variable

Instance varaibles are created in class and can also be created during any moment of objects life.

In the below code snippet instance_var is a instance variable.

Create a variable in the object life without adding it in class

Below code adds someother_var after the object is created and prints the value of the variable.

Access the instance variables using __dict__ object property

See below code how to see all the properties of the object

Dockerfile Best Practice

Build Modular Images i.e. Decouple applications

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

References –

Linux and Docker on Ubuntu Series

Linux basic commands (Ubuntu)

Linux Kernel and hardware

Linux Runlevels

Linux Package Management for Ubuntu

Linux User Management commands

Linux Networking commands

Install Docker on Linux

Linux File Types

Create a file in Linux

How to check the file size in Linux

Compressing and Uncompressing Files in Linux

Searching files and directories in Linux

Search content with pattern in the file in Linux

Search content with pattern in the file in Linux

File Permissions in Linux

Check running services in Linux


Docker FAQ’s

Install Docker on Ubuntu

Install Docker using install script on Ubuntu

Setup a Docker Swarm

Cache Busting and Version Pinning when building Docker images

Docker storage on Ubuntu

How to start docker in debug mode in Ubuntu

Docker Restart Policies

Use Docker image offline with Save and Load command in Ubuntu

Export Container and Import as Image using Docker in Ubuntu

Create a custom network in docker for communication between containers

Dcoker Security

Docker Best practice

Index and Search OrderCloud Extended Properties

This article explains usage of extended properties in OrderCloud-

But it doesn’t tell about how to index the extended properties to get the search results.

This blog post helps to setup the XP Indices feature already available with the OrderCloud and search such properties.

This example shows extended properties for Categories for which first Catalog needs to setup

1. Create Catalog from Portal (Assume Marketplace is already created)-

Navigate to Product Catalogs => Catalog => Select an operation “Create a new catalog”

Use below JSON or fill in form to create catalog

  "ID": "0001",
  "Active": true,
  "Name": "HabitatMaster",
  "Description": "HabitatMaster"

Catalog will be created with 201 status

2. Create Catagories from Portal with reference to Catalog Id created previously-

Navigate to Product Catalogs => Categories=> Select an operation “Create a new category”

Use below JSON to create catqagory for Catalog “0001” with the extended property “DisplayType” as “Text”

  "ID": "0001-CategoryDisplayText",
  "Active": true,
  "Name": "CategoryDisplayText",
  "Description": "CategoryDisplayText",
  "xp": {
    "DisplayType": "Text"

Likewise create another catagory with DisplayType Image as per below JSON-

  "ID": "0001-CategoryDisplayImage",
  "Active": true,
  "Name": "CategoryDisplayImage",
  "Description": "CategoryDisplayImage",
  "xp": {
    "DisplayType": "Image"

3. Search Category with newly created extended property i.e. DisplayType

First search all categories with Catalog Id “0001”. Following results will be shown-

Now lets try and search with extended properties i.e. Catalog Id “0001” and DisplayType as Text. For this use “Add Filter” option and key as “xp.DisplayType” and Filter Value as “Text”.

This request wont show any results and hence Xp Indices needs to created for Category

4. Create XP Indices

Navigate to Seller => Xp Indices

Indexable resources Catagory “Add Index” => “xp.DisplayType” and Save Changes

Now search the Catagories same as in Step 3 with Filter option and this time you should be able to see the results-

Compressing and Uncompressing Files in Linux

Compress a file in Linux using tar command

To compress multiple files us tar command with cf option

tar -cf test.tar <<file1>> <<file2>> <<file3>>

To see the content of the tar files use tf option

tar -tf test.tar

To extract the contents from tar file use xf option-

tar -f test.tar

Use zcf command to reduce the file size

tar -zcf testzcf.tar <<file1>> <<file2>>

See the size of test.tar and testzcf.tar file. The content in the tar is same but the size of the testzcf.tar is reduced with zcf command

There are other utlitlies to reduce the file size i.e. to compress files

Use gzip to compress file-

gzip <<file>>

This created filename.gz file. See the size of the firstfile.txt when comperessed

You can also use bzip2 and xz to compress files

Decompress a file

To decompress the gzip file use gunzip command-

You can use bunzip2 and unxz to decompress file