As of the base class to call a method from the child?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
12 views
Hi all, please help, there's a class
import paramiko import time import subprocess import telnetlib class Base(object): def __init__(self, args): self.args = args def __enter__(self): self.con = self._connect() return self.con def __exit__(self, type, value, traceback): self.con.close() def run(self, *args, **kwargs): raise NotImplementedError('Abstract method not implemented.') def _connect(self, **kwargs): raise NotImplementedError('Abstract method not implemented.') class Paramiko(Base): def __init__(self, args): Base.__init__(self, args) def _connect(self): self.con = paramiko.SSHClient() self.con.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.con.connect(hostname=self.args.host, port=self.args.port, username=self.args.user, password=self.args.password) return self.con def _response(self, channel): while not channel.recv_ready(): time.sleep(0.1) stdout =" while channel.recv_ready(): stdout += channel.recv(1024) return stdout def _send_connect(self, cmd): with Base(self.args) connect as: channel = connect.invoke_shell() self._response(channel) channel.send(cmd+'\') return self._response(channel) def run(self, cmd): try: return(self._send_connect(cmd)) except paramiko.ssh_exception.AuthenticationException as e: return(e)

I call it from another class like so
Paramiko(args).run(cmd)
as you can see I Base is the base class for Paramiko, please tell me how to do so would be when I in the base class call the method __enter__ which is the call to self._connect() method worked _connect() of child class i.e. Paramiko and if it is not there then throw Abstract method not implemented. from the base.
I hope clearly wrote what I need), thanks
by | 12 views

2 Answers

0 like 0 dislike
Overload the method __enter__ in class Paramiko.
by
0 like 0 dislike
It initially works
Python 2.7 >>> class Base(object): ... def from_base(self): ... self.from_child() ... def from_child(): ... raise NotImplementedError ... >>> class Child(Base): ... def from_child(self): ... print('child!') ... >>> child = Child() >>> child.from_base() child!
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
110,608 questions
257,187 answers
0 comments
40,796 users