Sunday, April 29, 2018

Python : Super() vs Self

Python : Super() vs Self vs Classname

All 3 can be differentiated wrt to usage inside th child class. 

General Idea:
  1. Assume Class A , having a method m1
  2. Class B  is child of Class A 
  3. Class B has methods m1 ,m2
  4. If you want to call parent method m1 (ie., A().m1()) inside m1,m2 in Class B and use self , it will call the already exsisting method m1() in the same Class B  .
  5. Hence we use super() so that you are specifying python to use parent method.


super()
1.  Used inside child constructor to call parent constructor
2.  Used inside overriding method to call the parent method

self
1. Used to call method and properties of the same class
2. Used to call methods and properties of parent class


className
1. Can be used to call properties and methods of parent class.
2. Parent class name needs to be hardcoded
3. "self" needs to be sent as an arguement.

1.Mainly used inside child constructor to call parent constructor.
https://www.digitalocean.com/community/tutorials/understanding-class-inheritance-in-python-3
class Bird():
    name=None
    def __init__(self,name):
        self.name=name
    def bird_name(self):
        return(self.name)
    def fly(self):
        return("True")

class non_fly(Bird):
    swim=None
    def __init__(self,name,swim):   #Constr overriding
        super().__init__(name)      #call parent constr

       #  Bird.__init__(self, name) 
        self.swim=swim
    def fly(self):                  #Method Overrdiing
        return("false")

peng=non_fly("peng",True)
print(peng.bird_name(),peng.fly(),peng.swim)#inherit

2. Used in overriding method to class the parent method

class a():
    def m(self):
        return("m")


class b(a):       
    def m(self):
        #print(self.m()) #error - infinite loop case as it means m() in class b.
        print(a.m(self)) # here class name is hardcoded and need to send "self"  
        print(super().m()) #BETTER OPTION
B=b()
B.m() 


O/P:
m

 

3. MRO

Ref:
Python - MRO(Method Resolution Order)
https://stackoverflow.com/questions/30041679/python-self-and-super-in-multiple-inheritance


class a():
    def x(self):
        return("a")  
class b():
    def x(self):
        return("b")
class c(a):
    def s(self):
        print(super().x(),self.x())  #a,b

class d(b,c):
    pass
d().s()

print(d.mro()) 
#(<class '__main__.d'>, <class '__main__.b'>, <class '__main__.c'>, <class '__main__.a'>, <class 'object'>)

Note :
1. d().s()
2. goes to s() in class c
3. super() = wrt current class ie., super() of class c = class a .Therefore "a"
4. self - check if 1st parent has mentioned method , if not it goes to next parent .There class b has method x. Therefore "b"

Thursday, April 19, 2018

SOAP UI - Add Headers to all SOAP Ui Test Steps using Script

Add Headers to all SOAP Ui Test Steps using Script


import com.eviware.soapui.support.types.StringToStringMap 
 def headers = new StringToStringMap()
 headers.put("name","value");
 headers.put("name1","value1")
 headers.put("name2","value2")
 testRunner.testCase.testSteps.each
 {
  q->
  if(q.getValue().config.type.equals("restrequest") || q.getValue().config.type.equals("request"))
  {
   q.getValue().getHttpRequest().setRequestHeaders(headers)
  }
 }