0% found this document useful (0 votes)
82 views

Day26 Class Operator Overloading Paramiko

The document discusses operator overloading in Python. It defines a Number class and overloads operators like +, -, and * to allow adding, subtracting, and multiplying Number objects. It shows how to define __add__, __sub__, and __mul__ methods to perform the operations. It then demonstrates creating Number objects and using the overloaded operators on them. It also discusses other special methods like __str__ and built-in functions like dir() and __dict__ that provide information about class objects in Python.

Uploaded by

Arun Mmohanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Day26 Class Operator Overloading Paramiko

The document discusses operator overloading in Python. It defines a Number class and overloads operators like +, -, and * to allow adding, subtracting, and multiplying Number objects. It shows how to define __add__, __sub__, and __mul__ methods to perform the operations. It then demonstrates creating Number objects and using the overloaded operators on them. It also discusses other special methods like __str__ and built-in functions like dir() and __dict__ that provide information about class objects in Python.

Uploaded by

Arun Mmohanty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

10/5/2020 Untitled66 - Jupyter Notebook

In [4]:

##python operator overloading


class number:
def __init__(self, x=0, y=0):
self.x=x
self.y=y

obj1=number(1,2)
obj2=number(2,5)

print(obj1+obj2)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-7955ee499f6d> in <module>
8 obj2=number(2,5)
9
---> 10 print(obj1+obj2)

TypeError: unsupported operand type(s) for +: 'number' and 'number'

In [21]:

##python operator overloading


class number:

def __init__(self, x=0, y=0):


self.x=x
self.y=y

def __str__(self):
return "({0},{1})".format(self.x, self.y)

x=number(2,3) ## memory location same


y=number(6,7) ## memory location same
print(x)
print(x+y)

(2,3)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-c79da0c2d1f2> in <module>
12 y=number(6,7) ## memory location same
13 print(x)
---> 14 print(x+y)

TypeError: unsupported operand type(s) for +: 'number' and 'number'

localhost:8888/notebooks/Untitled66.ipynb?kernel_name=python3 1/7
10/5/2020 Untitled66 - Jupyter Notebook

In [19]:

class number:
def __init__(self, x=0, y=0):
self.x=x
self.y=y

obj1=number(1,2)
print(obj1)

<__main__.number object at 0x000002BC2A4911F0>

In [22]:

##python operator overloading


class number:

def __init__(self, x=0, y=0):


self.x=x
self.y=y

def __str__(self):
return "({0},{1})".format(self.x, self.y)

def __add__(self, other):


a=self.x + other.x
b=self.y + other.y
return number(a,b)

x=number(2,3)
y=number(6,7)
print(x+y)

(8,10)

localhost:8888/notebooks/Untitled66.ipynb?kernel_name=python3 2/7
10/5/2020 Untitled66 - Jupyter Notebook

In [27]:

##python operator overloading


class number:

def __init__(self, x=0, y=0):


self.x=x
self.y=y

def __str__(self):
return "({0},{1})".format(self.x, self.y)

def __add__(self, other):


a=self.x + other.x
b=self.y + other.y
return number(a,b)

def __mul__(self, other):


a=self.x * other.x
b=self.y * other.y
return number(a,b)

def __sub__(self, other):


a=self.x - other.x
b=self.y - other.y
return number(a,b)

x=number(2,3)
y=number(10,7)
print(x-y)

(-8,-4)

localhost:8888/notebooks/Untitled66.ipynb?kernel_name=python3 3/7
10/5/2020 Untitled66 - Jupyter Notebook

In [34]:

class number:

def __init__(self, x=0, y=0):


self.x=x
self.y=y

def __str__(self):
return "({0},{1})".format(self.x, self.y)

def __add__(self, other):


a=self.x + other.x
b=self.y + other.y
return number(a,b)

def __mul__(self, other):


a=self.x * other.x
b=self.y * other.y
return number(a,b)

def __sub__(self, other):


a=self.x - other.x
b=self.y - other.y
return number(a,b)

x=number(2,3)
y=number(10,7)
print(x+y)

(12,10)

In [36]:

#def __pow__(self, other) ### v1**v2

localhost:8888/notebooks/Untitled66.ipynb?kernel_name=python3 4/7
10/5/2020 Untitled66 - Jupyter Notebook

In [38]:

dir(number)

Out[38]:

['__add__',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__sub__',
'__subclasshook__',
'__weakref__']

In [40]:

number.__dict__

Out[40]:

mappingproxy({'__module__': '__main__',
'__init__': <function __main__.number.__init__(self, x=0, y=0)
>,
'__str__': <function __main__.number.__str__(self)>,
'__add__': <function __main__.number.__add__(self, other)>,
'__mul__': <function __main__.number.__mul__(self, other)>,
'__sub__': <function __main__.number.__sub__(self, other)>,
'__dict__': <attribute '__dict__' of 'number' objects>,
'__weakref__': <attribute '__weakref__' of 'number' objects>,
'__doc__': None})

localhost:8888/notebooks/Untitled66.ipynb?kernel_name=python3 5/7
10/5/2020 Untitled66 - Jupyter Notebook

In [51]:

#paramiko module
#pip install paramiko
import paramiko

class MAIN:

def __init__(self,IP,USER,PASS,CMD):
self.IP=IP
self.USER=USER
self.PASS=PASS
self.CMD=CMD
self.PORT=22

def sub_par(self):
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.IP, self.PORT, self.USER, self.PASS, self.CMD)
stdin, stdout, stderr = ssh.exec_command(self.CMD)
status_code= stdout.channel.recv_exit_status()
outlines=stdout.readlines()
resp=''.join(outlines)
ssh.close()

HOST='172.16.0.5'
USER='root'
PASS='redhat@123'
CMD='uptime'
par=MAIN(HOST, USER, PASS, CMD)
par.sub_par()

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-51-9d7a7f595a21> in <module>
28 CMD='uptime'
29 par=MAIN(HOST, USER, PASS, CMD)
---> 30 par.sub_par()

<ipython-input-51-9d7a7f595a21> in sub_par(self)
15 ssh=paramiko.SSHClient()
16 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
---> 17 ssh.connect(self.IP, self.PORT, self.USER, self.PASS, self.C
MD)
18 stdin, stdout, stderr = ssh.exec_command(self.CMD)
19 status_code= stdout.channel.recv_exit_status()

d:\python3\lib\site-packages\paramiko\client.py in connect(self, hostname, p


ort, username, password, pkey, key_filename, timeout, allow_agent, look_for_
keys, compress, sock, gss_auth, gss_kex, gss_deleg_creds, gss_host, banner_t
imeout, auth_timeout, gss_trust_dns, passphrase, disabled_algorithms)
433 key_filenames = key_filename
434
--> 435 self._auth(
436 username,
437 password,

d:\python3\lib\site-packages\paramiko\client.py in _auth(self, username, pas


sword, pkey, key_filenames, allow_agent, look_for_keys, gss_auth, gss_kex, g
ss_deleg_creds, gss_host, passphrase)
localhost:8888/notebooks/Untitled66.ipynb?kernel_name=python3 6/7
10/5/2020 Untitled66 - Jupyter Notebook

658 DEBUG,
659 "Trying SSH key {}".format(
--> 660 hexlify(pkey.get_fingerprint())
661 ),
662 )

AttributeError: 'str' object has no attribute 'get_fingerprint'

In [ ]:

localhost:8888/notebooks/Untitled66.ipynb?kernel_name=python3 7/7

You might also like