为什么我的Python类不可调用?
我试图用我在Java中学到的相同方式编写OO Python代码,这可能是我要去南方的地方。我有一个主要脚本和一个其他人写的我修改过的类。我在主脚本中调用这个类,但是我使用的每一种技术我都得不到[class name]。为什么我的Python类不可调用?
下面是最近的尝试。这是主要剧本。为了安全起见,已删除变量directory.pairs。
import yaml import pysftp
import FingerprintKey
with open('config/config.yaml') as settings:
cfg = yaml.load(settings)
host = cfg['host']
username = cfg['username']
password = cfg['password']
serverkey = cfg['fingerPrint']
x = FingerprintKey(serverkey)
options = pysftp.CnOpts()
options.hostkeys.clear()
options.hostkeys.add('www.example.com', u'ecdsa-sha2-nistp384', x)
with pysftp.Connection(host, username=username, password=password, cnopts=options) as sftp:
#for source, destination in directoryPairs.items():
#sftp.get_d(source, destination, preserve_mtime=True)
#if sftp.exists(source):
#files = sftp.listdir(source)
#for f in files:
#sftp.remove(os.path.join(source, f))
sftp.close()
下面是类FingerprintKey.py
import hashlib as hl def trim_fingerprint(fingerprint):
if fingerprint.startswith('ecdsa-sha2-nistp384 384 '):
return fingerprint[len('ecdsa-sha2-nistp384 384 '):]
return fingerprint
def clean_fingerprint(fingerprint):
return trim_fingerprint(fingerprint).replace(':', '')
class FingerprintKey:
def __init__(self, fingerprint):
self.fingerprint = clean_fingerprint(fingerprint)
def compare(self, other):
if callable(getattr(other, "get_fingerprint", None)):
return other.get_fingerprint() == self.fingerprint
elif clean_fingerprint(other) == self.get_fingerprint():
return True
elif hl.md5(other).digest().encode('hex') == self.fingerprint:
return True
else:
return False
def __cmp__(self, other):
return self.compare(other)
def __contains__(self, other):
return self.compare(other)
def __eq__(self, other):
return self.compare(other)
def __ne__(self, other):
return not self.compare(other)
def get_fingerprint(self):
return self.fingerprint
def get_name(self):
return u'ecdsa-sha2-nistp384'
def asbytes(self):
# Note: This returns itself.
# That way when comparisons are done to asbytes return value,
# this class can handle the comparison.
return self
这些文件的两者都是在同一个目录。
回答:
感谢mkrieger1我对代码进行了以下补充,并且工作正常。
我改名的类文件AuthOnFingerPrint
import AuthOnFingerPrint options.hostkeys.add('www.example.com', u'ecdsa-sha2-nistp384 384 ', AuthOnFingerPrint.FingerprintKey(serverkey))
以上是 为什么我的Python类不可调用? 的全部内容, 来源链接: utcz.com/qa/260332.html